How can I reference Form Objects in my custom Sub-Routine?

  • Thread starter every1luvsVB via AccessMonster.com
  • Start date
E

every1luvsVB via AccessMonster.com

Hi there,

I am having trouble referencing form objects in my custom-defined sub routine.


- In the calling statement, I pass the form as 'Me'; defining it as 'thisForm
As Form' in the custom sub's argument list. This is recognised in my
subroutine and is not a problem.

- In the calling statement, I also pass a reference to a button control on
this form as a subsequent argument. In this instance, I pass the control as
'Me!btnOpenReport'; and define it as 'strButtonName As String' in the custom
sub's argument list.

- When I then try to manipulate Me!btnOpenReport's properties from within my
custom subroutine, eg. via 'thisForm!strButtonName.enabled = False', I
receive the following error..

Run-time error '2465':

Microsoft Office Access can't find the field 'strButtonName' referred to in
your expression.

I expect I should not pass the button reference as a string but cannot figure
out the correct syntax. Can anyone help?
 
G

Guest

Declare the button as a control. Something like this:

sub Example (Thisform as form, Button as control)
Button.enabled=false
end sub

Call the sub like this:
Call Example (Me, NameOfTheButton)
 
D

Dirk Goldgar

every1luvsVB via AccessMonster.com said:
Hi there,

I am having trouble referencing form objects in my custom-defined sub
routine.


- In the calling statement, I pass the form as 'Me'; defining it as
'thisForm As Form' in the custom sub's argument list. This is
recognised in my subroutine and is not a problem.

- In the calling statement, I also pass a reference to a button
control on this form as a subsequent argument. In this instance, I
pass the control as 'Me!btnOpenReport'; and define it as
'strButtonName As String' in the custom sub's argument list.

- When I then try to manipulate Me!btnOpenReport's properties from
within my custom subroutine, eg. via 'thisForm!strButtonName.enabled
= False', I receive the following error..

Run-time error '2465':

Microsoft Office Access can't find the field 'strButtonName' referred
to in your expression.

I expect I should not pass the button reference as a string but
cannot figure out the correct syntax. Can anyone help?

You're right. You shouldn't be defining the argument as a string.
Instead, define it as one of the following

Access.CommandButton
Access.Control
Object

(listed in descending order of specificity). I would use the first one,
by preference, as that will give you the intellisense dropdown for the
properties and methods that apply specifically to the CommandButton
control.
 
M

Marshall Barton

every1luvsVB said:
- In the calling statement, I pass the form as 'Me'; defining it as 'thisForm
As Form' in the custom sub's argument list. This is recognised in my
subroutine and is not a problem.

- In the calling statement, I also pass a reference to a button control on
this form as a subsequent argument. In this instance, I pass the control as
'Me!btnOpenReport'; and define it as 'strButtonName As String' in the custom
sub's argument list.

- When I then try to manipulate Me!btnOpenReport's properties from within my
custom subroutine, eg. via 'thisForm!strButtonName.enabled = False', I
receive the following error..

Run-time error '2465':

Microsoft Office Access can't find the field 'strButtonName' referred to in
your expression.

I expect I should not pass the button reference as a string but cannot figure
out the correct syntax. Can anyone help?


If you call the procedure with a control reference AND the
procedure is declared as a value variable, then the
control's Value is passed as the argument. If you intended
to pass the name of the control, then you need to explicitly
use the control's Name property.

proc Me, Me!btnOpenReport.Name

but that passes the identical string as:

proc Me, "btnOpenReport"

In either of those cases, the procedure would use the
arguments this way:

thisForm(strButtonName).Enabled = False

With that explanation of your problem out of the way, I
suggest that if the form object is not used for other
things, it is usually better to declare the procedure as:

Sub myproc(ctl As Control)

and use this:

ctl.Enabled = False
 
E

every1luvsVB via AccessMonster.com

Guys,

Thanks heaps for the advice. The Control declaration works. And Marshall;
very good point; I do not need to pass the form reference at all to achieve
what I need to achieve.

Thanks for all your help!
 
G

Guest

Hi All,

I am trying to achieve a similar thing here, passing a reference to a
control to a subroutine. Following the logic in code of:

call subMySub ("ThisControl")

or

call subMySub (ThisControl.Name)

Private Sub subMySub (ctrl as Control)

ctrl.enabled = false

End Sub

I get a type mismatch when calling the sub. Have I missed something?


Thanks

swas
 
D

Dirk Goldgar

In
swas said:
Hi All,

I am trying to achieve a similar thing here, passing a reference to a
control to a subroutine. Following the logic in code of:

call subMySub ("ThisControl")

or

call subMySub (ThisControl.Name)

Private Sub subMySub (ctrl as Control)

ctrl.enabled = false

End Sub

I get a type mismatch when calling the sub. Have I missed something?

Either:

Call subMySub("ThisControl")

...

Private Sub subMySub(ControlName As String)

Me.Controls(ControlName).Enabled = False

End Sub

Or:

Call subMySub(ThisControl)

...

Private Sub subMySub(ctrl as Control)

ctrl.Enabled = False

End Sub

In other words, either pass the name of the control and use that name
as a string index into the Controls collection, or pass an object
reference to the control itself. Don't mic the two.
 
G

Guest

How can the current control on a form be passed to a subroutine? For example,
in the form.dirty event:

call subMySub(<Selected form control>)

Sorry but still getting over this one... I understand directly passing a
single control as explained below though.

swas
 
S

Stefan Hoffmann

hi,
How can the current control on a form be passed to a subroutine? For example,
in the form.dirty event:
You may use Screen.ActiveControl, but i assume you mean somthing like that:

Private Sub cmdButton_Click()

subMySub cmdButton

End Sub

In the case of referencing the calling control, you have to hard code it.

When you need it very often, you may take a look at mztools.com. This
VBA plugin has a some template functions.


mfG
--> stefan <--
 
D

Dirk Goldgar

In
swas said:
How can the current control on a form be passed to a subroutine? For
example, in the form.dirty event:

call subMySub(<Selected form control>)

Sorry but still getting over this one... I understand directly
passing a single control as explained below though.

The form has an ActiveControl property, so a procedure in the form's
module could be written like this:

Private Sub DoSomethingWithActiveControl()

With Me.ActiveControl

' ... do something ...

End With

End Sub

If your procedure is going to be in a standard module, not the form's
module, you can use Screen.ActiveControl instead of Me.ActiveControl.
You have to think carefully about the circumstances under which such a
procedure is going to be called, since Screen.ActiveControl could
theoretically be one thing and Forms!SomeForm.ActiveControl another.
However, in the Dirty event of a form, if the form is being dirtied by
user action, the form's ActiveControl and Screen.ActiveControl will be
the same object.
 
G

Guest

Thanks Stefan and Dirk.

I had actually scoured the form properties, controls collection etc...
looking for currentcontrol or similar. After reading your response I type
'me.' and the very frst intellisense property is ActiveControl...

The detailed responses are really appreciated.

swas
 

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