Results 1 to 3 of 3
  1. #1
    lzook88 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    75

    VBA Inserting two new records

    I have VBA code that inserts a new record in two tables from one form.



    When I press the submit button, it creates the two records in the separate tables as I wanted, however it creates a blank record with only one field ("Station") filled out in the table tblUsers.

    Code:
    Private Sub CloseUser_Click()
    Dim DBBS As Database
    Dim rs As DAO.Recordset
    Dim rss As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT FullName, HashID, Password, UserSecurity, Station, Status FROM tblUsers")
    
    With rs
        .AddNew
        !FullName = Me.UserName
        !HashID = Me.HashID
        !Password = Me.Password
        !UserSecurity = Me.UserSecurity
        !Station = Me.Station
        !Status = Me.Status
        .Update
    End With
    rs.Close
    Set rs = Nothing
    Set rss = CurrentDb.OpenRecordset("SELECT PLocation, Shift, Type, Station FROM tblLocations")
    With rss
        .AddNew
        !PLocation = Me.HashID
        !Shift = Me.Shift
        !Type = Me.Type
        !Station = Me.Station
        .Update
    End With
    rss.Close
    Set rss = Nothing
    DoCmd.Close
    End Sub
    I have a feeling it might have something to do with both tables having that station value but that is all I could think of.. any help would be greatly appreciated.

    Thanks as always

  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,726
    Is the button CloseUser your "submit" button?

    I would put a break point on your first Set statement, the step through the code using local watches to confirm values and assignments. Since you are not looping in your code, I don't see why/how it would add ore than 1 record in each table.
    But you may find something unexpected by stepping through the code.
    Good luck.

  3. #3
    lzook88 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    75
    I found the issue. I some how accidently tied the "Station" combobox field back to the Station field on the Users Table. So when the form would close it would save that record.

    all fixed! Just had to do it step by step to see where it was getting put in at.


    Thanks.

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

Similar Threads

  1. Inserting records into MSWord table
    By riggsdp in forum Programming
    Replies: 5
    Last Post: 12-16-2014, 09:00 AM
  2. inserting records into word from access
    By sssandhya89 in forum Access
    Replies: 1
    Last Post: 03-22-2013, 11:33 AM
  3. Inserting records in multiple tables
    By Nikos in forum Database Design
    Replies: 8
    Last Post: 02-17-2012, 02:35 PM
  4. Inserting multiple records into a table
    By New2Access in forum Programming
    Replies: 1
    Last Post: 07-07-2011, 09:18 PM
  5. Inserting records into tables with autonumber
    By LAazsx in forum Import/Export Data
    Replies: 1
    Last Post: 12-13-2010, 11: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