SQL insert into table using variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamesnkk
    New Member
    • Nov 2006
    • 134

    #1

    SQL insert into table using variables

    I tried and search around the web, but could not find an answer to it,
    You see I am trying to insert 3 variables into the table, but seem like those " and ' cause the problem, No matter how I play with the ' and ", it still don;t work What wrong with the statement, could you please help.



    Dim cust As String
    Dim tel As String
    Dim cont As String

    cust = "Albert Longman"
    tel = "1234567890 "
    cont = "123456789"



    Dim sqls As String

    sqls = "insert into tblinvoice(cust omer, contact, telephone) values(' " & cust & " '," & cont & " ','" & tel & ")"
    DoCmd.RunSQL strstmt
  • MSeda
    Recognized Expert New Member
    • Sep 2006
    • 159

    #2
    try this

    Dim cust As String
    Dim tel As String
    Dim cont As String
    Dim sqls As String

    cust = "Albert Longman"
    tel = "1234567890 "
    cont = "123456789"


    sqls = "insert into tblinvoice(cust omer, contact, telephone) values('" & cust & "', '" & cont & "', '" & tel & "');"
    DoCmd.RunSQL sqls

    Comment

    • jamesnkk
      New Member
      • Nov 2006
      • 134

      #3
      Originally posted by MSeda
      try this

      Dim cust As String
      Dim tel As String
      Dim cont As String
      Dim sqls As String

      cust = "Albert Longman"
      tel = "1234567890 "
      cont = "123456789"


      sqls = "insert into tblinvoice(cust omer, contact, telephone) values('" & cust & "', '" & cont & "', '" & tel & "');"
      DoCmd.RunSQL sqls

      Yeah It working, Thanks so much.

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        You are missing a couple of single quotes, also what the spaces and you were running strstmt.

        Try this

        Code:
        Dim cust As String
        Dim tel As String
        Dim cont As String
         Dim sqls    As String
        
           cust = "Albert Longman"
           tel = "1234567890"
           cont = "123456789"
        
           sqls = "INSERT INTO tblinvoice (customer, contact, telephone) " _
        	  "VALUES ('" & cust & "','" & cont & "','" & tel & "');"
           DoCmd.RunSQL sqls
        Mary

        Comment

        Working...