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
Solved

Access VBA Issue - If ElseIf, Else - Compile Error

Posted on 2011-09-21
10
282 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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