Registrations are now open. Join us today!
There is still a lot of work to do on the wiki yet! More information about editing can be found here.
Already have an account?

Microsoft KB Archive/105766

From BetaArchive Wiki

Article ID: 105766

Article Last Modified on 2/14/2005



APPLIES TO

  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 4.0 Enterprise Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition



This article was previously published under Q105766

SYMPTOMS

Binding a masked edit control (MSMASKED.VBX) to a data control can result in the following error message when the form is loaded:

Invalid property value.

Additionally, the following error can occur when you attempt to update a record:

Type mismatch

For example, this problem can occur when a masked edit control is bound to a date field and has the mask ##/##/##.

CAUSE

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.

RESOLUTION

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.

STATUS

This behavior is by design.

MORE INFORMATION

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.

  • If the PromptInclude property is True (the default), the data coming from the data control must match the Mask property exactly or an "Invalid property value" error occurs. If a user tries to change a valid value in a bound masked edit control and does not entirely fill up the masked edit control, the error "Type mismatch" occurs upon writing values into a numeric data type field of a database.
  • If PromptInclude is False, the masked edit control does not require or provide literal characters from the Mask property. In other words, the Text property operates like ClipText. For example, a Mask of ##/##/## receives the date 1/2/93 into the Text property as 12/93/__.

The only time it would be advisable to bind a masked edit control directly to a data source would be when you are binding the masked edit control to a fixed-length primary key field; that is, a field that holds a unique value for each record in a table, and that value is always a fixed number of characters in length; for example, a serial number or product identification code.

Step-by-Step Example for the Workaround

This example shows how to use the Masked edit control with an Access database. This particular example demonstrates using the Masked edit control on a field of data type Date/Time.

Before testing the example, either the load DATAMGR.EXE file (located in the \VB directory) or run the VISDATA.MAK file (located in the \VB\SAMPLES\VISDATA directory.) Open the BIBLIO.MDB sample database by selecting it after choosing Open Database from the File menu and selecting the Access database option.

Next, select the Authors table, and click the Design button. Select the Add button, enter Dates for the field name, and select the Date/Time for the field type. Then choose OK to close the Add Field window, and then click the Open button. Add several dates to the Dates field in an "mm/dd/yy" format. You may leave some of the Dates fields blank, but you should enter at least five different dates of five different records in the Authors table to test the example.

Now, you are ready to complete the example:

  1. Start Visual Basic for Windows, or from the File menu, choose New Project (ALT, F, N) if Visual Basic for Windows is already running. Form1 is created by default.
  2. Add the following controls with the associated properties to Form1:

       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
    
                        
  3. Add the following lines of code to the (general)(declarations) section of Form1:

       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
                        
  4. Add the following code to the Load event of Form1:

       Sub Form_Load ()
    
          UpdFlag = False
    
       End Sub
    
                        
  5. Add the following code to the Validate Event of the Data1 Control:

       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
    
                        
  6. Add the following code to the KeyPress event of MaskedEdit1:

       Sub MaskedEdit1_KeyPress (KeyAscii As Integer)
    
          UpdFlag = True
    
       End Sub
    
                        
  7. Add the following code to the Change event to MaskedEdit1:

       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
    
                        
  8. Add the following code to the Change event of Text1:

       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
    
                        
  9. Press the F5 key to run the program. The masked edit control should behave as if it was bound to the data control.

    NOTE: This example verifies that the dates are valid in the Validate event before actually placing the dates in the database.


Keywords: kbprb KB105766