Results 1 to 5 of 5
  1. #1
    tagteam is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    501

    Add second where condition

    I am using this vba code to open a form with a where condition



    DoCmd.OpenForm FormName:="frmInvoices", WhereCondition:="[InvoiceNumber]='" & Me.InvoiceNumber & "'"

    I would like to add an additional where condition but cant get it to work, I have tried

    DoCmd.OpenForm FormName:="frmInvoices", WhereCondition:="[InvoiceNumber]='" & Me.InvoiceNumber & "'", WhereCondition:="[CarNumber]='" & Me.CarNumber & "'"

    However, i get the error: Named Argument already specified

    I also tried
    DoCmd.OpenForm FormName:="frmInvoices", WhereCondition:="[InvoiceNumber]='" & Me.InvoiceNumber & "'" AND WhereCondition:="[CarNumber]='" & Me.CarNumber & "'"

    But that one gives me a syntax errror

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Try this:
    DoCmd.OpenForm FormName:="frmInvoices", WhereCondition:="[InvoiceNumber]='" & Me.InvoiceNumber & "'" & " AND [CarNumber]='" & Me.CarNumber & "'"

    Note: You only have 1 WHERE statement, but it can have several criteria

    eg: WHERE fld1 = "val1" AND fld2 = "Val2" AND fldx = "ValX"

  3. #3
    tagteam is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    501
    The solution listed by orange gave me a Data Type mismatch in criteria expression (runtime error 3464) Because i had field in there that was a number type field and i did not specify that. I will figure out how to set the number type field and retry the code from orange.

    However, I was able to get it to work with this code

    Code:
    Dim strDocName As String
    Dim strLinkCriteria As String
    Dim strLinkCriteria1 As String
    Dim strLinkCriteria2 As String
    
    
    strDocName = "frmInvoices"
    
    
    strLinkCriteria1 = "[InvoiceNumber]=" & "'" & Me.[InvoiceNumber] & "'"
    strLinkCriteria2 = "[CarNumber]=" & Me.[CarNumber]
    strLinkCriteria = strLinkCriteria1 & " AND " & strLinkCriteria2
    'Debug.Print "Resolved String = " & strLinkCriteria
    DoCmd.OpenForm strDocName, , , strLinkCriteria
    The debug is just in there for when i was testing
    Last edited by tagteam; 03-22-2016 at 03:47 PM.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Using Debug statements is a great testing technique, and you can always comment them after testing.

    So Car Number was really a number data type, not a text/string as per your earlier post
    Code:
    ...WhereCondition:="[CarNumber]='" & Me.CarNumber & "'"

  5. #5
    tagteam is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    501
    Yes, that is correct. The carnumber field was a number field.

    Is there a different way to have written it if it was a number field instead of a text field. I would like to try your method as well to see if I can get it working. It is a little easier and cleaner.
    Thanks

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

Similar Threads

  1. Condition issues
    By fspata in forum Forms
    Replies: 52
    Last Post: 11-13-2015, 12:26 PM
  2. Where Condition
    By NISMOJim in forum Programming
    Replies: 4
    Last Post: 04-09-2014, 11:10 PM
  3. Report and IF Condition
    By ismailkhannasar in forum Queries
    Replies: 1
    Last Post: 01-31-2013, 04:29 AM
  4. Help with using the AND condition
    By ssturges in forum Access
    Replies: 1
    Last Post: 11-25-2012, 12:36 AM
  5. How to use IIF condition
    By nshaikh in forum Queries
    Replies: 4
    Last Post: 09-12-2008, 01:23 AM

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