Find Certain Text in Field for If Then Statement

G

Guest

I have Access 2003. I have a field named "Contents" in a table named
"Ticket". On one of my forms, I have a command button named
"cmdDocumentTicket" and when clicked, it'll add text to the "Contents" field
by pulling information from other fields in the table. Part of what it adds
are the words "Original Message From".

I need to write VBA code on the Exit command button so that when a user
clicks the Exit Button, it'll confirm the "Contents" field contains the text
"Original Message From" somewhere in the field. If it doesn't, I want it to
prompt them with a message.

Below is code I've started which won't work because I need help on it.

'Check to make sure ticket has been documented
If Me.Contents <> "No Solution Yet" Then
strMsg = strMsg & "Do not forget to document ticket"
MsgBox strMsg, vbExclamation, "Invalid Data"

Me.cmdDocumentTicket.SetFocus
End If

Thank you so much in advance!
 
G

Guest

To find if a string exists anywhere is another string, use the Instr() function

If Instr(Me.Contents, "No Solution Yet") = 0 Then
strMsg = strMsg & "Do not forget to document ticket"
MsgBox strMsg, vbExclamation, "Invalid Data"
Me.cmdDocumentTicket.SetFocus
End If
 
D

Dave Miller

I would put this code in the form_unload event, rather than the
commandbutton.

Private Sub Form_Unload(Cancel As Integer)
dim strMsg as string
With Me
If Not (InStr(.Contents, "Original Message From") > 0) Then
strMsg = strMsg & "Do not forget to document ticket"
MsgBox strMsg, vbExclamation, "Invalid Data"
.cmdDocumentTicket.SetFocus
Cancel = True
End If
End With
End Sub

David Miller
 
G

Guest

This worked perfectly. Thank you so much!

Klatuu said:
To find if a string exists anywhere is another string, use the Instr() function

If Instr(Me.Contents, "No Solution Yet") = 0 Then
strMsg = strMsg & "Do not forget to document ticket"
MsgBox strMsg, vbExclamation, "Invalid Data"
Me.cmdDocumentTicket.SetFocus
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top