Results 1 to 7 of 7
  1. #1
    Jgk is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2014
    Posts
    46

    Need Help with SQL Statement used in Filter Macro

    SELECT IIf(IsNull([LastName]),IIf(IsNull([FirstName]),[Company],[FirstName]),IIf(IsNull([FirstName]),[LastName],[LastName] & ", " & [FirstName])) AS [File As], IIf(IsNull([LastName]),IIf(IsNull([FirstName]),[Company],[FirstName]),IIf(IsNull([FirstName]),[LastName],[FirstName] & " " & [LastName])) AS [Customer Name], Customers.*


    FROM Customers
    ORDER BY IIf(IsNull([LastName]),IIf(IsNull([FirstName]),[Company],[FirstName]),IIf(IsNull([FirstName]),[LastName],[LastName] & ", " & [FirstName])), IIf(IsNull([LastName]),IIf(IsNull([FirstName]),[Company],[FirstName]),IIf(IsNull([FirstName]),[LastName],[FirstName] & " " & [LastName]));



    The problem I have is it lists contact as Customer Name and I Need Customer Name to be Company.


    In my forms it is pulling:

    Customer Name File As ID Company LastName FirstName
    Dan Smith Smith,Dan 2 ABC Smith Dan
    JCF JCF 1 JCF

    The null statement is used in a macro filter needed in forms in embedded macro.

    Thanks for your assistance.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    There can be FirstName or LastName without the other? Consider:

    Nz(LastName & IIf(IsNull(FirstName), Null, IIf(IsNull(LastName), Null, ", ") & FirstName), Company) AS FileAs

    If you want Company to list as customer name, then why the IIf for CustomerName?
    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.

  3. #3
    Jgk is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2014
    Posts
    46
    You are correct I do not need Customer name, but the File as is critical due to dcounts and lookup fields in forms. I used the Desktop Project Management Access template and modified to add Customer table and Customer Extended Query. The Employee Extended Query in sample dbase has the query I am trying to modify the File As:
    I renamed that table (Contractors) and query (Contractors Extended). I made it work by not having first and last names just (Contractor) for Contractors table and Company for Customers table. I have hidden fields for first and last name entry on forms. It is sad but it works.

    I am having trouble putting your sql statement in Query.

    Thanks

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    What is issue with the suggested expression? It worked in my test.
    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.

  5. #5
    Jgk is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2014
    Posts
    46
    Thanks again,

    This is not the project just enough info to see my predicament. The queries for both customer and contractor extended are used in forms and need the Customer Name and Contractor Name to pull to forms correctly( not first and last name but field Company from Customer table and field Contractor from Contractor table. In my actual database I made work by removing first and last names from both Contractor and Customer Tables and Forms. However, the embedded macros are adding Customers and Contractors which I like and want to utilize.

    I just did not understand where your code had to go after If statement.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    What I suggest is an expression to create a field in query. It simply replaces the longer and unnecessarily complicated expression you are using.

    SELECT Customers.*, Nz(LastName & IIf(IsNull(FirstName), Null, IIf(IsNull(LastName), Null, ", ") & FirstName), Company) AS FileAs
    FROM ...
    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.

  7. #7
    Jgk is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2014
    Posts
    46

    Thumbs up solved

    Got It Thank you very much,
    SELECT Contractors.*, Nz([LastName] & IIf(IsNull([FirstName]),Null,IIf(IsNull([LastName]),Null,", ") & [FirstName]),[Contractor]) AS [Contractor Name]
    FROM Contractors;

    It worked had to change header fields than everything good .

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

Similar Threads

  1. Macro If statement syntax error
    By euphonium01 in forum Macros
    Replies: 5
    Last Post: 10-15-2013, 07:30 AM
  2. MACRO to filter
    By DAE60 in forum Programming
    Replies: 1
    Last Post: 11-13-2012, 02:10 PM
  3. IF Statement in a Macro
    By Elbows in forum Access
    Replies: 7
    Last Post: 10-11-2012, 12:46 PM
  4. How to issue a GRANT statement from a macro
    By xfp39024 in forum Access
    Replies: 2
    Last Post: 06-15-2012, 11:48 AM
  5. Query filter statement
    By Brian62 in forum Access
    Replies: 1
    Last Post: 02-16-2010, 02:34 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