Code-Validation Question

Lamb2087

Registered User.
Local time
Today, 19:22
Joined
Feb 7, 2003
Messages
103
I use this code to validate for numbers in a text box:

Private Sub txtCS_NUM_BeforeUpdate(Cancel As Integer)
If Not IsNumeric(Me![txtCS_NUM]) Then
MsgBox "The data must be numbers only"
Cancel = True
Me![txtCS_NUM].Undo
End If

End Sub

How can I change it to validate for text??

and is there a way to validate for all the text fields and all the numeric fields??
 
How did you intend to validate for text? Every character you could enter is text. Did you mean, only letters of the alphabet apply?

You would have to write a public function to do that, keep it in a general module. But you could call it with a string and use the answer in queries or such.

Code:
Public Function IsItAlpha( stInput as String ) as Boolean

Dim boAnswer as Boolean
Dim stChar as String
Dim loI as Long
Dim loJ as Long

boAnswer = True

loJ = Len( stInput )

For loI = 1 to loJ
    stChar = Mid$( stInput, loI, 1 )
    Select Case stChar
        Case "A" to "Z"
        Case "a" to "z"
        Case Else
            boAnswer = False
            Exit For
    End Select
Next loI

IsItAlpha = boAnswer

End Function

Note that embedded characters such as blanks and tabs will fail this test.
 
Validation for Letters

How could I use this:

Public Function IsItAlpha( stInput as String ) as Boolean

Dim boAnswer as Boolean
Dim stChar as String
Dim loI as Long
Dim loJ as Long

boAnswer = True

loJ = Len( stInput )

For loI = 1 to loJ
stChar = Mid$( stInput, loI, 1 )
Select Case stChar
Case "A" to "Z"
Case "a" to "z"
Case Else
boAnswer = False
Exit For
End Select
Next loI

IsItAlpha = boAnswer

End Function

For each text box where I want just letters? Can I use it in each BeforeUpdate Event of the text box I am validating for?
 
On the BeforeUpdate() all you would have to do is:

Code:
If IsItAlpha(Me.txtYourTextbox) = False Then
   MsgBox "The data you have entered is invalid.", vbExclamation, "Your App Name"
   Cancel = True
   Me.txtYourTextbox.Undo
End If
 
Last edited:

Users who are viewing this thread

Back
Top Bottom