Passing Parameters Into A Form [RESOLVED]
Hi guys,
I've been looking for a way to pass parameters into a form and then use that parameter in the subform once it's been passed. The situation is this - I have a few tables in the current database who's contents I'd like to display. I also have a main form with a command button. The idea is, once the button is clicked, a subform is opened that populates its text and list boxes with information from a particular table, depending on what value was passed in as a parameter (ie: DoComd.OpenForm <subformname>, OpenArgs:="First_Table" will open First_Table, DoCmd.OpenForm <subformname>, OpenArgs:="Second_Table" will open Second_Table etc. etc....)
My question is pretty general - how is this done?
The DoComd... OpenArgs example is something I found earlier but I've no idea how to get the subform to act based on the parameter that was passed into it.
Hope you can help,
Thanks
Re: Passing Parameters Into A Form
On load of the form you would need to check the me.OpenArgs.
Add this to an sql statement for the form and set the recordseource (I think) property to the new sql statement. Then it may require a me.refresh/me.requery to get the requested data.
Worth a try to see if it works. Post up if it does/doesn't work.
Re: Passing Parameters Into A Form
Thanks. Here's the latest -
I've passed a string parameter into the form, which I'm calling frmEdit. I have a button in frmEdit called CmdEnter. As this button's event procedure, I've instructed it to check Me.OpenArgs and display whatever the arguments are in a textbox, like so -
Dim str As String
str = "Display " & Me.OpenArgs
txtDisp.Text = Me.OpenArgs
It's not displaying the arguments at the moment. Am I on the right track here?
Re: Passing Parameters Into A Form
Show your exact DoCmd.OpenForm command
Try a messagebox in the open event of the new form ...
Re: Passing Parameters Into A Form
Hey! Your question was the solution to my problem! I was trying to set a Label in a Report, and it works if I pass an "OpenArgs" to the Report and use it to set the Label.Caption in the Report Open Event, but it does not work (on the 1st page) if I try to set the Label.Caption in the control Button click event!
Thanks, good luck, and good programming!
Re: Passing Parameters Into A Form
Here is my code that works ...
Code:
'>>> In Control Form Module
Private Sub Command0_Click()
Dim aWhere As String
Dim aStrArg As String
'Select a WHERE statement (from Global List) based on the Radio Box Group
aWhere = aDept(Me.Dpt_Chain.Value)
'Pass a String Argument to the Report to install in the Lable
aStrArg = "TEST 22"
'Open the Report and pass it a string ...
DoCmd.OpenReport "Price_1", acViewPreview, , aWhere, acWindowNormal, aStrArg
End Sub
'>>> In Report Code
Private Sub Report_Open(Cancel As Integer)
If Not IsNull(Me.OpenArgs) Then
Me.Controls("Dpt_Label").Caption = Me.OpenArgs
End If
End Sub
Re: Passing Parameters Into A Form
I've made some of the changes suggested here and I've studies the info available on the MSDN library. My code now reads like so -
VB Code:
'This is the code in the form that will open the second (or sub) form
Private Sub OpenEditForm()
Dim strRep As String
strRep = "Test Argument Passing"
Dim db As Database
Dim rs As Recordset
Dim sqlStr As String
Set db = CurrentDb()
DoCmd.SetWarnings (False)
DoCmd.OpenForm "frmEditMon", , , , , , strRep
End Sub
'This is the code in the subform
'CmdMonEnt is a command button
Private Sub CmdMonEnt_Click()
Dim str As String
str = "Display : " & Me.OpenArgs
'txtDisp is a textbox
txtDisp.Text = Me.OpenArgs
If Not IsNull(Me.OpenArgs) Then
MsgBox Me.OpenArgs
End If
End Sub
Everything compiles ok and there are no errors but the subform isn't displaying the argument string that I'm passing in to it. Any ideas?
Re: Passing Parameters Into A Form
DoCmd.OpenForm "frmEditMon", , , , , , strRep
IGNORE THIS ... I Confused OpenForm with OpenReport
I think you have 1 too many commas ... as I recall OpenArgs is the SIXTH parameter.
Re: Passing Parameters Into A Form
OOPS! OpenForm seems to have 1 more parameter than OpeReport ...
expression.OpenReport(ReportName, View, FilterName, WhereCondition, WindowMode, OpenArgs)
expression.OpenForm(FormName, View, FilterName, WhereCondition, DataMode, WindowMode, OpenArgs)
Put a message box in the OPEN Event for the subform ... I'll bet you'll find it there. Pass the parameter to a Public (Global) variable in the Open Event code and it will be available to the rest of the code for the form.
Re: Passing Parameters Into A Form
Thanks - that pretty much clinched it.
This is the line of code that opens the form
DoCmd.OpenForm "<form name>", , , , , , strArgument
And this is the code in the Subform that gets the parameter
Private Sub Form_Load()
strArg = Me.OpenArgs
MsgBox "Parameter : " & strArg
End Sub
Thanks for all the help.