field validation (1 Viewer)

cyraxote

Registered User.
Local time
Today, 01:38
Joined
Sep 19, 2002
Messages
20
Hi.

Another simple question, I hope.

I have a field for email addresses. I want to validate the field so that any text placed in it must contain the @ sign.

So far, I have had no luck with variations on the InStr function.

On the other hand, I have no idea what I'm doing, so my lack of success is probably not a surprise.

Any help would be greatly appreciated.

Thanks.

Rodney
 

pono1

Registered User.
Local time
Yesterday, 22:38
Joined
Jun 23, 2002
Messages
1,186
Rodney,

Assuming you have a textbox named txtEmail on a form, you can put code similar to the following in the BeforeUpdate event of txtEmail.

Code:
Private Sub TxtEmail_BeforeUpdate(Cancel As Integer)

'create a variable in memory
Dim MyString As String

'fill the variable with data from
'the email textbox on the current form
'note to self: "Me" = the current form
MyString = Me.TxtEmail.Value

'if the textbox is null or has a @ then
If IsNull(MyString) Or InStr(MyString, "@") Then
    'that's fine -- so leave
    Exit Sub
End If

'otherwise, hold everything
' prompt the user
MsgBox "There's no '@' in the text box", vbOKOnly, "Invalid Address"

'now cancel and put the cursor back in the offending textbox
DoCmd.CancelEvent

End Sub
Regards,
Tim
 

cyraxote

Registered User.
Local time
Today, 01:38
Joined
Sep 19, 2002
Messages
20
Tim,

Thanks very much. I would have had no idea that any coding was involved. Perhaps it's time to take a class....
 

pono1

Registered User.
Local time
Yesterday, 22:38
Joined
Jun 23, 2002
Messages
1,186
C,

My mistake -- didn't quite grasp what you were looking for. I think you can do what you want without code -- using, instead, the validation rule for your textbox.

Again, assuming there is a textbox named TxtEmail on a form, you can experiment with something like this in the textbox's Validation Rule property.

InStr(1,[TxtEmail],"@")<>0

Regards,
Tim
 

cyraxote

Registered User.
Local time
Today, 01:38
Joined
Sep 19, 2002
Messages
20
Tim:

Thanks very much. That worked like charm. :)

Rodney
 

Users who are viewing this thread

Top Bottom