Results 1 to 11 of 11
  1. #1
    hazeleyre23 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    34

    Insert into with a WHERE statement

    Hi,

    I was wondering if it is possible to do an INSERT INTO with a WHERE statement in VBA.

    Below is a copy of my code, however I only want it to insert on the same ID Line, is this possible?

    Code:
                CurrentDb.Execute "INSERT INTO dbo_Agent_Data(ToDate)" & _
                " VALUES('" & Me.txtUpdateToDate.Value & "')"
    thanks

  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,641
    Try the other format:

    INSERT INTO...
    SELECT...
    FROM...
    WHERE...
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    hazeleyre23 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    34
    Hi, it is coming up with a syntax error, would you be able to advise what I am doing wrong (sorry fairly new to Access).

    Code:
                CurrentDb.Execute "INSERT INTO dbo_Agent_Data(ToDate)" & _
                " SELECT ('" & Me.txtUpdateToDate.Value & "')" & _
                " FROM Forms![ToDateUpdate]" & _
                " WHERE id_New = '" & Me.txtUpdateID.Value & "'"
    thank you very much

  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,641
    The FROM would need to be the name of a table.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    If I understand you correctly, you do not want to use INSERT at all. Insert creates new records in your table; to change data in existing records (adding data into blank fields in existing records is not an INSERT), you need to use UPDATE.

    Your statement might look something like this:

    currentdb.Execute "Update dbo_Agent_Data set ToDate = #" & me!txtUpdateToDate & "# WHERE id_new = " & me!txtUpdateID

    The # signs are date delimiters in MS Access SQL.

    This assumes that the table dbo_Agent_Data is not the recordsource of your form. Does the form have a recordsource? It may not, but that's OK, it doesn't have to.

    Post back if you need further clarification.

  6. #6
    hazeleyre23 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    34
    Hi,

    Yes that is exactly what I am after, update the line rather then insert a new line, thank you

    I have put that statement into my code but I am now receiving an error message:

    "You must use the dbSeeChanges option with OpenRecordSet when accessing a SQL Server table that has an IDENTITY column"

    My Identity column column is ID, this is also my Primary Key. Not sure what I need to do next.....

    thank you for your help.
    Last edited by hazeleyre23; 04-06-2016 at 05:22 AM.

  7. #7
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Why not ue the design view? its a lot easier haha

  8. #8
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    OK, I didn't know you were using SQL Server. I don't use it, so I can't help much. I'm sure there are other gurus here who can.

    I'll keep watching this thread so I can learn the answer, too!

  9. #9
    hazeleyre23 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    34
    Ok thank you anyway for your help.

  10. #10
    hazeleyre23 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    34
    I solved it....

    Code:
    CurrentDb.Execute "UPDATE dbo_Agent_Data SET ToDate = #" & Me!txtUpdateToDate & "# WHERE id_NEW = " & Me!txtUpdateID & "", dbSeeChanges

  11. #11
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Glad you got it working - I'll have to look that option up.

    If only every problem had a solution as easy as that.....sigh!

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

Similar Threads

  1. Insert statement fail
    By JeroenMioch in forum Access
    Replies: 3
    Last Post: 04-29-2014, 09:11 AM
  2. INSERT statement
    By GraeagleBill in forum Programming
    Replies: 2
    Last Post: 03-29-2013, 12:53 PM
  3. Insert Into statement
    By TimMoffy in forum Programming
    Replies: 7
    Last Post: 07-13-2012, 07:10 AM
  4. Insert statement
    By crowegreg in forum Programming
    Replies: 2
    Last Post: 08-19-2011, 02:20 PM
  5. Replies: 4
    Last Post: 09-03-2010, 02:55 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