Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

Access VBA Issue - If ElseIf, Else - Compile Error

I am trying to create a custom function in Access. My VBA skils are almost non-existent, but I was hoping to fake my way through it. I am passing the following fields/field types

Salutation - Text
LastName - Text
FatherFirstName - Text
Suffix - Text
MotherFirstName - Text
OtherNameCheck - Yes/No
OtherName - Text
ReverseFather - Yes/No

I initially tried passing FatherFirstName and MotherFirstName as Strings but was getting data type mismatch errors so I changed them to Variants. I also was passing the Yes/No fields as Boolean but that didn't work either.

My code is attached. I'm getting a Compile Error: Syntax Error. When I hover over the yellow debugger line when using the Immediate Window to test, it shows [MotherFirstName] = "Jane". I assumed that meant it was getting stuck at the OtherNameCheck which is why I changed passing those Yes/No fields from Boolean to String, but that isn't working either.

I'm out of ideas and I can't stare at it anymore hoping something will jump out at me. Any assistance will be VERY much appreciated.
Public Function StandardName(Salutation As String, LastName As String, FatherFirstName As Variant, Suffix As String, MotherFirstName As Variant, OtherNameCheck As String, OtherName As String, ReverseFather As String)


If [OtherNameCheck] = "True" And [ReverseFather] = "True" And [FatherFirstName] Is Not Null And [MotherFirstName] Is Not Null Then StandardName = "Mr. " & [FatherFirstName] & " & " & "Ms. " & [OtherName]
ElseIf [OtherNameCheck] = "True" And [FatherFirstName] Is Not Null And [MotherFirstName] Is Not Null Then StandardName = "Mr. " & [LastName] & " & Ms. " & [OtherName] _
ElseIf [ReverseFather] = "True" And [FatherFirstName] Is Not Null And [MotherFirstName] Is Not Null Then StandardName = "Mr. " & [FatherFirstName] & " & Mrs. " & [LastName] _
ElseIf [FatherFirstName] Is Not Null And [MotherFirstName] Is Not Null Then StandardName = [Salutation] & " " & [LastName] _
ElseIf [FatherFirstName] Is Not Null And [MotherFirstName] Is Null And [ReverseFather] = "True" Then StandardName = [Salutation] & " " & [FatherFirstName] _
ElseIf [FatherFirstName] Is Not Null And [MotherFirstName] Is Null Then StandardName = [Salutation] & " " & [LastName] & " " & [Suffix] _
ElseIf [MotherFirstName] Is Not Null And [OtherNameCheck] = "True" And [FatherFirstName] Is Null Then StandardName = [Salutation] & " " & [OtherName] _
ElseIf [MotherFirstName] Is Not Null And [FatherFirstName] Is Null Then StandardName = [Salutation] & " " & [LastName] _
Else StandardName = [LastName]
End If

End Function

Open in new window

0
alibah
Asked:
alibah
  • 5
  • 3
  • 2
1 Solution
 
alibahAuthor Commented:
Just noticed that in the code I posted, I was missing the underscore at the end of my first line. I added it and it still doesn't work.
0
 
mbizupCommented:
Since you're using VBA, not SQL, change all of your IS NOT NUlls to this syntax:

Not IsNUll ([FatherFirstName] )
0
 
mbizupCommented:
Or this:

IsNUll ([FatherFirstName] )  = FALSE
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
mbizupCommented:
Actually - that might not be necessary.

