Results 1 to 4 of 4
  1. #1
    compooper is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2011
    Posts
    87

    VBA variables in SQL

    Hi folks,

    I am trying to use a variable (defined in my function) in a SQL statement.
    But its not working. I've bolded the line where my function fails to work, everything else is A-OK!

    Function Upload(Filepath As String, CoName As Integer, InvType As Integer, InvDate As Date)
    Dim dbs As Database
    Set dbs = CurrentDb
    If CoName = 1 And InvType = 2 Then
    DoCmd.TransferText acImportDelim, , "Inv_Co1", Filepath, True
    DoCmd.SetWarnings False
    dbs.Execute "qryCo1apdMerge"
    dbs.Execute "qryCo1updtNotApp"
    DoCmd.RunSQL "UPDATE TestTable SET TestTable.CompanyID = FilePath AND TestTable.InvType = InvType AND TestTable.InvDate = InvDate WHERE TestTable.CompanyID is Null AND TestTable.InvType is Null AND TestTable.InvDate is Null"
    DoCmd.RunSQL "DELETE * FROM Inv_Co1"
    DoCmd.SetWarnings True
    End If


    End Function

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Variables have to be concatenated into the string:

    "...SET TestTable.CompanyID = " & FilePath & " more SQL here"

    Generally text values need to be surrounded by single quotes, date values by #:

    "...SET TestTable.CompanyID = '" & FilePath & "' more SQL here"

    By the way, I don't think the "AND" between values will work. The syntax I'm familiar with is commas:

    UPDATE...
    SET Field1 = 123, Field2 = 'abc', Field3 = #7/6/11#

    A tutorial/sample db on building SQL in VBA:

    http://www.baldyweb.com/BuildSQL.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    compooper is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2011
    Posts
    87
    Worked brilliantly, thanks so much!
    By the way, your sample databases are great!! Really helped me build my database!

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Glad it helped!
    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. Hyperlink Variables
    By CCW8 in forum Access
    Replies: 5
    Last Post: 07-08-2013, 06:20 PM
  2. VBA in variables
    By smikkelsen in forum Access
    Replies: 3
    Last Post: 11-12-2010, 03:14 PM
  3. Passing variables to a Word Doc.
    By techexpressinc in forum Forms
    Replies: 2
    Last Post: 09-16-2010, 01:04 PM
  4. Variables in Forms
    By NewDeveloper in forum Forms
    Replies: 1
    Last Post: 06-20-2010, 08:04 AM
  5. sql in vb variables
    By emilylu3 in forum Programming
    Replies: 3
    Last Post: 03-04-2006, 01:26 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
  •  
Other Forums: Microsoft Office Forums