Results 1 to 5 of 5
  1. #1
    cihan is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2012
    Posts
    6

    SQL expression INSERT INTO

    I am using the following code to insert a table, but INSERT INTO doesnt work.
    If I enter a number instead of parameter AUFNR --> ("INSERT INTO RESULTATO (Field1) VALUES ('AUFNR') "), then it inserts into the table.
    Field1 and AUFNR are both defined as Double...
    AUFNR is found correctly and is not null.

    The following message appears
    "Micrsoft office cant append all the records in the append query.....due to type conversion problem."

    Any help appreciated.
    ----------------


    Dim AUFNR As Double
    Dim strSQL As String
    Dim rst As Recordset
    Dim dbs As Database
    Set dbs = CurrentDb


    strSQL = ("SELECT [AUFNR] " _
    & "FROM AFXX LEFT JOIN VBAP " _
    & "ON VBAP.[KDAUF] = AFXX.[KDAUF] " _
    & "ORDER BY [AUFNR];")

    Set rst = dbs.OpenRecordset(strSQL)
    rst.MoveFirst
    AUFNR = rst![AUFNR]
    DoCmd.RunSQL ("INSERT INTO RESULTATO (Field1) VALUES ('AUFNR') ")

    rst.Close
    Set rst = Nothing

  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,521
    ...values(" & aufnr & ")"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by cihan View Post
    I am using the following code to insert a table, but INSERT INTO doesnt work.
    If I enter a number instead of parameter AUFNR --> ("INSERT INTO RESULTATO (Field1) VALUES ('AUFNR') "), then it inserts into the table.
    Field1 and AUFNR are both defined as Double...
    AUFNR is found correctly and is not null.

    The following message appears
    "Micrsoft office cant append all the records in the append query.....due to type conversion problem."

    Any help appreciated.
    ----------------
    Dim AUFNR As Double
    Dim strSQL As String
    Dim rst As Recordset
    Dim dbs As Database
    Set dbs = CurrentDb


    strSQL = ("SELECT [AUFNR] " _
    & "FROM AFXX LEFT JOIN VBAP " _
    & "ON VBAP.[KDAUF] = AFXX.[KDAUF] " _
    & "ORDER BY [AUFNR];")

    Set rst = dbs.OpenRecordset(strSQL)
    rst.MoveFirst
    AUFNR = rst![AUFNR]
    DoCmd.RunSQL ("INSERT INTO RESULTATO (Field1) VALUES ('AUFNR') ")

    rst.Close
    Set rst = Nothing

    1) "AUFNR" is a number (double) so it doesn't require delimiters.
    2) You have to concantate the variable into the SQL string.

    The red are my changes.......

    Code:
    Dim dAUFNR As Double
    Dim strSQL As String
    Dim rst As Recordset
    Dim dbs As Database
    Set dbs = CurrentDb
           
           
    strSQL = "SELECT [AUFNR]" 
           strSQL =strSQL & " FROM AFXX LEFT JOIN VBAP"
           strSQL =strSQL & " ON VBAP.[KDAUF] = AFXX.[KDAUF]" 
           strSQL =strSQL & " ORDER BY [AUFNR];"
    
    Set rst = dbs.OpenRecordset(strSQL)
    rst.MoveFirst
    
    'you should check to ensure there is a record
    dAUFNR = rst![AUFNR]
    
    strSQL = "INSERT INTO RESULTATO (Field1) VALUES (" & AUFNR & ");"
    dbs.Execute strSQL, dbFailOnError
    
    rst.Close
    Set rst = Nothing

  4. #4
    cihan is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2012
    Posts
    6
    Thank you pbaldy
    It is solved.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help.
    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. INSERT query: insert new data only
    By drh in forum Access
    Replies: 2
    Last Post: 04-04-2014, 05:31 PM
  2. Insert into
    By glasgowlad1999 in forum Access
    Replies: 2
    Last Post: 10-14-2011, 02:38 PM
  3. New insert row
    By khparhami in forum Access
    Replies: 5
    Last Post: 09-06-2010, 10:37 AM
  4. SQL Insert into
    By jamin14 in forum Programming
    Replies: 15
    Last Post: 04-01-2010, 12:35 AM
  5. help with insert
    By jamie in forum Access
    Replies: 1
    Last Post: 11-16-2009, 06:02 AM

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