Results 1 to 3 of 3
  1. #1
    Fredrik831 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2012
    Location
    Sweden
    Posts
    14

    My "INSERT INTO" code doesn't work when earlier posts exists.

    Hi!



    I want to create a form where the user selects a value from a combobox, press a command button and the action would then be registered with the value (from the combobox) and the current time in a seperate table. I'm using VBA code that's triggered when i press the command button.

    My target table consists of: FixturID, Tidpunkt, DatumID (autonumber and primary key).

    I understand that I shall not include the autonumber field in the VBA code for my autonumber to generate a value successfully. My code works as it should, as long as the FixturID is unique. As soon as I select a value that is already registered, then Access doesn't register a new post.

    Does somebody know what's wrong? FixturID is a foreign key, but that shouldn't interfere?

    Here is my code:
    Private Sub Commandbutton_Click()
    CurrentDb.Execute "INSERT INTO Datum (FixturID, Tidpunkt) VALUES (" & Fixid.Value & ", #" & (Text5.Value) & "#)"
    End Sub

    So, a summarized version of my question: How can I get access to register a new post even if an earlier post with the FixturID exists?


    Thanks in advance!

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    I'm not sure I'm following your details, but it seems you may have a unique index on FixturId in your target table - or -
    FixturId (the fk) doesn't exist in the other table.

    You may need a Unique composite index on FixturId + Tidpunkt to prevent duplicates in you Target table.

    You may also have to use DateValue(Text5.Value) or CDate(Text5.Value) to get the conversion from string/text to date???

    As a standard practice you should put a
    Debug.Print "INSERT INTO Datum (FixturID, Tidpunkt) VALUES (" & Fixid.Value & ", #" & (Text5.Value) & "#)"

    before your Currentdb.execute line and comment out the Currentdb.execute to see what the SQL looks like to ensure the SQL is OK.
    Last edited by orange; 12-03-2012 at 08:15 AM. Reason: typo/spelling

  3. #3
    Fredrik831 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2012
    Location
    Sweden
    Posts
    14
    Thank you, it seems Access somehow had a unique index on FixturID. I removed the referential integrity, and after that it all worked. But, then I added the referential integrity again and it still worked. I'm glad, but confused

    I inserted the Debug.Print statement and this was inserted in the immediate window:
    INSERT INTO Datum (FixturID, Tidpunkt) VALUES (3, #2012-12-04 07:50:32#)

    Thanks again!

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

Similar Threads

  1. Replies: 2
    Last Post: 09-29-2012, 11:22 PM
  2. Replies: 1
    Last Post: 04-05-2012, 08:26 AM
  3. VBA code syntac doubt "Before Insert"
    By ramindya in forum Programming
    Replies: 1
    Last Post: 02-12-2012, 07:52 PM
  4. Replies: 0
    Last Post: 01-11-2012, 12:34 PM
  5. Replies: 7
    Last Post: 01-19-2011, 10:39 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