How to pass a variable from one form to another conditionally
I have a form, which can be accessed 2 different ways.
The first is from another form, which will pass the SupplierID to it, by creating a filter. This will list all items for that Supplier. Then when I add a record in that form, I need to reference that SupplierID to use as a foreign key.
But I also access that form direct from a menu, so no data is passed to it. Therefore, if I want to add a record, then user will need to enter the SupplierID.
So, how do I let the form know where it was accessed from, so that it knows whether to take the SupplierID from another form or by user input? Do I check whether that form is open and use that form's SupplierID, or is there a neater way?
I like to add a semi-colon to the beginning of me.OpenArgs to do two things.
1. I don't have to deal with me.OpenArgs being NULL
2. It results in myArray(0) being blank, so I can use a 1 based array, not zero
The key is to 1) know the sequence of the arguments you are passing to the form so that you can reference specific elements of the array. Using Jim's example of:
Split() would return:
myArray(0): this would be an empty string
myArray(1) : ADD
myArray(2): SETCTRLTODATA=txtCustomerID:123
myArrY(3): EXITTOFORM=frmLoad
you could do something like the following to identify the control that you want to push data into, and the value for that control.
This can be a bit more complicated, which is why I like to use the method I described. You could also use a combination of the two, by simply passing in the name of the form used to call the new form. To open the second form, the syntax might look like:
Open in new window
I like to add a semi-colon to the beginning of me.OpenArgs to do two things.1. I don't have to deal with me.OpenArgs being NULL
2. It results in myArray(0) being blank, so I can use a 1 based array, not zero
The key is to 1) know the sequence of the arguments you are passing to the form so that you can reference specific elements of the array. Using Jim's example of:
"ADD;SETCTRLTODATA=txtCust
This might actually look like the following with an actual CustomerID
"ADD;SETCTRLTODATA=txtCust
Split() would return:
myArray(0): this would be an empty string
myArray(1) : ADD
myArray(2): SETCTRLTODATA=txtCustomerI
myArrY(3): EXITTOFORM=frmLoad
you could do something like the following to identify the control that you want to push data into, and the value for that control.
Open in new window
This can be a bit more complicated, which is why I like to use the method I described. You could also use a combination of the two, by simply passing in the name of the form used to call the new form. To open the second form, the syntax might look like:DoCmd.OpenForm "frmCustomerAddOnly", acNormal, , , acFormEdit, acDialog, me.name
Then, in the Form_Load event, you might use:
Open in new window