automatically transfer a field when form opens in add mode

grapevine

Registered User.
Local time
Today, 23:11
Joined
Feb 21, 2009
Messages
39
I have a supplier form (frmsuppliers) where I add the information for new suppliers, I want users to be able to click on a button to open the contacts form frmcontacts in add mode with the name of the supplier that was shown automatically shown on the screen. The following code opens the form with the correct supplier showing, but the form shows the existing records and when I either click the add button, the supplier id stops showing the correct supplier.

I want to avoid users having to type in this information, how can I change the code to open in add mode as everyway I have tried this it seems to still retain the connection, but the supplier Id is not automatically populated. Any help gratefully received.


Code:
Private Sub AddNewContact_Click()
If IsNull(Me.SupplierID) Then Exit Sub
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenForm "frmcontacts", acNormal, , "[SupplierID]=" & Me.SupplierID
 
End Sub
[\code]
 
Use the OpenArgs property of the OpenForm method to pass the SupplierID to your form frmcontacts, so change your code to;
Code:
Private Sub AddNewContact_Click()
If IsNull(Me.SupplierID) Then Exit Sub
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenForm "frmcontacts", acNormal, , "[SupplierID]=" & Me.SupplierID, [B][COLOR="Magenta"], , Me.SupplierID[/COLOR][/B]

End Sub
Now in your form frmcontacts as part of the button to add a supplier put;
Code:
     Me.SupplierID = Openargs
You could put some validation in your button to ensure that OpenArgs holds a valid value.
 
"Now in your form frmcontacts as part of the button to add a supplier put;"...


thank you for your reply and I have incorporated the first part of your reply, but I am sorry, but what button are you referring to? - do you mean the field that holds the supplier ID as I do not have a button to add a supplier in the contacts form.
Many thanks
 
OK your form is currently opening with a filtered record set to show only records with SupplierID. Now I'm not sure how you are adding new records. Are you using a customs button or the native Access form Navigation buttons. If it's the latter then put your code in the Form's On Current event. You'd want to check if you where on a new record, and that OpenArgs held a valid value or not so your code might look like;
Code:
If Not IsNull(OpenArgs) And Me.NewRecord = True Then
     Me.SupplierID = Openargs
End If
This code assumes that you are not multitasking your form, and that this is the only thing you will be passing in the OpenArgs.
 

Users who are viewing this thread

Back
Top Bottom