Error in SQL INSERT INTO statement.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • usha2
    New Member
    • Mar 2012
    • 23

    #1

    Error in SQL INSERT INTO statement.

    Table name:All_Table
    I have a string value in string:strdata
    A column in Table:login
    How can i insert the string value in column"login"?
    I tried this following code,but give syntax error msg.
    Code:
    strIns = "INSERT INTO All_Table
    (login) VALUES " & strdata "
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    The correct syntax for an insert statement is
    Code:
    INSERT INTO tableName (fieldName)
    VALUES ('field value')

    Comment

    • usha2
      New Member
      • Mar 2012
      • 23

      #3
      Thank you.
      When my field values are in string then what will be the syntax.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        That is the syntax for a string.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32669

          #5
          In VBA that would be something like :
          Code:
          strVar = "INSERT INTO [All_Table] ([Login]) " & _
                   "VALUES ('" & strSomeString & "')"

          Comment

          • Mihail
            Contributor
            • Apr 2011
            • 759

            #6
            Declare a Public variable strData in a Public module (Of course you must remove the declaration from your actual module if it is not a public one)
            In the same module design a function fGetData()

            Code:
            Option Explicit
            Public strData As String
            
            Public fGetData() As String
                fGetData = strData
            End Function

            Then use Rabbit's approach:
            Code:
            INSERT INTO tableName (fieldName)
            VALUES (fGetData())

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32669

              #7
              I'm sorry to say Mihail, that while that approach may well work functionally, it is generally considered a clumsy one. Instead of tidying the design, it spreads out where the various elements can be found. Not an approach to recommend :-(

              Furthermore, and possibly even more important a point, it is introducing a potentially considerable delay into the query. SQL queries are optimised to the data. If a VBA function is introduced into the mix in the WHERE clause then each record of input must be extracted (thereby bypassing most of the said optimisations) and processed through VBA code which is, at least partially, interpreted. Relatively speaking, this is like taking a bus to win a F1 Grand Prix. Far better to work out the correct SQL first in VBA, in a single process, then execute it at top speed.

              Comment

              • Mihail
                Contributor
                • Apr 2011
                • 759

                #8
                Good to know, NeoPa.
                I provide a solution; I don't say that is the best one :)

                Comment

                Working...