Results 1 to 11 of 11
  1. #1
    The is offline Novice
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    13

    Question Syntax error when inserting new record


    Hi guys

    I got Syntax error when running that code:
    Code:
    Private Sub AddTenants_Click()
    Dim strInsert As String
    strInsert = "Insert into Tenants ( T_Name, Nationality, Employer, Lease_Date, U_ID, Rent, Phone)" & _
    " Values ( '" & Me.Name & " ', '" & Me.Nationality & "', '" & Me.Employer & "', '" & Me.Lease_Date & "', " & Me.U_ID & ", " & _
    Me.Current_Rent & ", '" Me.Phone "')"
    CurrentDb.Execute (strInsert)
    Dim New_T_ID As dao.Recordset
    Set New_T_ID = CurrentDb.OpenRecoedSet("select @@identity")
    CurrentDb.Execute "UPDATE Units SET Current_Tenant=" & New_T_ID &, Current_Rent= " & Me.Rent & " WHERE U_ID= " & Me.U_ID & " ;"
    End Sub
    All the date taken from fields in the form. I press the add new record button to execute that code. Then get the last inserted id to use it in another update query.
    There is auto increment id in the Tenants table, I didn't add it to the statement to allow database to generate one, not sure if that is wrong in access DB.
    I am working on the latest version of MS access

    Thank you guys

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Missing &s

    & Me.Current_Rent & ", '" Me.Phone & "')"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    The is offline Novice
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    13
    Thanks man

    Now I am getting "method or data member not found" and it highlits ".OpenRecoedSet" from the line:
    Code:
    Set New_T_ID = CurrentDb.OpenRecoedSet("select @@identity")
    from that line, I am trying to get the generated id for the recently added row

  4. #4
    The is offline Novice
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    13
    Another issue I am dealing with, The insertion update the last current record instead of adding new one. How can that be if I am using "INSERT" SQL command

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Fix the spelling of OpenRecoedSet to OpenRecordSet

    You have @@ in table or query name? That SELECT statement is incomplete, need FROM clause.

    The UPDATE statement is missing quote mark.

    & New_T_ID & ", Current_Rent= "

    However, I don't think referencing a recordset like that will work. Don't need recordset anyway. Need SQL string.

    Dim New_T_ID As String
    New_T_ID = "SELECT identity FROM sometable"

    Why are you using INSERT and UPDATE actions? Are you not using bound form?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    India
    Posts
    616
    June is already helping you out.Is Lease_Date a date field? If yes, then code will fail as date should be surrounded by # instead of ' .

  7. #7
    The is offline Novice
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    13
    Quote Originally Posted by June7 View Post
    Fix the spelling of OpenRecoedSet to OpenRecordSet

    You have @@ in table or query name? That SELECT statement is incomplete, need FROM clause.

    The UPDATE statement is missing quote mark.

    & New_T_ID & ", Current_Rent= "

    However, I don't think referencing a recordset like that will work. Don't need recordset anyway. Need SQL string.

    Dim New_T_ID As String
    New_T_ID = "SELECT identity FROM sometable"

    Why are you using INSERT and UPDATE actions? Are you not using bound form?
    This form was a bound form, and I am trying to make it manually because I have good experience on MySQL than MS Access. My date is related between different table, and this is easier to me.

    Should your code be like:
    Code:
    Dim New_T_ID As String
    New_T_ID = CurrentDb.Execute ("SELECT identity FROM sometable")
    Is that better?

    I really appreciate your valuable help

  8. #8
    The is offline Novice
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    13
    Thanks sir, yah it is a date, and I didn't know that before.

    Thanks

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Good catch on the date delimiters.

    No, should be as I already showed: New_T_ID = "SELECT identity FROM sometable"

    You don't want a recordset, you want SQL string to concatenate with the other SQL string and then Execute the constructed nested SQL statement.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Also note that Currentdb.Execute can only be used with action queries: "INSERT" (append), "UPDATE" or "DELETE".
    It will not function with "SELECT" queries.

  11. #11
    The is offline Novice
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    13
    Thank you guys

    All done

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 16
    Last Post: 06-03-2014, 10:22 PM
  2. Replies: 2
    Last Post: 04-14-2014, 10:42 PM
  3. Replies: 1
    Last Post: 02-21-2013, 12:27 PM
  4. Incomplete Syntax Clause (syntax error)
    By ajetrumpet in forum Programming
    Replies: 4
    Last Post: 09-11-2010, 10:47 AM
  5. error on inserting value
    By dada in forum Programming
    Replies: 3
    Last Post: 08-18-2010, 11:28 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