Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick question on how to validate contents of a text box 2

Status
Not open for further replies.

ftpdoo

Programmer
Aug 9, 2001
202
GB
Hi,

I have a text box called "txtText" which contains string data. (ie Bla Bla Bla)

I want to validate the contents of this text box to ensure that it either starts with:

"INSERT INTO"
"DELETE FROM"
"UPDATE "

Followed by anything else.. (ie INSERT INTO bla bla bla)

If the txtText contains any other data then display a Message box..

Any idea's??
Thnx,
Jonathan
 
Hi!

You can write a function like this:

Private Function ValidateTextBox(strText As String) As Boolean

Dim lngSpacePosition As Long
Dim strTestString As String
Dim strTestWord As String

strTestString = Trim(strText)
lngSpacePosition = InStr(strTestString, " ")
strTestWord = Left(strTestString, lngSpacePosition - 1)

Select Case strTestWord
Case "Update"
ValidateTextBox = True
Case "Insert"
strTestString = Mid(strTestString, lngSpacePosition +1)
lngSpacePosition = InStr(strTestString, " ")
strTestWord = Left(strTestString, lngSpacePosition - 1)
If strTestWord = "Into" Then
ValidateTextBox = True
Else
ValidateTextBox = False
End If
Case "Delete"
strTestString = Mid(strTestString, lngSpacePosition +1)
lngSpacePosition = InStr(strTestString, " ")
strTestWord = Left(strTestString, lngSpacePosition - 1)
If strTestWord = "From" Then
ValidateTextBox = True
Else
ValidateTextBox = False
End If
Case Else
ValidateTextBox = False
End Select

In the Before Update event of the text box type this code

If ValidateTextBox = False Then
Call MsgBox("Your query must start with Insert Into " & _
"Update or Delete From")
Cancel = -1
End If

hth
Jeff Bridgham
 
You could also use the Like method:
Code:
If txtTest.Text Like "INSERT INTO*" Or _
   txtTest.Text Like "DELETE FROM*" Or _
   txtTest.Text Like "UPDATE*" Then
    'FOUND A MATCH
Else
    'DID NOT MATCH
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top