Results 1 to 7 of 7
  1. #1
    Steven19 is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Jun 2020
    Location
    London
    Posts
    110

    Primary Key and Combo Box not synchronised

    I have the following table:

    tblTypeofIncident
    TypeofIncidentID - Primary Key
    TypeofIncident - Text



    I have a form just for this one table.

    The user should from a combo box select the type of incident, and click continue, my control button does a docmd, picks up the ID and takes it to the Foreign Key of the following form.

    However, the function works for the first value in the combo, but when selecting the second value, the value changes in the combo box, but the primary key doesn't change with the selection. All my code does then is to rename the text in the TypeofIncident field to value 2. I dont have issues with other ways of doing it, but I'm scratching my head over what should be something simple.

    Private Sub Closebtn_Click()
    DoCmd.OpenForm "FrmReportingAccident", , , "[TypeofIncidentID] = " & Me![TypeofIncidentID], , , Me!TypeofIncidentID
    DoCmd.Close acForm, Me.Name, acSaveNo
    End Sub

  2. #2
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    I don't get that trailing part and am surprised if you're not getting an error. Why not
    DoCmd.OpenForm "FrmReportingAccident", , , "[TypeofIncidentID] = " & Me![TypeofIncidentID]

    If the PK isn't changing then it must be because the control isn't updating but I would think that moving focus to a command button should take care of that. Step through your code and check the value of the control. Also recommend that you use Me. not Me!
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Also note that ", acSaveNo" is only for saving changes in the form design.....you have not changed the form design, so it is not needed.
    I use
    Code:
    DoCmd.Close acForm, Me.Name
    with nary a problem.

  4. #4
    Steven19 is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Jun 2020
    Location
    London
    Posts
    110

    Attached database for review

    Hi

    I've attached the database, Open MainMenu, and click New Accident / Injury / Near Miss this is how simple it should be.

    I have cleared lots of test data, but the rest of the database works fine, its just this section.

    Can you kindly have a look and see what the issue may be?

    Regards
    Attached Files Attached Files

  5. #5
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Try this:

    In form "FrmTypeofIncident", the name of the combo box is "TypeofIncident" (NOT TypeofIncidentID).

    In the code, you have
    Code:
    Private Sub Closebtn_Click()
        DoCmd.OpenForm "FrmReportingAccident", , , "[TypeofIncidentID] = " & Me.[TypeofIncidentID]
        DoCmd.Close acForm, Me.Name, acSaveNo
    End Sub
    plus you do not set "OpenArgs"

    The code would be
    Code:
    Private Sub Closebtn_Click()
        DoCmd.OpenForm "FrmReportingAccident", , , "[TypeofIncidentID] = " & Me.[TypeofIncident], , , Me.[TypeofIncident]
        DoCmd.Close acForm, Me.Name
    End Sub

    Next, I would make form "FrmTypeofIncident" an unbound form (delete the Record source value). The combo box does not need to be on a bound form since all you are doing is getting a value (the PK) from a combo box.
    You will also have to clear the combo box "Control Source".

    I don't understand why you are setting the default value of "[TypeofIncidentID]" in table tblAccidents (but I don't really need to understand ).



    Also, on form "FrmTypeofIncident", I would change the names of the two buttons.
    The button CLOSE I would rename from "Private Sub Command6_Click()" to "Private Sub Closebtn_Click()" and the button CONTINUE I would rename from "Private Sub Closebtn_Click()" to "Private Sub Continuebtn_Click()"

  6. #6
    Steven19 is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Jun 2020
    Location
    London
    Posts
    110
    Thank you.

    That worked excellently. All suggestions taken on board and updated.

    Much appreciated

  7. #7
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You're welcome..

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

Similar Threads

  1. Replies: 6
    Last Post: 04-06-2021, 08:54 AM
  2. Replies: 3
    Last Post: 11-16-2016, 12:40 PM
  3. Replies: 5
    Last Post: 03-08-2016, 12:28 PM
  4. Combo Box to change primary key #
    By Sharkun in forum Forms
    Replies: 2
    Last Post: 01-11-2011, 04:05 PM
  5. Combo boxes both primary keys to select record
    By Alexandre Cote in forum Forms
    Replies: 3
    Last Post: 08-10-2010, 07:11 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