Opening Form and setting variable

R

Rose B

I am sure (hope) that there is an easy way of doing what I want to do.....
but I cannot find it!

I have a form (form A) with a number of buttons all of which, when clicked,
need to open another form (form B) and set a variable used by that form (form
B) to a particular value - let's call it myVariable. I have defined
myVariable as a Public String and set it in the On Click event of the
button(s) in form A. It is first used in the On Open event of form B. Form B
is unbound.

Is anyone able to help?
 
J

Jack Leach

Not quite sure what you're looking for here, but usually to pass a variable
to a form, you would use the OpenArgs of the DoCmd.OpenForm method. Granted,
the way you described should work fine (I think the public variable would
need to be in a standard module, not a form module), but that's generally a
waste of resources if you don't have to have it public. Try this:


(from form A cmdbutton click)

Private Sub Command_Click()
DoCmd.OpenForm "frmName", , , , , , "valuetopass"
End Sub

(from form B Open)
Private Sub Form_Open(Cancel As Integer)
Dim SomeVariable As String
If Len(Nz(Me.OpenArgs), "") <> 0 Then
SomeVariable = Me.OpenArgs
End If
End Sub

This should effectively pass the required value to the opening form without
the use of a public variable.

hth

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 
R

Rose B

I am getting a syntax error on the

If Len(Nz(Me.OpenArgs), "") <> 0 Then

However, I omitted the If statement and it worked great - thanks for that!

(If you know why the syntax error is happening that would be of interest)
 
J

Jack Leach

Sorry.. didn't proofread my code. I closed the Nz function early. Corrected:

If Len(Nz(Me.OpenArgs, "")) <> 0 Then

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
R

Rose B

Thanks Jack - v much appreciated

Jack Leach said:
Sorry.. didn't proofread my code. I closed the Nz function early. Corrected:

If Len(Nz(Me.OpenArgs, "")) <> 0 Then

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
J

Jack Leach

No problem.

For the record, here's a description of what's going on:

The Len() function returns the length of a string
The Nz() function tests for null and sets the value to something you
specificy (just in this line, not the variable itself).

So in the Open event, if the for was opened without an openarg, you would
get an error on Null. Nz converts the null to a zero-length string, Len
makes sure theres something in the string before it processes the condition.

This is also a great way to prevent users from opening a form if no open arg
is supplied (if you only want to form to open from your code in certain
spots). Set Cancel to true if the openarg is null, and the form won't open.

If Len(Nz(Me.Openargs, "")) = 0 Then Cancel = True


Going backward, sending a value back from a form being closed, is a bit
trickier. Unfortunately access doesn't provide a CloseArg (it can't
really...). The general practice here is to hide the form (opened as
acDialog), and then get the value and close the hidden dialog from the
mainform.
--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
R

Rose B

Thanks Jack - the explanation is really helpful.

Jack Leach said:
No problem.

For the record, here's a description of what's going on:

The Len() function returns the length of a string
The Nz() function tests for null and sets the value to something you
specificy (just in this line, not the variable itself).

So in the Open event, if the for was opened without an openarg, you would
get an error on Null. Nz converts the null to a zero-length string, Len
makes sure theres something in the string before it processes the condition.

This is also a great way to prevent users from opening a form if no open arg
is supplied (if you only want to form to open from your code in certain
spots). Set Cancel to true if the openarg is null, and the form won't open.

If Len(Nz(Me.Openargs, "")) = 0 Then Cancel = True


Going backward, sending a value back from a form being closed, is a bit
trickier. Unfortunately access doesn't provide a CloseArg (it can't
really...). The general practice here is to hide the form (opened as
acDialog), and then get the value and close the hidden dialog from the
mainform.
--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top