Try this (I've removed the line continuation characters _):

If [OtherNameCheck] = "True" And [ReverseFather] = "True" And [FatherFirstName] Is Not Null And [MotherFirstName] Is Not Null Then StandardName = "Mr. " & [FatherFirstName] & " & " & "Ms. " & [OtherName]
ElseIf [OtherNameCheck] = "True" And IsNull([FatherFirstName]) = False Is Not Null Then StandardName = "Mr. " & [LastName] & " & Ms. " & [OtherName]
ElseIf [ReverseFather] = "True" And [FatherFirstName] Is Not Null And [MotherFirstName] Is Not Null Then StandardName = "Mr. " & [FatherFirstName] & " & Mrs. " & [LastName]
ElseIf [FatherFirstName] Is Not Null And [MotherFirstName] Is Not Null Then StandardName = [Salutation] & " " & [LastName]
ElseIf [FatherFirstName] Is Not Null And [MotherFirstName] Is Null And [ReverseFather] = "True" Then StandardName = [Salutation] & " " & [FatherFirstName]
ElseIf [FatherFirstName] Is Not Null And [MotherFirstName] Is Null Then StandardName = [Salutation] & " " & [LastName] & " " & [Suffix]
ElseIf [MotherFirstName] Is Not Null And [OtherNameCheck] = "True" And [FatherFirstName] Is Null Then StandardName = [Salutation] & " " & [OtherName]
ElseIf [MotherFirstName] Is Not Null And [FatherFirstName] Is Null Then StandardName = [Salutation] & " " & [LastName]
Else: StandardName = [LastName]
End If
0
 
Dale FyeCommented:
Try this.

I've removed the brackets around all of your variable names and changed the Yes/No values back to boolean.  I've also changed all of the "IS NOT NULL" and "IS NULL" references with the IsNull() function.

I have not looked at your logic, but you might want to consider setting up a matrix with the various tests you have to make sure you have all of your cases covered.
Public Function StandardName(Salutation As String, LastName As String, FatherFirstName As Variant, _
                             Suffix As String, MotherFirstName As Variant, _
                             OtherNameCheck As String, OtherName As Boolean, ReverseFather As Boolean) As String

    If OtherNameCheck And ReverseFather And IsNull(FatherFirstName) = False And IsNull(MotherFirstName) = False Then
        StandardName = "Mr. " & FatherFirstName & " & " & "Ms. " & OtherName
    ElseIf OtherNameCheck And IsNull(FatherFirstName) = False And IsNull(MotherFirstName) = False Then
        StandardName = "Mr. " & LastName & " & Ms. " & OtherName
    ElseIf ReverseFather And IsNull(FatherFirstName) = False And IsNull(MotherFirstName) = False Then
        StandardName = "Mr. " & FatherFirstName & " & Mrs. " & LastName
    ElseIf IsNull(FatherFirstName) = False And IsNull(MotherFirstName) = False Then
        StandardName = Salutation & " " & LastName
    ElseIf IsNull(FatherFirstName) = False And IsNull(MotherFirstName) = True And ReverseFather Then
        StandardName = Salutation & " " & FatherFirstName
    ElseIf IsNull(FatherFirstName) = False And IsNull(MotherFirstName) = True Then
        StandardName = Salutation & " " & LastName & " " & Suffix
    ElseIf IsNull(MotherFirstName) = False And OtherNameCheck And IsNull(FatherFirstName) = True Then
        StandardName = Salutation & " " & OtherName
    ElseIf IsNull(MotherFirstName) = False And IsNull(FatherFirstName) = True Then
        StandardName = Salutation & " " & LastName
    Else
        StandardName = LastName
    End If
    
End Function

Open in new window

0
 
mbizupCommented:
If you're using Objects the above *might* work (I'm not positive).

If they are regular variables, use IsNull() instead of IS NULL like this:

