Results 1 to 4 of 4
  1. #1
    saltydawg617 is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2011
    Posts
    4

    Question Using VBA Variables in a SQL statement


    I am using a barcode reader to capture data that is encoded in an ID card and enter it into a table. An input box opens so that you can scan the id. The ReadIDCard() then breaks the data down into different sections to corrispond with LastName, FirsName, and etc. Then I want the data to be inserted into a table Check_In.

    The code works fine until it gets to the SQL statement. I receive a input box titled "Input parameter value" and I have to type in the value. My question is how do I pass the value from a VBA variable into the SQL statement without having to type it in.

    Code:
     
    Public Function ReadIDCard()
     
    DIM txtIN As String
    DIM txtSSN As String
    DIM txtFirstName As String
    DIM txtLastName As String
     
    txtIN = InputBox("Scan ID") 'Scan ID Card Popup Box
    txtSSN = Mid$(txtIN, 2, 6) 'SSN
    txtFirstName = Trim(Mid$(txtIN, 16, 20)) 'First Name
    txtLastName = Trim(Mid$(txtIN, 36, 26)) 'Last Name
     
    DoCmd.RunSQL "INSERT INTO Check_In ([SSN], [LastName], [FirstName]) Values (txtSSN, txtLastName, txtFirstName)"

  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,641
    They have to be concatenated into the string. Text and date values need delimiters. Along the lines of

    Code:
    "...Values ('" & txtSSN & "',..."
    Decent tutorial here:

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

  3. #3
    saltydawg617 is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2011
    Posts
    4
    Thanks for the help. It was all in the spacing.

  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,641
    Happy to help, and welcome to the site.
    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 variables in SQL
    By compooper in forum Programming
    Replies: 3
    Last Post: 07-06-2011, 11:04 AM
  3. VBA in variables
    By smikkelsen in forum Access
    Replies: 3
    Last Post: 11-12-2010, 03:14 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