help creating custom "input masks" and entry validation for textboxes

350
1
03-11-2011 10:30 AM
JillFritz
New Contributor
I have several entry validations I would like to do for my forms; but, vba in arcgis does not have built-in "input masks". (Does C# in arcgis offer more straight forward input masking?)

Can anyone offer their solutions or point to online solutions to coding "input masks" and error validation for textboxes in vba/arcgis?

I especially need an input mask and entry validation for a date textbox.

My first code sample below is my attempt at both an input mask and validation for a date textbox; but, there are still incorrect entry's that can slip through; for example 02/31/2011 would be valid). The second code sample is a simple textbox entry validation for length not to exceed a specific value (the maximum value of the table field to which the data will be saved).

Seems like we should have a library of basic form code to share among ourselves.

Regards, Jill (USFS Region 6)

---------------------------------
Private Sub txtReqDate_Change()
On Error GoTo EH

'Data entry validation
If Not txtReqDate = "" Then
[INDENT]If Len(txtReqDate) = 2 Then
[INDENT]If Left(txtReqDate, 2) > 12 Or Left(txtReqDate, 2) < 0 Or IsNumeric(Left(txtReqDate, 2)) = False Then
[INDENT]MsgBox "ERROR: Re-enter Month.", vbOKOnly, "ERROR in Date Requested on Form"
'txtReqDate.Value = ""
Exit Sub[/INDENT] Else
[INDENT]txtReqDate.Value = txtReqDate.Value & "/"
Exit Sub[/INDENT] End If[/INDENT]
ElseIf Len(txtReqDate) = 5 Then
[INDENT]If Left(txtReqDate, 2) > 12 Or Left(txtReqDate, 2) < 0 Or _
Mid(txtReqDate, 4, 2) > 31 Or Mid(txtReqDate, 4, 2) < 0 Or _
IsNumeric(Mid(txtReqDate, 4, 2)) = False Then
[INDENT]MsgBox "ERROR: Re-enter day.", vbOKOnly, _
"ERROR in Date Requested on Form"
txtReqDate.Value = Left(txtReqDate.Value, 3)
Exit Sub[/INDENT] Else
[INDENT]txtReqDate.Value = txtReqDate.Value & "/"
Exit Sub[/INDENT] End If[/INDENT]
ElseIf Len(txtReqDate) = 10 Then
[INDENT]If IsDate(txtReqDate.Value) = False Then
[INDENT]MsgBox "ERROR: Re-enter date as 'MM/DD/YYYY')", vbOKOnly, "ERROR in Date Requested on Form"
'txtReqDate.Value = ""
Exit Sub[/INDENT] End If
If IsNumeric(Right(txtReqDate, 4)) Then
[INDENT]temp1 = CInt(Right(txtReqDate, 4))[/INDENT] Else
[INDENT]MsgBox "ERROR: Re-enter year.", vbOKOnly, _
"ERROR in Date Requested on Form"
txtReqDate.Value = Left(txtReqDate.Value, 6)
Exit Sub[/INDENT] End If
temp2 = CInt(DatePart("yyyy", Date))
If Right(txtReqDate, 4) < 1900 Or IsNumeric(Right(txtReqDate, 4)) = False Or temp1 > temp2 Then
[INDENT]MsgBox "ERROR: Re-enter year.", vbOKOnly, "ERROR in Date Requested on Form"
txtReqDate.Value = Left(txtReqDate.Value, 6)
Exit Sub[/INDENT] End If
If IsDate(txtReqDate.Value) = False Then
[INDENT]MsgBox "ERROR: Re-enter date as 'MM/DD/YYYY')", vbOKOnly, "ERROR in Date Requested on Form"
'txtReqDate.Value = ""
Exit Sub[/INDENT] End If[/INDENT] ElseIf Len(txtReqDate) > 10 Then
[INDENT]MsgBox "ERROR: Date must be entered as 'MM/DD/YYYY')", vbOKOnly, "ERROR in Date Requested on Form"
txtReqDate.Value = Left(txtReqDate.Value, 10)
Exit Sub[/INDENT] End If[/INDENT] End If

EH:
MsgBox Err.Description, vbInformation, "Change textbox: Request Date"
End Sub



Private Sub cmdSave_Click()
On Error GoTo EH

If txtReqDate <> "" Then
[INDENT] If IsDate(txtReqDate.Value) = False Or Len(txtReqDate) <> 10 Or Left(txtReqDate, 2) > 12 Or Left(txtReqDate, 2) < 0 Or _
[INDENT]Mid(txtReqDate, 4, 2) > 31 Or Mid(txtReqDate, 4, 2) < 0 Or _
IsNumeric(Mid(txtReqDate, 4, 2)) = False Then
MsgBox "ERROR: Date must be entered as 'MM/DD/YYYY')", vbOKOnly, "ERROR in Date Requested on Form"
Exit Sub[/INDENT] Else
[INDENT]Dim temp1, temp2 As Integer
If IsNumeric(Right(txtReqDate, 4)) Then
[INDENT]temp1 = CInt(Right(txtReqDate, 4))[/INDENT] Else
[INDENT]MsgBox "ERROR: Date must be entered as 'MM/DD/YYYY')", vbOKOnly, "ERROR in Date Requested on Form"
Exit Sub[/INDENT] End If
temp2 = CInt(DatePart("yyyy", Date))
If Right(txtReqDate, 4) < 1900 Or IsNumeric(Right(txtReqDate, 4)) = False Or temp1 > temp2 Then
[INDENT]MsgBox "ERROR: Date must be entered as 'MM/DD/YYYY')", vbOKOnly, "ERROR in Date Requested on Form"
Exit Sub[/INDENT] End If[/INDENT] End If[/INDENT] End If


-----------------------------
Private Sub txtGNIS_Change()
On Error GoTo EH

[INDENT] 'Data entry validation
If Not txtGNIS = "" Then
[INDENT] If Not Len(txtGNIS) < 21 Then
[INDENT]MsgBox "ERROR: GNIS must be 20 or less characters", vbOKOnly, "ERROR in GNIS textbox on Form"
txtGNIS = Left(txtGNIS, 20)
Exit Sub[/INDENT] End If
[/INDENT]
End If[/INDENT]EH:
[INDENT] MsgBox Err.Description, vbInformation, "Change textbox: GNIS ID"[/INDENT]End Sub
0 Kudos
1 Reply
DuncanHornby
MVP Notable Contributor
Jill,

Why don't you do away with all this painful validation and simply have a date time picker control on your VBA form? Far more user friendly too.


  1. When you are in form design mode open the toolbox up

  2. right click on toolbox and select additional controls

  3. Select the Microsoft Date time picker control 6 (sp4)

  4. drag the control onto your form


Duncan
0 Kudos