Do you have a UserForm set up already with an email field?
Or are you validating items entered into a worksheet?
If you have created a UserForm, then you simply need to add a button called cmdOK, and assuming that the text box is called txtEmail you could check for a correct entry in the email field by writing code in the cmdOK_Click event.
Validating an email is more difficult, but here is a simple routine which does a few spot checks
Private Sub cmdOK_Click()
If Len(txtEmail.Text) = 0 Then
' There is no text
GoTo InvalidEmail
ElseIf InStr(1, txtEmail.Text, "@") = 0 Then
' There is no @
GoTo InvalidEmail
ElseIf InStr(1, txtEmail.Text, "@") = 1 Or InStr(1, txtEmail.Text, "@") = Len(txtEmail.Text) Then
' The email starts or ends with an @
GoTo InvalidEmail
ElseIf InStr(1, txtEmail.Text, " ") <> 0 Then
' There is a space in the email
GoTo InvalidEmail
ElseIf InStr(InStr(1, txtEmail.Text, "@"), txtEmail.Text, ".") = 0 Then
' There is no full stop after the @
ElseIf Right(txtEmail.Text, 1) = "." Then
' Ends with a full stop
GoTo InvalidEmail
Else
' Email is OK - PUT YOUR CODE HERE
End If
Exit Sub
InvalidEmail:
MsgBox "You have entered an invalid email address!", vbCritical
End Sub
Main Topics
Browse All Topics





by: ColosseoPosted on 2004-08-26 at 12:53:11ID: 11906776
Hi wiggs
the code for your on click button could be
If input_Is_Complete Then
' Run your code above
End If
Then you would add a function like this to perform the check returning true if the email field has a value or false and an error message if it doesnt
Function input_Is_Complete()
input_Is_Complete = TRUE
If txtEmail.Value = "" Then
input_Is_Complete = FALSE
Msgbox "Please enter an email address!"
End If
End Function
It is also possible to add more than one field check into the function; check for different conditions and combine the error strings in to one big error of all problems on the form
Let me know if you would like help with that
Cheers
Scott