If [OtherNameCheck] = "True" And [ReverseFather] = "True" And IsNull([FatherFirstName]) = False And IsNull([MotherFirstName]) = False Then StandardName = "Mr. " & [FatherFirstName] & " & " & "Ms. " & [OtherName]
ElseIf [OtherNameCheck] = "True" And IsNull([FatherFirstName]) = False And IsNull([MotherFirstName]) = False Then StandardName = "Mr. " & [LastName] & " & Ms. " & [OtherName]
ElseIf [ReverseFather] = "True" And IsNull([FatherFirstName]) = False And [MotherFirstName] Is Not Null Then StandardName = "Mr. " & [FatherFirstName] & " & Mrs. " & [LastName]
ElseIf IsNull([FatherFirstName]) = False And IsNull([MotherFirstName]) = False Then StandardName = [Salutation] & " " & [LastName]
ElseIf IsNull([FatherFirstName]) = False And IsNull([MotherFirstName]) = True And [ReverseFather] = "True" Then StandardName = [Salutation] & " " & [FatherFirstName]
ElseIf IsNull([FatherFirstName]) = False And IsNull([MotherFirstName]) = True Then StandardName = [Salutation] & " " & [LastName] & " " & [Suffix]
ElseIf IsNull([MotherFirstName]) = False And [OtherNameCheck] = "True" And IsNull([FatherFirstName]) = True Then StandardName = [Salutation] & " " & [OtherName]
ElseIf IsNull([MotherFirstName]) = False And IsNull([FatherFirstName]) = True Then StandardName = [Salutation] & " " & [LastName]
Else: StandardName = [LastName]
End If

Open in new window

0
 
alibahAuthor Commented:
Thanks to both of you for your quick responses.

fyed,

I had to make one minor tweak to your code, but then it worked correctly in the Immediate Window. When I try to call the function from a query and pass the parameters though I'm getting "Data type mismatch in criteria expression."

Here's the code I'm currently using. I feel like it's probably the Yes/No fields goofing it up, but I don't know what else to try.

 
Public Function StandardName(Salutation As String, LastName As String, FatherFirstName As Variant, _
                             Suffix As String, MotherFirstName As Variant, _
                             OtherNameCheck As Boolean, OtherName As String, ReverseFather As Boolean) As String

    If OtherNameCheck And ReverseFather And IsNull(FatherFirstName) = False And IsNull(MotherFirstName) = False Then
        StandardName = "Mr. " & FatherFirstName & " & " & "Ms. " & OtherName
    ElseIf OtherNameCheck And IsNull(FatherFirstName) = False And IsNull(MotherFirstName) = False Then
        StandardName = "Mr. " & LastName & " & Ms. " & OtherName
    ElseIf ReverseFather And IsNull(FatherFirstName) = False And IsNull(MotherFirstName) = False Then
        StandardName = "Mr. " & FatherFirstName & " & Mrs. " & LastName
    ElseIf IsNull(FatherFirstName) = False And IsNull(MotherFirstName) = False Then
        StandardName = Salutation & " " & LastName
    ElseIf IsNull(FatherFirstName) = False And IsNull(MotherFirstName) = True And ReverseFather Then
        StandardName = Salutation & " " & FatherFirstName
    ElseIf IsNull(FatherFirstName) = False And IsNull(MotherFirstName) = True Then
        StandardName = Salutation & " " & LastName & " " & Suffix
    ElseIf IsNull(MotherFirstName) = False And OtherNameCheck And IsNull(FatherFirstName) = True Then
        StandardName = Salutation & " " & OtherName
    ElseIf IsNull(MotherFirstName) = False And IsNull(FatherFirstName) = True Then
        StandardName = Salutation & " " & LastName
    Else
        StandardName = LastName
    End If
    
End Function

Open in new window

0
 
Dale FyeCommented:
What did you "tweak"

I would change all of the datatypes to variant for any field that could possibly contain a NULL value in the table you are using; I meant to do that.

0
 
alibahAuthor Commented:
fyed,

My tweaks were minor. Your code had OtherNameCheck as String and OtherName as Boolean. They were flip-flopped. Also, MotherFirstName was as String and I changed it to Variant. That got it working in the Immediate Window.

Now, on your advice, I changed all but the Boolean fields to Variant and it is WORKING in my query as well!! I can't thank you enough.

Have a great day.
0
 
mbizupCommented:
How are you using the function results?  Your error implies you are using it in a query.   If you are comparing the output to a text field be sure to enclose it in quotes.

The syntax would be something like this

Chr(34) & yourfunctioncall & Chr(34)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now