Passing a form as a parameter

  • Thread starter Thread starter Laurel
  • Start date Start date
L

Laurel

I would like to pass my current form as a paramter to a function in a
module. The function looks like this and compiles OK, but I can't figure
out how to call it "Call subTotalScores(Me)" doesn't work. Message is
#2450 "<database> can't find form 'afrm_Form' referred to in a macro or
Visual Basic Code."

I've also tried making the parameter a string and passing Me.Name

If I explicitly reference my current form, I get the correct value (e.g.,
Forms!frmScoreEntry![Safety]) evaluates to 5, the value of that column on
the open form.

Public Sub subTotalScores(afrm_Form As Form)
Dim li_safety As Integer
li_safety = 0
li_safety = Forms!acfrm_Form![Safety]
End Sub

I've messed around with various syntaxes....
 
Unfortunately, it didn't work. I'm still getting "<database> can't find
form 'afrm_Form' referred to in a macro or Visual Basic Code."

I tried changing the offending line to : li_safety = afrm_Form![Safety],
but no luck.

BTW - Why do I need the "Set" in the assignment statement? Frequently it's
not required. What is the rule?


Pavel Romashkin said:
Perhaps

Dim Temp as Form
Set Temp = Me.Form
subTotalScores(Temp)

Pavel
I would like to pass my current form as a paramter to a function in a
module. The function looks like this and compiles OK, but I can't figure
out how to call it "Call subTotalScores(Me)" doesn't work. Message is
#2450 "<database> can't find form 'afrm_Form' referred to in a macro or
Visual Basic Code."

I've also tried making the parameter a string and passing Me.Name

If I explicitly reference my current form, I get the correct value (e.g.,
Forms!frmScoreEntry![Safety]) evaluates to 5, the value of that column on
the open form.

Public Sub subTotalScores(afrm_Form As Form)
Dim li_safety As Integer
li_safety = 0
li_safety = Forms!acfrm_Form![Safety]
End Sub

I've messed around with various syntaxes....
 
The syntax _should_ work, so I suspect VBA is just confusing itself. The
first thing I'd do is change the name of the argument:

Public Sub subTotalScores(theForm as Form)

then I'd try dropping the reference to the Forms collection:

li_safety = theForm!Safety


And to answer you question about Set, you need it whenever you're working
with objects rather than scalar variable -- a form or table, rather than a
String.


HTH

--
Rebecca Riordan, MVP

Designing Relational Database Systems
Microsoft SQL Server 2000 Programming Step by Step
Microsoft ADO.NET Step by Step

http://www.microsoft.com/mspress

Blessed are they who can laugh at themselves,
for they shall never cease to be amused...

Pavel Romashkin said:
Perhaps

Dim Temp as Form
Set Temp = Me.Form
subTotalScores(Temp)

Pavel
I would like to pass my current form as a paramter to a function in a
module. The function looks like this and compiles OK, but I can't figure
out how to call it "Call subTotalScores(Me)" doesn't work. Message is
#2450 "<database> can't find form 'afrm_Form' referred to in a macro or
Visual Basic Code."

I've also tried making the parameter a string and passing Me.Name

If I explicitly reference my current form, I get the correct value (e.g.,
Forms!frmScoreEntry![Safety]) evaluates to 5, the value of that column on
the open form.

Public Sub subTotalScores(afrm_Form As Form)
Dim li_safety As Integer
li_safety = 0
li_safety = Forms!acfrm_Form![Safety]
End Sub

I've messed around with various syntaxes....
 
Public Sub subTotalScores(afrm_Form As Form)
Dim li_safety As Integer
li_safety = 0
li_safety = Forms!acfrm_Form![Safety]

Rebecca's right.
Forms!acfrm_Form
is short for
Forms("acfrm_Form")
which is obviously wrong.

For this sort of reason I usually spell things out, e.g.

li_Safety = acfrm_Form.Controls("Safety").Value
 
Thanks to you and Rebecca. It works and I learned several extra things.

John Nurick said:
Public Sub subTotalScores(afrm_Form As Form)
Dim li_safety As Integer
li_safety = 0
li_safety = Forms!acfrm_Form![Safety]

Rebecca's right.
Forms!acfrm_Form
is short for
Forms("acfrm_Form")
which is obviously wrong.

For this sort of reason I usually spell things out, e.g.

li_Safety = acfrm_Form.Controls("Safety").Value
 
I think the original ? has been answered. As for SET, you use it when
the variable you are assigning to is an object.

So, if you declare
Dim A as String
then you assign to it like this
A = "My string"

But if you do
Dim Rst as ADODB.Recordset
then you want to do this
Set Rst = New ADODB.Recordset
and you're supposed to clean up at the end of your procedure:
Rst.Close ' Not all objects have Close method
Set Rst = Nothing

Cheers,
Pavel
Unfortunately, it didn't work. I'm still getting "<database> can't find
form 'afrm_Form' referred to in a macro or Visual Basic Code."

I tried changing the offending line to : li_safety = afrm_Form![Safety],
but no luck.

BTW - Why do I need the "Set" in the assignment statement? Frequently it's
not required. What is the rule?

Pavel Romashkin said:
Perhaps

Dim Temp as Form
Set Temp = Me.Form
subTotalScores(Temp)

Pavel
I would like to pass my current form as a paramter to a function in a
module. The function looks like this and compiles OK, but I can't figure
out how to call it "Call subTotalScores(Me)" doesn't work. Message is
#2450 "<database> can't find form 'afrm_Form' referred to in a macro or
Visual Basic Code."

I've also tried making the parameter a string and passing Me.Name

If I explicitly reference my current form, I get the correct value (e.g.,
Forms!frmScoreEntry![Safety]) evaluates to 5, the value of that column on
the open form.

Public Sub subTotalScores(afrm_Form As Form)
Dim li_safety As Integer
li_safety = 0
li_safety = Forms!acfrm_Form![Safety]
End Sub

I've messed around with various syntaxes....
 
Back
Top