Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / March 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
every1luvsVB - 01 Jan 2007 21:54 GMT
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?
Gerwin Berentschot - 01 Jan 2007 22:05 GMT
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)

Signature

Gerwin Berentschot
gerwin@mraccess.nl.(nospam)
Mr. Access - Access development and training

> Hi there,
>
[quoted text clipped - 20 lines]
> I expect I should not pass the button reference as a string but cannot figure
> out the correct syntax. Can anyone help?
Dirk Goldgar - 01 Jan 2007 22:11 GMT
> Hi there,
>
[quoted text clipped - 21 lines]
> 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.

Signature

Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Marshall Barton - 01 Jan 2007 22:20 GMT
>- 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
[quoted text clipped - 16 lines]
>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

Signature

Marsh
MVP [MS Access]

every1luvsVB - 02 Jan 2007 22:12 GMT
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!
swas - 14 Mar 2007 14:01 GMT
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

> >- 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
[quoted text clipped - 43 lines]
>
>     ctl.Enabled = False
Dirk Goldgar - 14 Mar 2007 19:35 GMT
> Hi All,
>
[quoted text clipped - 14 lines]
>
> 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.

Signature

Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

swas - 15 Mar 2007 13:18 GMT
Thanks  Dirk.

I should have known better.

swas

> > Hi All,
> >
[quoted text clipped - 42 lines]
> as a string index into the Controls collection, or pass an object
> reference to the control itself.  Don't mic the two.
swas - 15 Mar 2007 14:09 GMT
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

> > Hi All,
> >
[quoted text clipped - 42 lines]
> as a string index into the Controls collection, or pass an object
> reference to the control itself.  Don't mic the two.
Stefan Hoffmann - 15 Mar 2007 14:15 GMT
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 <--
Dirk Goldgar - 15 Mar 2007 16:20 GMT
> How can the current control on a form be passed to a subroutine? For
> example, in the form.dirty event:
[quoted text clipped - 3 lines]
> 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.

Signature

Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

swas - 15 Mar 2007 22:03 GMT
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

> > How can the current control on a form be passed to a subroutine? For
> > example, in the form.dirty event:
[quoted text clipped - 25 lines]
> user action, the form's ActiveControl and Screen.ActiveControl will be
> the same object.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.