Results 1 to 13 of 13
  1. #1
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591

    Unhappy Function won't run in a Query


    I've written a short function to concatenate a full name from its parts. I did it mostly to practice writing functions, but thought it would come in handy. I stuck it in a new module. Here it is.

    Code:
     
    Public Function ApplicantName(FName As String, MI As String, LName As String)
    
        ApplicantName = FName & IIf(MI = "", "", " " & MI & ".") & " " & LName
     
    End Function
    When I call this from a form to populate a text box, it works just fine. When I put it in the query I get a Data Type Mismatch in criteria expression. Here's the SQL.

    Code:
    SELECT SSN, Applicant.Appl_Fname, Appl_MI, Applicant.Appl_Lname, ApplicantName([appl_FName],[appl_MI],[appl_LName]) AS Full_Name
    
    FROM Applicant
    
    GROUP BY SSN, Appl_Fname, Appl_MI, Appl_Lname, ApplicantName([appl_FName],[appl_MI],[appl_LName]);
    This is a RowSource for a combo box.


    Appl_Name: ApplicantName([appl_fname],[appl_mi],[appl_LName])

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    You dont use IIF in code. its for queries.
    use
    IF...THEN
    ELSE
    ENDIF

  3. #3
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591
    So here is my revised code which works exactly the same as my old code. Works fine from the form. Does not work in the query.

    Code:
    Public Function ApplicantName(FName As String, MI As String, LName As String)    
                
        If MI = "" Then
            MI = " "
        Else
            MI = " " & MI & "." & " "
        End If
        
        ApplicantName = FName & MI & LName 
     
    End Function

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Maybe
    GROUP BY SSN, Appl_Fname, Appl_MI, Appl_Lname, Full_Name

    or
    GROUP BY SSN, Appl_Fname, Appl_MI, Appl_Lname, ApplicantName([appl_FName],[appl_MI],[appl_LName]) AS Full_Name

  5. #5
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591
    This as well, I took the GROUP BY out of my query and it runs, but returns an error on every line that has no middle initial (MI). Again, this does not happen in the form where it seems to work perfectly. I'm not sure why it would work in one place, but not the other.

  6. #6
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591
    FYI: This produces the same results. Fine in the form, errors in the query.

    Code:
    Public Function ApplicantName(FName As String, MI As String, LName As String)
        If MI = "" Or IsNull(MI) Then
            MI = " "
        Else
            MI = " " & MI & "." & " "
        End If
        
        ApplicantName = FName & MI & LName
      
    End Function

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I see. The error is happening because you are passing null to a variable that cannot handle nulls.

    Try
    Public Function ApplicantName(FName As String, MI As Variant, LName As String)

    Or make middle name optional. With the optional parameter you should be able to employ the Nz() Function on the MI field within your query.
    Public Function ApplicantName(FName As String, LName As String, Optional MI As String = "")

  8. #8
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591
    That did it. To account for all possibilities I made all three variables Variants and the query seems to work in all cases. I'm still confused why the function did not produce an error when called by the form. This is a very old database I am upgrading and I'm afraid there really are ghosts in this machine.

    Thanks for hanging in,

    Paul

    p.s. I'm going to leave this open briefly in case there are any other comments, but I'll come back later and mark it solved.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Good question why the textbox expression works. Are you are pulling values from columns of combobox? Columns of combobox are always text, not actually numbers or dates or Null. So if there is nothing there, it is an empty string.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591
    From the form I am calling the function from AfterUpdate event of a combo box, reading those columns and populating a text box. I'm more than a little confused about the difference between nulls and empty strings and how to tell the difference. They both seem like a whole lot of nothing.

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591
    I will read that. I just finished reading this which makes short work of explaining Nulls, Empty Strings and why Variants work with both.

    http://www.everythingaccess.com/tuto...C-Zero-and-ZLS

    Thanks for your help.

    Paul

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Both links essentially the same info by Allen Browne.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. VBA function query
    By Pacific1 in forum Programming
    Replies: 1
    Last Post: 12-04-2014, 01:19 PM
  2. What is the AutoNumber function in a Query?
    By supracharger in forum Queries
    Replies: 1
    Last Post: 06-01-2012, 10:07 AM
  3. Count Function in query
    By tjstromquist in forum Queries
    Replies: 1
    Last Post: 04-03-2012, 10:40 AM
  4. undefined function in query
    By mejia.j88 in forum Programming
    Replies: 8
    Last Post: 02-07-2012, 03:50 PM
  5. Replies: 4
    Last Post: 11-19-2010, 07:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums