Simple Question with IF statement.

SD23

Registered User.
Local time
Today, 05:52
Joined
Jun 13, 2006
Messages
60
I have a text field and the user enters is supposed to enter a name in the format Last Name, First Name. However if the user does not enter with the correct format, then I want an error message to pop up. Basically if the user does not enter a comma (,) in their input, then I want an error mesage to display. I figured this would be the easiest way. I m not sure how to implement this.

If Not Me.Invent1="," then
msgbox "error"
End if

This does not work. Any ideas on how I could correctly do this. I was going to put this in my exit event.

User input: doe joe
There is no comma, so error message pops up
User Input: doe, joe
No error message
 
the problem is that when you are doing that you are testing for an absolute.

the only time that if statement would come back true would be if the text box had only a comma in it (i.e. ",")

if order to test if there is a comma in that text box, do this instead.

If InStr(Me.Invent1,",") > 0 Then
msgbox "Error"
End If

InStr is the in string function, it searches the first field for the second field, and then returns the character position where it is found. InStr returns 0 if the string is not found, so this if statement says if the position of "," is anywhere greater then 0, display the error (if its found).
 
I inserted the code, but it gives me an error message whether I put a comma in the text field or if i leave it out. Not sure what the problem is.
 
im not sure whats wrong, its a pretty straight forward thing..

maybe you should use Invent1.Value instead of me.invent1?

do me a favor, break, or use the immediate window, to findout what it is returning for Me.Invent1 when it shouldn't be giving the error and yet it does.
 
elb,

Code:
If InStr(1, Me.Invent1,",") = 0 Then 
   MsgBox(...)
End If

Wayne
 
oh woops, yea, change the > to =, i was thinking you didn't want a comma, but if you want to make sure there is a comma, then you need to say that if there isn't a comma, display the error

Code:
If InStr(Me.Invent1,",") = 0 Then
    msgbox "Please use the format Last, First"
    Exit Sub
End If
 
Code:
If InStr([SIZE="3"][B]1[/B][/SIZE], Me.Invent1,",") = 0 Then 
   MsgBox(...)
End If

Wayne
 
the start position is optional, meaning not required.
 

Users who are viewing this thread

Back
Top Bottom