Autofill Field Based On Related Field In Previous Form

MattWhiteley

Registered User.
Local time
Today, 21:59
Joined
Jun 10, 2015
Messages
16
Ok, so I have my Assets form and the primary key is the ChargerID, in this form I have an "Add New Job For This Asset" button, which opens up the Jobs form at a new record. How do I make it so that the ChargerID field is automatically filled with whatever the previous record was instead of being blank. For example if I have Charger12345 open in the Asset form, I'd like to click the Add New Job button and it automatically have Charger12345 in the ChargerID field of the Jobs form.

Thanks.
 
Okay, I'm very new to VBA, I get what I'm reading there but I'm not sure how I'd go about manipulating that code to get the result I'd want. As I read it, that code says to open the Employees form at the specific record of Callahan by setting the name Callahan as a variable and searching the Employees table for the surname Callahan. I believe that's correct?

What I'm after is taking the primary key from the Asset form (ChargerID), setting that string as the variable (whatever it is, not just one specific ChargerID), opening the Jobs form at a new record and setting the ChargerID field in that form as the stored variable.
 
In the buttons click event where you open the "Add New Job" form, put in or change it to the below, (remember to change the "YourFormName" to the form name you use):
Code:
...
..
DoCmd.OpenForm "YourFormName", , , , , , Me.ChargerID
...
And in the OnCurrent event in the form you open, paste in the below:
Code:
Private Sub Form_Current()
  Me.ChargerID = Me.OpenArgs
End Sub
Else post a stripped version of your database with some sample data + name of the form in which you've the problem, (zip it).
 
Another way to do it without any code is to load the Jobs form as a subform of the Assets form and set the ChargerID fields as the Master/Child LinkFields properties.

This can even be done with the subform in a completely separate main form by entering the full reference (via the Forms collection) to the Assets form as the MasterLinkFields property of the subformcontrol.
 
In the buttons click event where you open the "Add New Job" form, put in or change it to the below, (remember to change the "YourFormName" to the form name you use):
Code:
...
..
DoCmd.OpenForm "YourFormName", , , , , , Me.ChargerID
...
And in the OnCurrent event in the form you open, paste in the below:
Code:
Private Sub Form_Current()
  Me.ChargerID = Me.OpenArgs
End Sub
Else post a stripped version of your database with some sample data + name of the form in which you've the problem, (zip it).
Thanks a bunch, that worked!

Slightly related question, so I won't start a new topic, I have a query that runs on ChargerID parameters, is there a way that I could have a button on the Asset form that would run that query with the current ChargerID as the parameter and open the form I have for that query?



Ignore, I've figured it out. :)
 
Last edited:

Users who are viewing this thread

Back
Top Bottom