PRB: Invalid Property Value When Binding Masked Edit ControlID: Q105766
|
Binding a masked edit control (MSMASKED.VBX) to a data control can result in the error "Invalid property value" when the form is loaded. Also, the error "Type mismatch" can occur when you attempt to update a record. For example, this problem can occur when a masked edit control is bound to a date field and has the mask ##/##/##.
Visual Basic uses the Text property of a bound masked edit control to transfer data to and from the data control. This requires strict compatibility, character for character, between the Mask property and the format of the data in the database. For example, dates are stored in a database as a numeric value, not as a string of the format dd/mm/yy.
Bind an invisible text box to the data control instead of binding the
masked edit control. Then transfer data between the text box and the
masked edit control. This allows you precise control over the format of
the data for the masked edit control making it appear as if the masked
edit control is bound.
This alternative approach involves more lines of code and complexity,
but it should prove much more flexible and forgiving. Below is an example
showing how to use this technique with a date field.
This behavior is by design.
When a bound masked edit control tries to pull data in from a data
control, it behaves differently depending upon the PromptInclude property
of the masked edit control.
Control Name Property Settings
-------------------------------------------------------------
Data Data1 DatabaseName = "BIBLIO.MDB"
RecordSource = "Authors"
MaskedEdit MaskedEdit1 Mask = "##/##/##"
PromptInclude = False
TextBox Text1 Visible = False
DataSource = Data1
DataField = Dates '** this field was
added to BIBLIO.MDB previously
Dim UpdFlag As Integer 'Flag to indicate updating the text box from
' the data control or from the masked edit
' control.
Const MAXMASKLEN = 6 ' This constant is the maximum number of
' characters the user can enter in this
' particular MaskedEdit.
Function IsValidDate% (MyMask As MaskEdBox, MaskFullLen As Integer)
' This function checks the validity of a date in a Masked edit control.
' It returns a zero if the FormattedText is not a valid date, a one
' if the field is empty, and a two if the FormattedText is valid.
'
' Parameters:
' MyMask - the Masked edit control being checked
' MaskFullLen - max. number of chars the Masked edit control can hold
If MyMask.Text = "" Then
IsValidDate% = 1
ElseIf Len(MyMask.Text) = MaskFullLen Then
If IsDate(MyMask.FormattedText) Then
IsValidDate% = 2
End If
Else
IsValidDate% = 0
End If
End Function
Sub Form_Load ()
UpdFlag = False
End Sub
Sub Data1_Validate (Action As Integer, Save As Integer)
Const DATA_ACTIONCANCEL = 0
If IsValidDate%(MaskedEdit1, MAXMASKLEN) = False Then
MsgBox "Not a valid date!"
Action = DATA_ACTIONCANCEL 'don't allow changes
MaskedEdit1.SetFocus
Exit Sub
End If
End Sub
Sub MaskedEdit1_KeyPress (KeyAscii As Integer)
UpdFlag = True
End Sub
Sub MaskedEdit1_Change ()
If UpdFlag = True Then
Select Case IsValidDate%(MaskedEdit1, MAXMASKLEN)
Case 1
Text1.Text = ""
Case 2
Text1.Text = CVDate(MaskedEdit1.FormattedText)
End Select
End If
End Sub
Sub Text1_Change ()
Const DATEFMT = "mmddyy"
' The invisible text box can get changed two ways: from the
' database because it is bound or from the MaskedEdit when pushing
' values back into the data control. This condition handles the
' situation when the data is coming from the database and the
' MaskedEdit needs to be updated.
If Not UpdFlag Then
If Text1.Text = "" Then ' If NULL condition then
MaskedEdit1.Text = "" ' Set the MaskedEdit to ""
Else
MaskedEdit1.Text = Format$(Text1.Text, DATEFMT) 'Format output.
End If
End If
UpdFlag = False
End Sub
Additional query words: 3.00 docerr
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 8, 1999