Article ID: 170358
Article Last Modified on 6/29/2004
Private Sub Data1_Reposition()
'Check to see if the record is empty or contains
'a zero length string.
'Note: The AllowZeroLength property of the field must be True.
If IsNull(Data1.Recordset("Phone")) Or _
Data1.Recordset("Phone") = "" Then
'Set the Masked Edit Box to an empty string
'Note: This sets the DataChanged for MaskEdBox1 to True
MaskEdBox1.Text = "(___) ___-____"
bFieldNull = True 'The field has no value
Else
bFieldNull = False 'The field has a value
End If
'Set the mask for the control
MaskEdBox1.Mask = "(###) ###-####"
End Sub
Private Sub Data1_Validate(Action As Integer, Save As Integer)
If MaskEdBox1.DataChanged Then
'Check to see if the Masked Edit Box has an empty string
'and that the field was empty when you moved to this record
If MaskEdBox1.Text = "(___) ___-____" And bFieldNull Then
'If the control has an empty string and the field is null
'set the DataChanged to False so that you don't
'update the field
MaskEdBox1.DataChanged = False
'If the control now has an empty string but the field
'had a value when moved to this record you want to remove
'the value from the field
ElseIf MaskEdBox1.Text = "(___) ___-____" _
And Not bFieldNull Then
'Turn the mask off so you can save an empty record
MaskEdBox1.Mask = ""
MaskEdBox1.Text = ""
Data1.UpdateRecord 'update the bound controls
End If
End If
End Sub
Dim bFieldNull As Boolean
Dim db As Database
Dim rs As Recordset
Private Sub Form_Load()
'Create a table with a phone field. Put some records into it.
'Some records will have a phone value, some will be empty
Dim fd As Field
Dim td As TableDef
Dim i As Integer
On Error GoTo myerr ' The next line will cause an error if the
'database already exists
'Create a database containing a table, "Table1", which contains
'one field, "Phone".
Set db = Workspaces(0).CreateDatabase("MaskTest", _
dbLangGeneral)
Set td = db.CreateTableDef("Table1")
Set fd = td.CreateField("Phone", dbText, 14)
fd.AllowZeroLength = True
td.Fields.Append fd
db.TableDefs.Append td
'Add data to the Phone Field for even number records. The Phone
'Field for odd number records will remain null.
Set rs = db.OpenRecordset("table1")
For i = 1 To 6
rs.AddNew
If i Mod 2 = 0 Then
rs("phone") = "(234) 654-9876"
End If
rs.Update
Next
db.Close
LoadTable 'Call the LoadTable sub
Exit Sub
myerr:
LoadTable 'Call the LoadTable sub
End Sub
Private Sub LoadTable()
'Set the Recordset for the Data Control to all the records
'in Table1 and bind the Phone field to the Masked Edit control.
Set db = DBEngine(0).OpenDatabase("MaskTest")
Set rs = db.OpenRecordset("Table1")
rs.MoveFirst
Set Data1.Recordset = rs
MaskEdBox1.Mask = "(###) ###-####"
text1.DataField = "Phone"
MaskEdBox1.DataField = "Phone"
End Sub
Keywords: kbprb KB170358