Solved

Access VBA Issue - If ElseIf, Else - Compile Error

Posted on 2011-09-21
10
280 Views
Last Modified: 2012-08-13
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
Comment
Question by:alibah
  • 5
  • 3
  • 2
10 Comments
 

Author Comment

by:alibah
ID: 36574980
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
 
LVL 61

Expert Comment

by:mbizup
ID: 36574999
Since you're using VBA, not SQL, change all of your IS NOT NUlls to this syntax:

Not IsNUll ([FatherFirstName] )
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36575006
Or this:

IsNUll ([FatherFirstName] )  = FALSE
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36575052
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
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 36575125
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 61

Expert Comment

by:mbizup
ID: 36575130
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
 

Author Comment

by:alibah
ID: 36575391
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
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 36575447
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
 

Author Comment

by:alibah
ID: 36575554
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
 
LVL 61

Expert Comment

by:mbizup
ID: 36575580
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now