Results 1 to 5 of 5
  1. #1
    Only4Access is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    38

    Arrow INSERT INTO If Not Exists

    Hi Experts,

    I have a form bounded to a temporary table (only for bulk data entry). When click on a button, I am trying to INSERT records If those are Not Exists in a table.



    This is what I tried.


    Code:
    Dim MyDate As Date
    Dim MyNo As Long
    Dim MyRec As Long
    Dim strSQL As String
     
    MyDate = Dlookup(“DateID”,”tblTemp”)
    MyNo = Dlookup(“NoID”,”tblTemp”)
    MyRec = Dlookup(“RecID”,”tblTemp”)
     
    strSQL = "INSERT INTO tblAct (DateID, NoID, RecID)" & _
             "VALUES (#" & MyDate & "#, " & MyNo & ", " & MyRec & ")" & _
             "ON DUPLICATE KEY UPDATE [DateID] = [DateID];"
     
    If Me.Dirty Then
            Me.Dirty = False
    End If
                 
    CurrentDb.Execute strSQL, dbFailOnError
    But this is not working. Giving error 'Run Time Error 3137' "Missing semicolon (; ) at end of SQL statement.

    MyDate, MyNo & MyRec are getting the first value from a field.

    Thank you for your time.

  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,652
    To the best of my knowledge that isn't valid SQL. Try

    Code:
    strSQL = "INSERT INTO tblAct (DateID, NoID, RecID)" & _
             "VALUES (#" & MyDate & "#, " & MyNo & ", " & MyRec & ")"
    That may error inserting a duplicate primary key, and this may be a case when dropping dbFailOnError is appropriate.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Only4Access is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    38
    Thank you Paul, That works. Thanks a lot.

    But, how can i INSERT only IF NOT EXISTS in tblAct? tblAct has Auto_Number PK & this PK have a relation in another table. Below this particular code I have an Append query DoCmd.OpenQuery.....

    So If this exists stop & continue below Append query. How can i do that?

    Thank you for your time Paul.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    You could use DCount() to test before continuing.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Only4Access is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    38
    Sorry for the delay in reply Paul, due to my sickness. Thank you very much. Dcount did the trick. Still learning VBA....

    One last question.

    I have to check 4 conditions in Dcount, since my table is increasing day by day, does it cause any performance issue? Is there any alternate faster way, such as SQL NOT EXISTS?

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. Replies: 2
    Last Post: 12-04-2012, 01:03 AM
  3. Insert if not exists to another table.
    By ramindya in forum Queries
    Replies: 1
    Last Post: 06-12-2012, 07:16 PM
  4. How do I Insert Values w/ Not Exists sub select?
    By toddbailey in forum Queries
    Replies: 1
    Last Post: 05-01-2012, 12:05 PM
  5. if exists UPDATE else INSERT
    By lloyddobler in forum Programming
    Replies: 18
    Last Post: 01-04-2012, 11:35 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