Results 1 to 14 of 14
  1. #1
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130

    Typing SQL statements in VBA

    Hello, I am trying to cascade some comboboxes for some forms, but I need to enter a string in, which is SQL, but it keeps coming up with a syntax error. I am just wondering what exactly I am doing wrong for this statement to keep getting flagged. Also, is there anyway to run a command on more than one line in Access vba?

    Here is my code:


    Code:
    Private Sub CustomerNamecbo_AfterUpdate()    Dim strSource As String
        strSource = "SELECT tblPartInfo.PartNumber, tblPartInfo.PartNumber " & _
                       "FROM tblPartInfo " & _
                       "WHERE CustomerName = '" & Me.CustomerNamecbo & "' _
                            ORDER BY tblPartInfo.PartNumber"
    
    
        Me.PartNumbercbo.RowSource = strSource
        Me.PartNumbercbo = vbNullString
    
    
    
    
    End Sub
    Ignore the '_' at the end of some of the lines. I know that is wrong, I just put them here so it would be easier to view.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    The _ is a line continuation character in vba.

    You must open and close your quotes (") on the same line. Make a complete string.

    eg: you have
    "WHERE CustomerName = '" & Me.CustomerNamecbo & "' _
    ORDER BY tblPartInfo.PartNumber"
    which should be (untested)
    "WHERE CustomerName = '" & Me.CustomerNamecbo & "'" _
    & " ORDER BY tblPartInfo.PartNumber"
    In effect you are building string segments on a line and concatenating these with &

  3. #3
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    That helps very much, thank you. Now I currently have:
    Code:
    Private Sub CustomerNamecbo_AfterUpdate()    
        Dim strSource As String
        strSource = "SELECT tblPartInfo.PartNumber, tblPartInfo.PartNumber " & _
                       "FROM tblPartInfo " & _
                       "WHERE CustomerName = '" & Me.CustomerNamecbo & "'" _
                        & " ORDER BY tblPartInfo.PartNumber"
    
    
        Me.PartNumbercbo.RowSource = strSource
        Me.PartNumbercbo = vbNullString
    
    
    End Sub
    but the cascadin combo box is not working. It asks to enter the paramater CustomerName and then does not update the next combo box. Any ideas?

    P.S. should i start a new thread for this question?

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  5. #5
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    My first combo box is not bound to the form, which I think is causing my problem. Is it still possible to let the criteria for the second combo box (which is bound to the form) be dependent upon the first, unbounded, combobox? I can either get nothing to show up in the second one or all of them, but not based upon the criteria from the first cb.

  6. #6
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    New Compressed (zipped) Folder.zip

    Here is my document in case anyone wanted to see for themselves. I have tried doing it both in VBA, in query builder, and SQL within the query builder.

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by dylcon View Post

    ...My first combo box is not bound to the form, which I think is causing my problem...
    That's not the problem. In point of fact, these Comboboxes are almost always Unbound!

    You need a semi-colon at the end of your SQL Statement:

    & " ORDER BY tblPartInfo.PartNumber;"


    so you end up with

    Code:
    Private Sub CustomerNamecbo_AfterUpdate()
        Dim strSource As String
        strSource = "SELECT tblPartInfo.PartNumber, tblPartInfo.PartNumber " & _
                       "FROM tblPartInfo " & _
                       "WHERE CustomerName = '" & Me.CustomerNamecbo & "'" _
                        & " ORDER BY tblPartInfo.PartNumber;"
    
    
        Me.PartNumbercbo.RowSource = strSource
        Me.PartNumbercbo = vbNullString
    
    
    End Sub

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The semi-colon is not required; I never include it when writing SQL in code. In the query, you use "Me." which you can't do, you have to use the full form reference there. Also, you're comparing the ID returned by the first combo to a name, so you never get a match.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    This code version works:

    Code:
        Dim strSource As String
        strSource = "SELECT tblPartInfo.PartNumber, tblPartInfo.PartNumber " & _
                       "FROM tblPartInfo " & _
                       "WHERE CustomerID = " & Me.CustomerNamecbo _
                        & " ORDER BY tblPartInfo.PartNumber"
        Debug.Print strSource
     
        Me.PartNumbercbo.RowSource = strSource
        Me.PartNumbercbo = vbNullString
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    The semi-colon would be required if opening an ADODB recordset.

    Why is PartNumber in the query twice? Should there be PartName? Why not just put the sql directly in the RowSource:

    SELECT PartNumber, PartName FROM tblPartInfo WHERE CustomerID=" & [CustomerNamecbo];

    Then in CustomerNamecbo AfterUpdate:

    Me.PartNumbercbo.Requery

    Are these comboboxes both bound or unbound? Are they on a continuous or datasheet form?

    What do you mean by 'run a command on more than one line'?
    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.

  11. #11
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    PBaldy,
    I have the comboboxes working appropriately now (thanks to your code), but I need that information to be inserted as the ID, not the actual PartNumber. How would I go about doing this?

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I just noticed that you had the part number field twice. One should probably be the ID field.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    Yeah sorry, I should have edited my previous post. I realized that right after I asked the question. Thanks again PBaldy.

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem.
    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: 2
    Last Post: 05-31-2013, 03:27 PM
  2. Button to filter records when typing something in
    By Doodlebug2000 in forum Forms
    Replies: 1
    Last Post: 12-06-2012, 02:21 PM
  3. Undo Typing in Unbound Textbox
    By June7 in forum Programming
    Replies: 4
    Last Post: 08-29-2012, 12:14 AM
  4. All text visible while I'm typing
    By viro in forum Access
    Replies: 4
    Last Post: 06-24-2012, 05:11 PM
  5. creating field names in table without typing
    By GHanover in forum Database Design
    Replies: 2
    Last Post: 12-14-2009, 05:13 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