Use Masked Edit Box with Variable Length Data from a Database

Category:
Forms and Controls
Type:
Snippets
Difficulty:
Intermediate
Author:
Brock Weaver

Version Compatibility:

More information:
If you've ever tried to use the DataSource and DataField properties of the Masked Edit control (at least 6.0 SP3), there's some common annoyances:

* Null fields leave the previous value visible in the control, making the user think the data becomes corrupted
* Variable-length data can adversely affect your data. For instance, if you allow 5 or 9 length Zip codes, data sometimes spills over from from record to the next
* If you manually handle the data transfer, you have to be sure to sync up your Mask property with your code.

This generic function ToMask eliminated all these problems for me (at least so far!)

NOTE: The function assumes that the data passed to it does not contain the mask character or any other filler characters. For instance, if you want to display $111.11 in the box pass 11111 as a value, and set the mask property to something like $###.##

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Code: