Results 1 to 7 of 7

Multi Select Value(s) Generates Append Query

  1. #1
    skydivetom is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    278

    Multi Select Value(s) Generates Append Query

    Hello Experts:



    I need some assistance with modifying/validating some existing VBA code. Attached are two Access files (zipped format).

    1. "Multi Select -- Version 01 (Works)" -- as the file name indicates, this example file (found online) works fine. Ultimately, when opening "frmMultiSelect", highlighting one or many values from the listbox and then clicking "RunQuery" appends records to the table "tblCompaniesAppend".

    2. In the second file "Multi Select -- Version 02 (Does Not Work)", I have mimicked the process... unfortunately, it did not work.

    I used "find/replace" in Notepad to ensure consistency of naming convention. Yet, my VBA code (may based on including more fields) does NOT execute.

    Could anyone please assist me with solving the VBA code so that the 2nd file appends records to the appendTable based on selected "Category" values from the form's listbox?

    Thank you in advance,
    skydivetom
    Attached Files Attached Files

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    20,628
    In order to handle if the query doesn't exist:

    On Error Resume Next
    MyDB.QueryDefs.Delete "qryAppendSelectedCategories"
    On Error GoTo Err_cmdButtonSelectCategories_Click

    and you missed a bracket here:

    [000_tblRawData].CommandName, [000_tblRawData].UIC FROM
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    dashiellx's Avatar
    dashiellx is offline Falconer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Location
    Baltimore
    Posts
    38
    In addition to what Paul posted, you are also not constructing the where clause properly.

    It should be more along the lines of:

    Code:
    strIN = "("
    For i = 0 To ListBoxActions1.ListCount - 1
        If ListBoxActions1.Selected(i) Then
            If ListBoxActions1.Column(0, i) = "All" Then
                flgSelectAll = True
            End If
            strIN = strIN & "[Category] ='" & ListBoxActions1.Column(0, i) & "' OR "
        End If
    Next i
    strIN = Left(strIN, Len(strIN) - 4) & ")"
    strWhere = " WHERE " & strIN
    Also, why create a query object? Just run the SQL

    Code:
    CurrentDb.Execute strSQL, dbFailOnError

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    20,628
    Quote Originally Posted by dashiellx View Post
    In addition to what Paul posted, you are also not constructing the where clause properly.
    Works fine as is, once the bracket is added. You've used "OR", the OP used an IN() clause, both work. I agree with just executing the SQL. I was trying to show where/why the original code was failing.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    skydivetom is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    278
    Paul -- perfect!!! Thousand thanks for the prompt response... I truly appreciate your assistance.

  6. #6
    skydivetom is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    278
    dashiellx -- thank you for the additional comment. I will further review the code and make corrections. As of now, the VBA executes as envisioned.

  7. #7
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    20,628
    Quote Originally Posted by skydivetom View Post
    Paul -- perfect!!! Thousand thanks for the prompt response... I truly appreciate your assistance.
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 4
    Last Post: 06-09-2016, 06:00 AM
  2. Replies: 22
    Last Post: 12-29-2014, 11:24 AM
  3. Replies: 3
    Last Post: 01-24-2013, 02:38 PM
  4. Replies: 5
    Last Post: 08-23-2012, 09:33 AM
  5. Append Query - Multi-Valued Field
    By catat in forum Queries
    Replies: 0
    Last Post: 05-11-2010, 01:52 PM

Posting Permissions

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