Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use a passed value (from previous form) to populate a new form

Status
Not open for further replies.

ojasg

Technical User
Mar 19, 2010
30
DE
I have multiple forms. They need to be seperate.

- The first form asks the user to eneter an order number in a combo box and populates relevant info on the form from the table. On this form user is just selecting an order number to look at.
- The user then presses a button that closes the first form, and opens the second form.

What I am struggling with -
When I open the second form I want it to open the same record that was open in the previous form.

I am not a pro with VBA .. but can decode some basic stuff.
I tried using OpenArgs to pass the order number as a string.I am able to do that, but not able to populate relevant info on the second form. The second form displays the first record and actually changes the order number on this first record to the order number that was passed from the previous form.

I sincerely thank you all in advance.. This forum has helped me a lot ..
 

Can you show the code you're using to open the 2nd form?


Randy
 
Look at the "where" parameter of the Docmd.Open form method.
 
*CODE ON FORM ONE FOR BUTTON CLICK EVENT*
Private Sub cmdStartWorkOrder_Click()
Dim i As String
i = WorkOrder.Value
SetupStartTime.Value = Now() /For something else - Ignore
DoCmd.Close acForm, "frmOrderScanAndConfirm24M20"
DoCmd.OpenForm "frmSetupDisplayClock", , , , , , i
End Sub

*CODE ON FORM LOAD EVENT OF FORM TWO*
Private Sub Form_Load()
Dim i As String
i = CStr(Me.OpenArgs)
WorkOrder.Value = i
End Sub


The Above code passes the value.. but I want to use the passed value to populate remaining fields on form 2.
 

As soon as you close the first form, the variable (i) is gone. It no longer has a value.
Try opening the second form before you close the first. It might even be better to hide the first form, rather than closing it.
Either way, you need to open the second form while (i) still has a value.

Randy
 
randy700 said:
[blue] As soon as you close the first form, the variable (i) is gone. It no longer has a value.[/blue]
This is not true! [surprise]
TheAceMan1 said:
[blue]A form is not fully closed until all active code is finished running! ... even though the form is removed from the screen! [purple]This includes any traversing thru subs & functions![/purple][/blue]
As an example consider the following code:
Code:
[blue]Private Sub cmdStartWorkOrder_Click()
   Call Sub1
   MsgBox "cmdStartWorkOrder On Close - Active"
End Sub

Public Sub Sub1()
   Call Sub2
   MsgBox "Sub1 On Close - Active"
End Sub

Public Sub Sub2()
   Call Sub3
   MsgBox "Sub2 On Close - Active"
End Sub

Public Sub Sub3()
   Dim I As Long
   
   I = 1000
   
   DoCmd.Close acForm, "frmClients", acSaveNo
   [green]'The form is removed from the screen but code continues to run.
   '*** I is still active! ***[/green]
   
   [green]'Open the 2nd form[/green]
   DoCmd.OpenForm "frmCustomers"

End Sub
[green]'The routine is finished but subs are still active!
'The code finishes normally until the initial sub is completed.
'In reverse order you'll get the messages:
'   Sub2 On Close - Active
'   Sub1 On Close - Active
'   mdStartWorkOrder On Close - Active[/green]
[green][b]'   The form is now fully closed[/b][/green][/blue]
This can be proven by setting a break point and single stepping.

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
How are ya ojasg . . .

Two things:
[ol][li][blue]OpenArgs[/blue] is a [purple]String[/purple] datatype.[/li]
[li]You should be setting the focus to the record with the new [blue]WorkOrder[/blue]
Code:
[blue]Private Sub Form_Load()
   DoEvents
   Me.Recordset.FindFirst "[WorkOrder] = '" & Me.OpenArgs & "'"
End Sub[/blue]
[/li][/ol]
[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thanks a lot guys for your suggestions..I was finally able to get it to work.
This is what worked -->
--------------------------------------------------------
*CODE ON FORM ONE FOR BUTTON CLICK EVENT*
Private Sub cmdStartWorkOrder_Click()
'Initiate the setup timer
SetupStartTime.Value = Now() DoCmd.OpenForm "frmSetupDisplayClock", , , , , , WorkOrder.Value
DoCmd.Close acForm, "frmOrderScanAndConfirm24M20"
End Sub

*CODE ON FORM LOAD EVENT OF FORM TWO*
Private Sub Form_Load()
DoEvents
Me.Recordset.FindFirst "[WorkOrder] = " & Me.OpenArgs
End Sub
----------------------------------------------------------

I am now able to pass a value from one form and use it on a new form to open a specific record.

AceMan.. you really aced this one. Randy .. you are right about form close events, the code does not work if I close the form one before opening form two.. Thanks for that. MajP .. thanks for your input as well.


 
You can do it this way but most people would simply do

DoCmd.OpenForm "frmSetupDisplayClock", , ,"[WorkOrder] = " & workOrder.value

Do not know why you want to use open args and the additional code.
 
hey MajP..
Thats an easier solution..I guess I did not understand it the first time you tried to explain. I used it and it works just fine!
Thank you very much!
 
However, there is a difference in functionality and it depends what you want to do.
1) In the openargs case, you open the form with all the records and then go to the specific record. So all records are available to navigate to
2)with the where argument method it opens a filtered set to only the record/s that match your criteria
 
Ok..
Actually I need the Case 2..
I don't need any other record to be accessible. This works good for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top