简体   繁体   中英

Access Form Go To Record

In Access, I have a main form with a listbox. The listbox is used to navigate to different records on the main form. The main form also has a button with the following code to open a dialog form on which I add a new record.

Private Sub New_Btn_Click()
    DoCmd.OpenForm "New Issue", , , , acFormAdd, acDialog
    Requery

    Me.ID_Box.SetFocus
End Sub

After I close the dialog form, I want the listbox to update and to select the new record that added on the "New Issue" form AND I want the main form to navigate to the new record.

I cannot figure out how to make either of these actions occur. Please help me find the best method for these steps.

Assuming the listbox has a rowsource, you can requery the ListBox to show the new record like this: ListBox1.Requery

Then to move the 'main' form to the latest record (assuming the ID/PK is an autonumber field, which is how the form recordsource is sorted): DoCmd.GoToRecord , "MainFormNameHere",acLast

If you don't have an ordered recordset, you can use

  Me.Recordset.FindFirst "PrimaryKeyField = " & NewRecordPK

Where NewRecordPK is a variable with the new records primary key stored.

You could call this from the dialog if you like. Just make sure you save the record first using something like Docmd.Save Then,

Forms!frmMainForm.Form.Recordset.FindFirst "PrimaryKeyField = " & Me.PrimaryKeyField

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM