Results 1 to 3 of 3
  1. #1
    NISMOJim is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2010
    Posts
    273

    Add if not in table

    I've been away from Access for about a year, and have forgotten everything I didn't know too well to begin with. Here's what I have...



    tblEmp is full of employee names and other information. frmReq is my main form for completing a parts requisition, which has a record source of tblReq. On frmReq is a combo box (cboName with a control source of ReqName to be saved into tblReq) that drops down the list of names of all users, and their related information will display in other textboxes via DLookups in those textboxes. cboName uses the row source of tblEmp.

    All of this works great, with the exception of two problems...

    A new employee that isn't in the table yet will need to complete the information in all of the textboxes and save the record in tblEmp. I believe this can be done with something like
    CurrentDb.Execute "UPDATE tblEmp SET tblEmp.EmpName = " & Me.cboName
    and so on for each field.

    The other problem comes when a user is in the table, but needs to edit one of the other fields from tblEmp. The code needs to determine if the employee name is already in the table, so a duplicate record isn't created for that employee.
    Once determined that his record is in the table, it will only need to update the fields that need editing.

    Here is my thought process...

    If the name typed in the combobox, or selected from the dropdown list is already in tblEmp, then only update the fields that are changed on the form.
    If nothing is changed, only add the name in a new record for tblReq (other fields further down the form will have a control source of tblReq as part of this record.
    If the name is not already in tblEmp, then the name along with the other fields need to be added as a new record.

    I'm guessing this will be a loop through the records, then an if statement, but it's beyond anything I know how to do.

    Thanks for any & all help!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    No, you don't add 1 field at a time with code.
    You open a form and the user enters ALL fields for that 1 employee record. No code needed.

    now, you can start with a search to see if the employee already exists. If no records found,open the entry screen to add new record in the docmd.OpenForm command.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    I agree with ranman256.

    Check for Employee existence first. If not found, you need an Append(INSERT) query to add/insert a new Record in the table.

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

Similar Threads

  1. Replies: 1
    Last Post: 03-31-2016, 03:33 AM
  2. Replies: 4
    Last Post: 08-30-2012, 07:58 PM
  3. Replies: 8
    Last Post: 03-22-2012, 08:48 AM
  4. Replies: 2
    Last Post: 08-01-2011, 11:35 PM
  5. Replies: 2
    Last Post: 10-27-2009, 07:09 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