Results 1 to 8 of 8
  1. #1
    Mazdaspeed6 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    14

    Function to insert records inserts record, but somehow deletes old record

    I am trying to use a form to insert new records into a table. I have a function that is called,


    Code:
    Function Insert_Asset_Click(ByVal AssetTag As String, ByVal Make As String, ByVal Model As String, ByVal ServiceTag As String)
        Dim conn As ADODB.Connection
        Dim cmd As ADODB.Command
        Dim rs As ADODB.Recordset
        Dim myErr As ADODB.Error
        Dim strError As String
        On Error GoTo myHandler
        'Dim sConn As String
        Dim query As String
    
        'MsgBox "Got Here" 'Debug command
        
        Set conn = CurrentProject.Connection
           
        Set cmd = New ADODB.Command
        cmd.ActiveConnection = CurrentProject.Connection
        cmd.CommandText = "INSERT INTO Device(AssetTag, Make, Model, ServiceNum) VALUES(" + AssetTag + ", " + Make + ", " + Model + ", " + ServiceNum + ");"
        cmd.CommandType = adCmdText
        
        'Set rs = cmd.Execute
        
        DoCmd.RunMacro "Converted Macro-Clear_Insert_Device", 1
        
        'Close connection to database
        conn.Close
        Exit Function
    I have no idea why all i see in the table is one record after multiple attempts to insert different records into the table. I get no error messages. I am new to coding with VBA so i bet its something simple, but i'm totally confused.

  2. #2
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    you didn't run the insert statement because you commented it out:
    'Set rs = cmd.Execute

    another thing, your commandText is not right, should be:

    cmd.CommandText = "INSERT INTO Device(AssetTag, Make, Model, ServiceNum) VALUES('" + AssetTag + "', '" + Make + "', '" + Model + "', '" + ServiceNum + "');"

    Please modify and run again.

  3. #3
    Mazdaspeed6 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    14
    Thanks for the help but its still not working. I run it and the record gets inserted, but when I try to select all the records from the table, only one gets returned, the record i most recently added.

  4. #4
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    I don't know what this Macro do:
    DoCmd.RunMacro "Converted Macro-Clear_Insert_Device", 1
    may it delete the original record?

  5. #5
    Mazdaspeed6 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    14
    Its supposed to run a macro to clear the form, but it doesn't work. I tried commenting it out and running the function, still doesn't keep the old record.

  6. #6
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    Please check all the steps of adding a record, besides(before and after) this function.

  7. #7
    Mazdaspeed6 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    14
    I figured it out, there was some other code suppressing the error but i thought it was going to print the error. I was passing the value ServiceTag in the function but i used the variable ServiceNum when writing the SQL statement. What i don't understand is how it still inserted the record even with the error. But i figured out what the issue i was having was.

    Thanks for your help.

  8. #8
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    u r welcome

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

Similar Threads

  1. How to calucalte tow records in on record..
    By Alwesam4u in forum Queries
    Replies: 1
    Last Post: 11-26-2010, 11:39 PM
  2. Insert Record checks table
    By pfarnell in forum Forms
    Replies: 13
    Last Post: 09-05-2010, 10:47 AM
  3. Delete record function
    By Evgeny in forum Programming
    Replies: 6
    Last Post: 04-12-2010, 09:19 PM
  4. Replies: 3
    Last Post: 03-22-2010, 04:30 PM
  5. New Record has moved all other records...
    By medmons in forum Access
    Replies: 0
    Last Post: 06-01-2007, 01:00 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