Solved

Access VBA Issue - If ElseIf, Else - Compile Error

Posted on 2011-09-21
10
285 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 48

Expert Comment

by:Dale Fye
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 48

Accepted Solution

by:
Dale Fye 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
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…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

617 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