• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

Calling a Function

Dear Experts,
Its early in the morning & I have a rush job one & am having a thick moment ...

I have a form which is used for entering subscribers details (name, email address etc) & I am putting in a piece of code to validate the email address first.  

I have copied a piece of code courtesy of EE for a function which checks the email address for common syntax errors.  This is a function called ValidEmail.

I need to get this function called as part of of the code contained in the button "Add" which is below. The check needs to run against the field "AddEmail"

Add Code:

Private Sub Add_Click()
' ********* Check Bit  *********
If Len(Me!AddEmail & "") = 0 Then
       MsgBox "You must add at least an Email Address", vbExclamation, "Warning !"
        Cancel = True
        Exit Sub
    End If
    
'  *****************Need to add the check here ******************

' ********* Save Bit  *********
Dim rs As dao.Recordset
Set rs = CurrentDb.OpenRecordset("WeeklyMembers")
rs.AddNew
rs!Email = Me.AddEmail
rs!Person = Me.Personname
rs!Firm = Me.Firm
rs!Role = Me.Role
rs![Subscriber Since] = Now()
rs.Update
rs.Close

If Len(Me!Personname & "") = 0 Then
MsgBox (Me.AddEmail & " has been added"), , "Success"
Else
MsgBox (Me.Personname & " has been added"), , "Success"
End If
Me.AddEmail = ""
Me.Personname = ""
Me.Firm = ""
Me.Role = ""
End Sub

Open in new window


The Function ValidEmail code is below:

Function ValidEmail(ByVal strCheck As String) As Boolean
'Created by Chad M. Kovac
'Tech Knowledgey, Inc.
'http://www.TechKnowledgeyInc.com

Dim bCK As Boolean
Dim strDomainType As String
Dim strDomainName As String
Const sInvalidChars As String = "!#$%^&*()=+{}[]|\;:'/?>,< "
Dim i As Integer

bCK = Not InStr(1, strCheck, Chr(34)) > 0 'Check to see if there is a double quote
If Not bCK Then GoTo ExitFunction

bCK = Not InStr(1, strCheck, "..") > 0 'Check to see if there are consecutive dots
If Not bCK Then GoTo ExitFunction

' Check for invalid characters.
If Len(strCheck) > Len(sInvalidChars) Then
    For i = 1 To Len(sInvalidChars)
        If InStr(strCheck, Mid(sInvalidChars, i, 1)) > 0 Then
            bCK = False
            GoTo ExitFunction
        End If
    Next
Else
    For i = 1 To Len(strCheck)
        If InStr(sInvalidChars, Mid(strCheck, i, 1)) > 0 Then
            bCK = False
            GoTo ExitFunction
        End If
    Next
End If

If InStr(1, strCheck, "@") > 1 Then 'Check for an @ symbol
    bCK = Len(Left(strCheck, InStr(1, strCheck, "@") - 1)) > 0
Else
    bCK = False
End If
If Not bCK Then GoTo ExitFunction

strCheck = Right(strCheck, Len(strCheck) - InStr(1, strCheck, "@"))
bCK = Not InStr(1, strCheck, "@") > 0 'Check to see if there are too many @'s
If Not bCK Then GoTo ExitFunction

strDomainType = Right(strCheck, Len(strCheck) - InStr(1, strCheck, "."))
bCK = Len(strDomainType) > 0 And InStr(1, strCheck, ".") < Len(strCheck)
If Not bCK Then GoTo ExitFunction

strCheck = Left(strCheck, Len(strCheck) - Len(strDomainType) - 1)
Do Until InStr(1, strCheck, ".") <= 1
    If Len(strCheck) >= InStr(1, strCheck, ".") Then
        strCheck = Left(strCheck, Len(strCheck) - (InStr(1, strCheck, ".") - 1))
    Else
        bCK = False
        GoTo ExitFunction
    End If
Loop
If strCheck = "." Or Len(strCheck) = 0 Then bCK = False

ExitFunction:
ValidEmail = bCK
End Function

Open in new window


Can Anybody Help?
0
correlate
Asked:
correlate
1 Solution
 
IrogSintaCommented:
Try this:

If Not ValidEmail(Me.AddEmail) Then
    MsgBox "You need to enter a valid email address.", vbExclamation, "Invalid Email"
    Cancel = True
    Exit Sub
EndIf

Open in new window

0
 
correlateAuthor Commented:
Brilliant Thank you & thank you for the speedy response
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now