问题
I have a report that lists Deposits, with the DepositID set as a hyperlink. OnClick of DepositID, it opens the Deposit Entry form to that particular record. However, the form opens to a new record instead of the one the user clicks on. I think it is because I have another bit of code that runs OnLoad of the form that goes to a new record, and it might be executing that after executing the OnClick code.
OnClick of DepositID from the report:
DoCmd.OpenForm "DepositForm", acNormal, "", "[DepositID]= " & [DepositID]
OnLoad of the Form:
DoCmd.GoToRecord , , acNewRec
Is the OnLoad code running after the OnClick code, and effectively not showing the specified record from the report? Not sure how to fix.
回答1:
I wouldn't be using the AddNewRecord in your OnLoad event.
If you're using the form for both edit/add purposes, you should have two command buttons that open the form - one for each purpose
Remove this line from Form_Load
DoCmd.GoToRecord , , acNewRec
Use the edit command you have right now exactly as is - or add EditMode
DoCmd.OpenForm "DepositForm", acNormal, "", "[DepositID]= " & [DepositID], acFormEdit
Add a new button to Add New Records as in:
DoCmd.OpenForm "DepositForm", acNormal, , , acFormAdd
来源:https://stackoverflow.com/questions/38729591/access-vba-open-form-to-specific-record-from-report