How to use "Insert Into" to programatically insert multiple fields into a table

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CFW

    #1

    How to use "Insert Into" to programatically insert multiple fields into a table

    I use the following flawlessly to insert a single field:

    strSQL = "Insert into [_tblCasket] (Casket) Values " _
    & "(" & conQuote & NewCasket & conQuote & ")"
    Set db = CurrentDb

    If MsgBox(NewCaske t & " is not in the list. Do you want to add "
    & NewCasket & _
    " as a new Casket/Container in the list?", vbYesNo, _
    "Add a Casket/Container") = vbYes Then
    db.Execute strSQL
    Response = acDataErrAdded
    Else
    Response = acDataErrDispla y
    End If

    I can't figure out the syntax to insert into multiple fields?! Can I?
    TIA!
  • John OBrien

    #2
    Re: How to use "Insert Into" to programatically insert multiple fields into a table

    From what I can tell you are simply not specifying multiple fields
    there.
    Then your values appear to be improperly formatted.

    try:

    strSQl = "INSERT INTO _tblCasket (field1, field2, field3 ... ) VALUES
    {'" & varField1 & "' , '" & varField2 & "' , '" & varField3 & "');"

    such that the concatenated string has the proper SQL format:

    INSERT INTO _tblCasket (field1,field2, field3)
    VALUES ('stringvalue1' ,#datevalue2#,n umvalue3);

    keeping in mind that only string values need to be quoted, dates
    should be bracketed by #01/01/2003# to preserve their formatting and
    numbers need no qualifiers at all.

    also remember to use [] if your field names match internal keywords
    such as [Date] etc.

    regards,

    john OBrien
    Applied Computer Science



    CFW <cfwilk@comcast .net> wrote in message news:<tsa3pvgqr v9lenr6oqe27dpd c4arqc4l4s@4ax. com>...[color=blue]
    > I use the following flawlessly to insert a single field:
    >
    > strSQL = "Insert into [_tblCasket] (Casket) Values " _
    > & "(" & conQuote & NewCasket & conQuote & ")"
    > Set db = CurrentDb
    >
    > If MsgBox(NewCaske t & " is not in the list. Do you want to add "
    > & NewCasket & _
    > " as a new Casket/Container in the list?", vbYesNo, _
    > "Add a Casket/Container") = vbYes Then
    > db.Execute strSQL
    > Response = acDataErrAdded
    > Else
    > Response = acDataErrDispla y
    > End If
    >
    > I can't figure out the syntax to insert into multiple fields?! Can I?
    > TIA![/color]

    Comment

    • Pieter Linden

      #3
      Re: How to use &quot;Insert Into&quot; to programatically insert multiple fields into a table

      you mean you couldn't copy anything from an Insert query? Or the help
      file? The help file is your friend!

      INSERT INTO table (field1, field2, field3...) VALUES
      (value1,value2, value3)

      Comment

      Working...