Results 1 to 5 of 5
  1. #1
    sstiebinger is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Mar 2015
    Posts
    105

    Trouble with building query in VBA...

    I'm not sure if this should be in the query forum or the programming forum, so I'm sorry if I chose the wrong one.

    I'm having an issue trying to get a continuous subform query working in VBA...

    I have a form that lists my accounts and also has a text box and a button to search the accounts. The accounts are listed in a subform in continuous form mode.
    The continuous form data source is the following query, which works fine...

    Code:
    SELECT tblAccounts_Vendors.AccountID, tblAccounts_Vendors.RecordType, tblAccounts_Vendors.AccountName, tblAccounts_Vendors.AccountsCustom1, tblAccounts_Vendors.AccountsCustom2, tblAccounts_Vendors.ShipTo1, tblAccounts_Vendors.ShipTo3, tblAccounts_Vendors.ShipTo4, tblAccounts_Vendors.ShipTo5, tblAccounts_Vendors.Rep, tblAccounts_Vendors.ContactFName, tblAccounts_Vendors.MI, tblAccounts_Vendors.ContactLName, ([ContactLName] + ", ") & [ContactFName] & (", " + [MI]) AS cName FROM tblAccounts_Vendors WHERE (((tblAccounts_Vendors.RecordType)="Account")) ORDER BY tblAccounts_Vendors.AccountName;
    This displays my entire accounts list, as expected.

    In the main form is an unbound text box named txtSearchAccount and a button named cmdSearchAccounts.


    In the On Click event for the button I have the following code:

    Code:
    On Error Resume Next    Application.Echo False
        Dim strSQL_Accounts As String
        If IsNull(Me.txtSearchAccount) Then
            strSQL_Accounts = "SELECT tblAccounts_Vendors.AccountID, tblAccounts_Vendors.RecordType, tblAccounts_Vendors.AccountName, " & _
                "tblAccounts_Vendors.AccountsCustom1, tblAccounts_Vendors.AccountsCustom2, tblAccounts_Vendors.ShipTo1, tblAccounts_Vendors.ShipTo3, " & _
                "tblAccounts_Vendors.ShipTo4, tblAccounts_Vendors.ShipTo5, tblAccounts_Vendors.Rep, tblAccounts_Vendors.ContactFName, tblAccounts_Vendors.MI, " & _
                "tblAccounts_Vendors.ContactLName, ([ContactLName] + "", "") & [ContactFName] & ("", "" + [MI]) AS cName FROM tblAccounts_Vendors WHERE " & _
                "(((tblAccounts_Vendors.RecordType)=""Account"")) ORDER BY tblAccounts_Vendors.AccountName;"
        Debug.Print strSQL_Accounts
        Else
            strSQL_Accounts = "SELECT tblAccounts_Vendors.AccountID, tblAccounts_Vendors.RecordType, tblAccounts_Vendors.AccountName, " & _
                "tblAccounts_Vendors.AccountsCustom1, tblAccounts_Vendors.AccountsCustom2, tblAccounts_Vendors.ShipTo1, tblAccounts_Vendors.ShipTo3, " & _
                "tblAccounts_Vendors.ShipTo4, tblAccounts_Vendors.ShipTo5, tblAccounts_Vendors.Rep, tblAccounts_Vendors.ContactFName, tblAccounts_Vendors.MI, " & _
                "tblAccounts_Vendors.ContactLName, ([ContactLName] + "", "") & [ContactFName] & ("", "" + [MI]) AS cName FROM tblAccounts_Vendors WHERE " & _
                "(((tblAccounts_Vendors.RecordType)=""Account"")) AND [AccountName] LIKE ""*" & Me.txtSearchAccount & "*"" OR [ShipTo1] LIKE ""*" & Me.txtSearchAccount & _
                "*"" OR [cName] LIKE ""*" & Me.txtSearchAccount & "*"" ORDER BY tblAccounts_Vendors.AccountName;"
        End If
        Debug.Print strSQL_Accounts
        Me.sfrmAccountList.Form.RecordSource = strSQL
        Me.sfrmAccountList.Requery
        Application.Echo True
    End Sub
    When I click the search button... the form fails... and I can't figure out what I'm doing wrong... I'm sure it's probably obvious but I'm really not that familiar with queries in VBA...

    When I put some text into the search box and hit the button, this is what comes out in the immediate window for the debug.print command:

    Code:
    SELECT tblAccounts_Vendors.AccountID, tblAccounts_Vendors.RecordType, tblAccounts_Vendors.AccountName, tblAccounts_Vendors.AccountsCustom1, tblAccounts_Vendors.AccountsCustom2, tblAccounts_Vendors.ShipTo1, tblAccounts_Vendors.ShipTo3, tblAccounts_Vendors.ShipTo4, tblAccounts_Vendors.ShipTo5, tblAccounts_Vendors.Rep, tblAccounts_Vendors.ContactFName, tblAccounts_Vendors.MI, tblAccounts_Vendors.ContactLName, ([ContactLName] + ", ") & [ContactFName] & (", " + [MI]) AS cName FROM tblAccounts_Vendors WHERE (((tblAccounts_Vendors.RecordType)="Account")) AND [AccountName] LIKE "*seth*" OR [ShipTo1] LIKE "*seth*" OR [cName] LIKE "*seth*" ORDER BY tblAccounts_Vendors.AccountName;
    Thank you in advance for taking the time to look at my problem.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    I would use apostrophes instead of doubled up quote marks.

    strSQL_Accounts = "SELECT tblAccounts_Vendors.*, [ContactLName] + ', ' & [ContactFName] & ', ' + [MI] AS cName FROM tblAccounts_Vendors WHERE " & _
    "[RecordType]='Account' AND [AccountName] LIKE '* & Me.txtSearchAccount & *' OR [ShipTo1] LIKE '*" & Me.txtSearchAccount & _
    "*' OR [cName] LIKE '* & Me.txtSearchAccount & *' ORDER BY AccountName;"

    Be careful mixing AND and OR operators. Parens are critical. Do you want:

    (RecordType AND AccountName) OR ShipTo1 Or cName

    or

    RecordType AND (AccountName OR ShipTo1) Or cName

    or

    (RecordType AND (AccountName OR ShipTo1)) Or cName

    or

    RecordType AND (AccountName OR ShipTo1 Or cName)

    Is the main form UNBOUND?

    Why change the subform RecordSource? Could just build the WHERE clause and set the Filter and FilterOn properties.
    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
    sstiebinger is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Mar 2015
    Posts
    105
    I'm new to Access so I copied this code from another project and modified it to my needs... I'm not familiar with how the Filters work, so I'll look into that if that's easier...
    The main form is Unbound, yes.
    I'm looking for: RecordType AND (AccountName OR ShipTo1 OR cName)

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    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
    sstiebinger is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Mar 2015
    Posts
    105
    That looks like exactly what I need. Thanks for the link!

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

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2014, 10:08 AM
  2. Replies: 6
    Last Post: 06-25-2014, 12:40 PM
  3. Replies: 7
    Last Post: 08-30-2013, 03:43 PM
  4. Replies: 1
    Last Post: 08-29-2013, 06:14 AM
  5. Building a Query that can extract data and sum
    By Sara Bellum in forum Queries
    Replies: 5
    Last Post: 03-15-2013, 08:57 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