Passing values to the form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How would I pass values to the form I would open from another form's button
click event?
for eg.
I have a formA(with buttonA) and formB.I would like to pass variables values
x and y from formA to formB when I click buttonA.
 
When you use the DoCmd.OpenForm method, one of the things you can pass is a
value called OpenArgs.

When the form opens, you can check whether OpenArgs has any values.

To pass multiple values, you have to concatenate them into a single value,
then parse them apart when you read the value in the next form.

I'll often do something like:

strOpenArgs = "x=" & x & ";y=" & y
DoCmd.OpenForm "MyForm", OpenArgs:= strOpenArgs

then, in the Open event of MyForm, have something like:

Dim intLoop As Integer
Dim varOptions as Variant
Dim varValue as Variant

If IsNull(Me.OpenArgs) = False Then
varOptions = Split(Me.OpenArgs, ";")
For intLoop = LBound(varOptions) To UBound(varOptions)
varValue = Split(varOptions(intLoop), "=")
If UBound(varValue) > 0 Then
Select Case varValue(0)
Case "x"
x = varValue(1)
Case "y"
y = varValue(1)
End Select
If UBound(varValue) > 0 Then
Next intLoop
End If
 
Anand,
As long as you leave the 1st form open, you can use the OpenArgs argument to notify
Form2 to get those values.

Private Sub CmdOpenForm2_Click()
DoCmd.OpenForm "Form2", , , , , , "SendValues"
End Sub

On Form2 On Current event....

Private Sub Form_Current()
If OpenArgs = "SendValues" Then
Me.FieldX = Forms!frmForm1!FieldX
Me.FieldY = Forms!frmForm1!FieldY
End If
End Sub
 
Al,
I'm using your code to set up my OpenArgs argument but I have one question.
How do I keep this data in the specific record on Form2? Or will it stay
there even if I opened this form from the main menu? The reason I ask is
because there are two ways to open Form2 in my db. You can either open it
from the main menu and enter a record or open it from another form and add a
record. The only difference is when you open it from the main menu it does't
need any data from Form1. If you open it from Form1 then data is passed to
it. Am I making sense here? How do I keep the data there no matter which way
I open the form.
Thanks

Al Camp said:
Anand,
As long as you leave the 1st form open, you can use the OpenArgs argument to notify
Form2 to get those values.

Private Sub CmdOpenForm2_Click()
DoCmd.OpenForm "Form2", , , , , , "SendValues"
End Sub

On Form2 On Current event....

Private Sub Form_Current()
If OpenArgs = "SendValues" Then
Me.FieldX = Forms!frmForm1!FieldX
Me.FieldY = Forms!frmForm1!FieldY
End If
End Sub
--
hth
Al Camp
Candia Computer Consulting - Candia NH
http://home.comcast.net/~cccsolutions
 
Secret,
This will not effect your opening the Form2 from the main menu. The OpenArgs value is
only used by Form1 to notify Form2 of this special action.
When opening Form2 from the amin menu, there will no OPenArgs value, therefore Form2s
If OpenArgs = will fail.
You wrote...
How do I keep this data in the specific record on Form2?
Not sure I understand what you mean by that... if the value of FieldX and Y from Form1
are passed to bound fields in Form2 they are saved to the table, just like any field.
When the MainMenu opens the form later... those values will still be there.

But...on second thought though... it may be better to put my code in the "OnOpen" event
of Form2This way, the test for OpenArgs only occurs once, when the form Opens... not every
time a record on Form2 is "Current"
--
hth
Al Camp
Candia Computer Consulting - Candia NH
http://home.comcast.net/~cccsolutions


, so it only fires once... when the form is opened, and not every time a record is
"Current".
Secret Squirrel said:
Al,
I'm using your code to set up my OpenArgs argument but I have one question.
How do I keep this data in the specific record on Form2? Or will it stay
there even if I opened this form from the main menu? The reason I ask is
because there are two ways to open Form2 in my db. You can either open it
from the main menu and enter a record or open it from another form and add a
record. The only difference is when you open it from the main menu it does't
need any data from Form1. If you open it from Form1 then data is passed to
it. Am I making sense here? How do I keep the data there no matter which way
I open the form.
Thanks
 
Hi Al,
Thanks for the response. I will try to work out everything and see how it
works. I do have one more question though. When opening Form2 via Form1 is
there a way to filter the records so that the only record they can access
this way is the one they create? The only way to access other records on
Form2 would be from accessing it from the main menu.

Thanks
 
Well it will be to add new records but it will also be used to edit an
existing record. Once I create a new record that is tied to a record on Form1
then if I want to go back and edit it I want it to open/filter to that record
at all times when accessing Form2 from Form1.
 
Douglas, I followed your method and wrote code something like this-

in cmdGenerateReport_Click event in form frmReports --
Dim strArgs As String
strArgs = dtFromDate & ";" & dtToDate
DoCmd.OpenReport "rptDailyAttendance_Brief2",
acViewPreview, , "Attendance_Date between #" & dtFromDate & "# and #" &
dtToDate & "#", , strArgs

in Report_Open event --
Dim varValues As Variant
Dim dtFromDate As Date
Dim dtToDate As Date
If IsNull(Me.OpenArgs) = False Then
varValues = Split(Me.OpenArgs, ";")
dtFromDate = varValues(0)
dtToDate = varValues(1)
Me.txtFromDate = dtFromDate ''' getting error here
Me.txtToDate = dtToDate
End If

txtFromDate and txtToDate are unbound fields in the report

I am getting an error at Me.txtFromDate=....The error says -" You can't
assign a value to this object."
I am able to retrieve the values of the FromDate and ToDate from the form
frmReports into the report_open event but I am unable to assing these values
to the txtFromDate and txtToDate text fields of the report.How do I do this?
-------------
Anand Vaidya
I'm here to know.


Douglas J Steele said:
When you use the DoCmd.OpenForm method, one of the things you can pass is a
value called OpenArgs.

When the form opens, you can check whether OpenArgs has any values.

To pass multiple values, you have to concatenate them into a single value,
then parse them apart when you read the value in the next form.

I'll often do something like:

strOpenArgs = "x=" & x & ";y=" & y
DoCmd.OpenForm "MyForm", OpenArgs:= strOpenArgs

then, in the Open event of MyForm, have something like:

Dim intLoop As Integer
Dim varOptions as Variant
Dim varValue as Variant

If IsNull(Me.OpenArgs) = False Then
varOptions = Split(Me.OpenArgs, ";")
For intLoop = LBound(varOptions) To UBound(varOptions)
varValue = Split(varOptions(intLoop), "=")
If UBound(varValue) > 0 Then
Select Case varValue(0)
Case "x"
x = varValue(1)
Case "y"
y = varValue(1)
End Select
If UBound(varValue) > 0 Then
Next intLoop
End If
 
Back
Top