Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    shylock is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100

    Insert Into Query

    I am trying to insert a record into the LogicalServer table with an INSERT INTO query from VBA. I get a "Syntax error in INSERT INTO statement" when I use DoCmd.RunSQL sqlAppend statement.

    I set up the variable I use for the VALUES portion of the query as follows:
    Code:
     SNm = Me.txtServerNm
        For Each varItm In lstIP.ItemsSelected
            vIP = Me.lstIP.Column(0, varItm)
        Next varItm
        CD = Me.txtComDate
        For Each varItm In lstServerType.ItemsSelected
            vType = Me.lstServerType.Column(0, varItm)
        Next varItm
        If (Me.txtDecommDate = "") Then
            Ddaate = Empty
        Else
            Ddate = Me.txtDecommDate
        End If
        Stat = Me.txtStatus
        For Each varItm In lstSvrTypeId.ItemsSelected
            PhysS = Me.lstSvrTypeId.Column(0, varItm)
        Next varItm
    My sqlAppend is
    Code:
     sqlAppend = "INSERT INTO LogicalServer([Name], [IPID], [CommissionDate], [LogicalSerTypeID], [DecommissionDate], [Sttus], [Physical]" _
                    & " VALUES (" & SNm & ", '" & vIP & ", '" & CD & ", '" & vType & ", '" & Ddate & ", '" & Stat & ", '" & PhysS & "');"
    My table has the following fields:

    Field Name Data Type
    LogicalServerID Number This number is assigned automatically upon saving the record. It is also a GUID as are IPID and LogicalServerTypeID
    Name Short Text
    IPID Number
    CommissionDate Date/Time
    LogicalServerTypeID Number
    DecommissionDate Date/Time
    Status Short Text


    Physical Yes/No

    the DeBug>print sqlAppend shows the following:
    Code:
    INSERT INTO LogicalServer([Name], [IPID], [CommissionDate], [LogicalSerTypeID], [DecommissionDate], [Sttus], [Physical] VALUES (AAAA-JackTest, '{72FE854E-5056-9F6E-4C35-F6A172A91CA9}, '3/21/2019, '{A331FB98-E7FE-E555-74FA-AC3CD4221013}, '12:00:00 AM, 'Active, 'False');
    All the VALUES are correct.

    If I include the LogicalServerID field in the query I still get an error as this Value would be empty since it is not assigned until the record is saved.

    Can anyone show me what is wrong?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    dont use vba sql, instead use an append query. It wont get the syntax wrong.

  3. #3
    shylock is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100
    ranman256:

    I changed from INSERT INTO to APPEND INTO as follows:
    Code:
     sqlAppend = "APPEND INTO LogicalServer([Name], [IPID], [CommissionDate], [LogicalSerTypeID], [DecommissionDate], [Sttus], [Physical]" _
                   & " VALUES (" & SNm & ", '" & vIP & ", '" & CD & ", '" & vType & ", '" & Ddate & ", '" & Stat & ", '" & PhysS & "');"
    I get an error: "Run-time error '3129' Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'

    Do you have any other suggestions?

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,880
    Why the For each loop? are you using a multi-select listbox?

  5. #5
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I see lots of little things and a big thing.

    You don't state what the BE is.... I am thinking it is SQL Server (Express)???

    Little things:
    "Name" is a reserved word - shouldn't be used for object names.
    Possible you have misspelled words: [Sttus]

    In the code
    Code:
        If (Me.txtDecommDate = "") Then
             Ddaate = Empty
        Else
            Ddate = Me.txtDecommDate
        End If

    The big thing is you do not have the delimiters correct.
    Without knowing what your field types are and what dB engine you are using, this is what I would start with
    Code:
    sqlAppend = "INSERT INTO LogicalServer([Name], [IPID], [CommissionDate], [LogicalSerTypeID], [DecommissionDate], [Sttus], [Physical]"
    
     sqlAppend = sqlAppend & " VALUES ('" & SNm & "', '" & vIP & "', '" & CD & "', '" & vType & "', '" & Ddate & "', '" & Stat & "', '" & PhysS & "');"
    The variable "SNm" needs delimiters. You are missing the closing delimiter for most of the rest of the variables.



    BTW, the top 2 lines of EVERY code module should be
    Code:
    Option Compare Database
    Option Explicit      '<<-- 

  6. #6
    shylock is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100
    ssnafu:

    Thanks for the reply. It makes sense that I didn't have the delimiters correct. I will redo them and try again when I go back to work on Monday.

    I am using MS Access 2013. Is that what you meant by "You don't state what the BE is...."?

    The little things: I have corrected the mistyped words: Ddaate is Ddate; SttuS is Status.

    I know Name is a reserved word but the table comes from SQL Server and I am not permitted to change the original tables.

    I will let you know on, Monday, if your delimiter changes work.

  7. #7
    Minty is online now VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,159
    As an extra gotcha, your date fields will need to be delimited with # # if this is Access, not ' ' .
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,880
    I have corrected the mistyped words: Ddaate is Ddate; SttuS is Status.
    Then I hope you didnt miss steve's advice
    BTW, the top 2 lines of EVERY code module should be
    Code:
    Option Compare Database
    Option Explicit '<<--

  9. #9
    shylock is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100
    ssnafu:

    could you please resend your version of the sqlAppend. What you sent is overlapping and garbled.

    Also, can you explain why each cn with ode module should begin with "Option Explicit"?


    I have modified my SQL to get it as close to what ssnafu suggests.

    Code:
       sqlAppend = "INSERT INTO LogicalServer([Name], [IPID], [CommissionDate], [LogicalServerTypeID], [DecommissionDate], [Status], [Physical]" _
                   & " VALUES('" & SNm & "', '" & vIP & "', '" & CD & "', '" & vType & "', '" & Ddate & "', '" & Stat & "', '" & PhysS & "');"
    I still get the syntax error. Is thois because of what Minty says in post #7? If so, how do I put the # symbols around the dates?
    Last edited by shylock; 03-22-2019 at 02:33 PM. Reason: Added code

  10. #10
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Speaking of misspelling, it is ssanfu - not ssnafu....


    Quote Originally Posted by shylock View Post
    I am using MS Access 2013. Is that what you meant by "You don't state what the BE is...."?
    I saw the single quotes for the date and thought you might have a SQLS as the BE.


    OK, when creating an SQL statement, if the data type is a number, no delimiters are needed.
    If the data type is text, must use single/double quotes delimiters.
    If the data type is date/time, must use hash (#) mark delimiters.
    See Build an SQL statement in VBA

    Code:
    sqlAppend = "INSERT INTO LogicalServer([Name], [IPID], [CommissionDate], [LogicalSerTypeID], [DecommissionDate], [Sttus], [Physical]"
    sqlAppend = sqlAppend & " VALUES ('" & SNm & "', '" & vIP & "', #" & CD & "#, '" & vType & "', #" & Ddate & "#, '" & Stat & "', '" & PhysS & "');"
    Note: I added the delimiters (#) for the two date fields.

  11. #11
    shylock is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100
    ssanfu:

    Oops, sorry about that. Maybe I'm a bit dyslexic..

    I tried the following code per your suggestion:

    Code:
        sqlAppend = "INSERT INTO LogicalServer ([Name], [IPID], [CommissionDate], [LogicalServerTypeID], [DecommissionDate], [Status], [Physical]"
        sqlAppend = sqlAppend & " VALUES ('" & SNm & "', '" & vIP & "', #" & CD & "#, '" & vType & "', #" & Ddate & "#, '" & Stat & "', '" & PhysS & "');"
    I still get a syntax error.

    the Debug.Print is correct, as shown:

    Code:
    INSERT INTO LogicalServer ([Name], [IPID], [CommissionDate], [LogicalServerTypeID], [DecommissionDate], [Status], [Physical] VALUES ('A1A-JacksTest', '{AE7E8E8B-E7FE-E555-7F9E-F1025BD0D729}', #3/22/2019#, '{A331FB98-E7FE-E555-74FA-AC3CD4221013}', #12:00:00 AM#, 'Active', 'No');
    Thanks for showing me how to put the # delimiters in.

    Thanks,
    Jack

  12. #12
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,425
    these are guid values {A331FB98-E7FE-E555-74FA-AC3CD4221013} ??
    They are not text. I've had very little experience with them but think they're 32 byte values and need to be referenced as

    {guid{A331FB98-E7FE-E555-74FA-AC3CD4221013}} or something like that. Would have to dig deeper to be sure.

    After digging, maybe:
    sqlAppend = "INSERT INTO LogicalServer ([Name], [IPID], [CommissionDate], [LogicalServerTypeID], [DecommissionDate], [Status], [Physical]"
    sqlAppend = sqlAppend & " VALUES ('" & SNm & "', '" & vIP & "', #" & CD & "#, {guid{" & vType & "}}, #" & Ddate & "#, '" & Stat & "', '" & PhysS & "');"
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  13. #13
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I still get a syntax error.
    It always helps to say what the error is.......

    I've never used GUIDs either.
    I didn't/don't know what the field types are for IPID and LogicalServerTypeID (both look to be GUIDs).....

  14. #14
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,425
    I see that I missed one. Oh well, hopefully the sample provided enough of a clue.

  15. #15
    Minty is online now VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,159
    Also - is Physical a boolean field? If so "No" won't be valid.
    Either 0 -1, True False or No Yes without the quotes.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Insert into query vba
    By jaryszek in forum Access
    Replies: 4
    Last Post: 07-28-2017, 05:00 AM
  2. Replies: 1
    Last Post: 08-29-2016, 07:50 PM
  3. Replies: 3
    Last Post: 05-27-2015, 01:04 PM
  4. INSERT query: insert new data only
    By drh in forum Access
    Replies: 2
    Last Post: 04-04-2014, 05:31 PM
  5. INSERT INTO query in VBA
    By jgelpi16 in forum Programming
    Replies: 5
    Last Post: 09-17-2010, 02:19 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