Find Certain Text in Field for If Then Statement

  • Thread starter Thread starter Guest
  • Start date Start date
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!
 
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
 
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
 
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
 
Back
Top