Solved

Access VBA Issue - If ElseIf, Else - Compile Error

Posted on 2011-09-21
10
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

749 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