Passing a value from one form to another

  • Thread starter Thread starter DaveF
  • Start date Start date
D

DaveF

I have a "customer" form with a command button that when clicked opens up an
"order" form that displays the customers orders.

If the customer has an order, the "orders" form opens to display that
customer's orders. This is accomplished using the stLinkCriteria parameter
of the DoCmd.OpenForm command.

However, if the customer does not have any orders, the "order" form opens
with a blank record. But I want to automatically populate this form with
the customer's name.

If a customer does not have an existing order, when the create order button
is clicked on the "customer" form, I want to pass the customer's ID to the
"order" form so that when it opens to a blank record, the customer name can
be displayed in a dropdown box.

IOW, for an initial order, I want to prepopulate the "order" form with the
customer's ID.

In order to do this I need to pass the ID from the parent form to the child
form and use it to select a value in a drop down box.

Can anyone give me an idea how to do this?
 
Something like the following should work:

docmd.OpenForm "frmOrders"

Forms![FormName]![FieldName]=Me!customerID


You must first open the form first for this to work. Also,
don't close the parent form before you do this.

Kevin
 
I have a "customer" form with a command button that when clicked opens up an
"order" form that displays the customers orders.

If the customer has an order, the "orders" form opens to display that
customer's orders. This is accomplished using the stLinkCriteria parameter
of the DoCmd.OpenForm command.

However, if the customer does not have any orders, the "order" form opens
with a blank record. But I want to automatically populate this form with
the customer's name.

If a customer does not have an existing order, when the create order button
is clicked on the "customer" form, I want to pass the customer's ID to the
"order" form so that when it opens to a blank record, the customer name can
be displayed in a dropdown box.

IOW, for an initial order, I want to prepopulate the "order" form with the
customer's ID.

In order to do this I need to pass the ID from the parent form to the child
form and use it to select a value in a drop down box.

Can anyone give me an idea how to do this?
You can use the OpenArgs argument to pass the Customer Name from one
form to the next:

I assume the original form includes the CustomerID and the
CustomerName field.
DoCmd.OpenForm "FormName", , , stLinkCriteria , , , Me![CustomerName]

In the second form's Load event, you can check for the OpenArgs value:

If Not IsNull(Me.OpenArgs) Then
If IsNull([CustNameControl]) Then
[CustNameControl] = Me.OpenArgs
End If
End If
 
Thanks Fred. That works great.

There is just one glitch with a new record. If I create a new customer and
click the orders button, the ID is passed to the orders form, but the
customer name does not appear in the drop down box. However, if I use an
existing customer or create a new customer and move off the new customer and
then return, then the customers name will display in the dropdown box when
the orders form loads.

I think the reason for this behavior is that a brand new customer record has
not really been committed to the database when I call create order. I have
to move off the new customer and return for the new record to commit. Then
the customer's name will display when the orders form loads.

Do you know how I can commit a new customer record before calling the create
order?

Dave




I have a "customer" form with a command button that when clicked opens up an
"order" form that displays the customers orders.

If the customer has an order, the "orders" form opens to display that
customer's orders. This is accomplished using the stLinkCriteria parameter
of the DoCmd.OpenForm command

However, if the customer does not have any orders, the "order" form opens
with a blank record. But I want to automatically populate this form with
the customer's name.

If a customer does not have an existing order, when the create order button
is clicked on the "customer" form, I want to pass the customer's ID to the
"order" form so that when it opens to a blank record, the customer name can
be displayed in a dropdown box.

IOW, for an initial order, I want to prepopulate the "order" form with the
customer's ID.

In order to do this I need to pass the ID from the parent form to the child
form and use it to select a value in a drop down box.

Can anyone give me an idea how to do this?
You can use the OpenArgs argument to pass the Customer Name from one
form to the next:

I assume the original form includes the CustomerID and the
CustomerName field.
DoCmd.OpenForm "FormName", , , stLinkCriteria , , , Me![CustomerName]

In the second form's Load event, you can check for the OpenArgs value:

If Not IsNull(Me.OpenArgs) Then
If IsNull([CustNameControl]) Then
[CustNameControl] = Me.OpenArgs
End If
End If
 
Thanks Fred. That works great.

There is just one glitch with a new record. If I create a new customer and
click the orders button, the ID is passed to the orders form, but the
customer name does not appear in the drop down box. However, if I use an
existing customer or create a new customer and move off the new customer and
then return, then the customers name will display in the dropdown box when
the orders form loads.

I think the reason for this behavior is that a brand new customer record has
not really been committed to the database when I call create order. I have
to move off the new customer and return for the new record to commit. Then
the customer's name will display when the orders form loads.

Do you know how I can commit a new customer record before calling the create
order?

Dave




I have a "customer" form with a command button that when clicked opens up an
"order" form that displays the customers orders.

If the customer has an order, the "orders" form opens to display that
customer's orders. This is accomplished using the stLinkCriteria parameter
of the DoCmd.OpenForm command

However, if the customer does not have any orders, the "order" form opens
with a blank record. But I want to automatically populate this form with
the customer's name.

If a customer does not have an existing order, when the create order button
is clicked on the "customer" form, I want to pass the customer's ID to the
"order" form so that when it opens to a blank record, the customer name can
be displayed in a dropdown box.

IOW, for an initial order, I want to prepopulate the "order" form with the
customer's ID.

In order to do this I need to pass the ID from the parent form to the child
form and use it to select a value in a drop down box.

Can anyone give me an idea how to do this?
You can use the OpenArgs argument to pass the Customer Name from one
form to the next:

I assume the original form includes the CustomerID and the
CustomerName field.
DoCmd.OpenForm "FormName", , , stLinkCriteria , , , Me![CustomerName]

In the second form's Load event, you can check for the OpenArgs value:

If Not IsNull(Me.OpenArgs) Then
If IsNull([CustNameControl]) Then
[CustNameControl] = Me.OpenArgs
End If
End If
Dave,
You are correct, the new record has not been saved yet.
Add a line of code as the first entry in the command button event:

DoCmd.RunCommand acCmdSaveRecord
' The combo box is on this form I believe, not
' the form being opened?
' If so, you may also need to requery the combo box.
' Try it first without the next line of code
Me!ComboName.Requery
' if it works don't bother using it.
' Then open the other form
DoCmd.OpenForm ... etc.
 
Calling DoCmd.RunCommand acCmdSaveRecord before I load the orders form does
the job!

Thanks much for your help.

Dave
 
Back
Top