PC Review


Reply
Thread Tools Rate Thread

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

 
 
every1luvsVB via AccessMonster.com
Guest
Posts: n/a
 
      1st Jan 2007
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?

--
Message posted via http://www.accessmonster.com

 
Reply With Quote
 
 
 
 
=?Utf-8?B?R2Vyd2luIEJlcmVudHNjaG90?=
Guest
Posts: n/a
 
      1st Jan 2007
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)

--
Gerwin Berentschot
(E-Mail Removed).(nospam)
Mr. Access - Access development and training



"every1luvsVB via AccessMonster.com" wrote:

> 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?
>
> --
> Message posted via http://www.accessmonster.com
>
>

 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      1st Jan 2007
"every1luvsVB via AccessMonster.com" <u18005@uwe> wrote in message
news:6ba6991e03231@uwe
> 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.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


 
Reply With Quote
 
Marshall Barton
Guest
Posts: n/a
 
      1st Jan 2007
every1luvsVB via AccessMonster.com wrote:
>- 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

--
Marsh
MVP [MS Access]
 
Reply With Quote
 
every1luvsVB via AccessMonster.com
Guest
Posts: n/a
 
      2nd Jan 2007
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!

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...oding/200701/1

 
Reply With Quote
 
=?Utf-8?B?c3dhcw==?=
Guest
Posts: n/a
 
      14th Mar 2007

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


"Marshall Barton" wrote:

> every1luvsVB via AccessMonster.com wrote:
> >- 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
>
> --
> Marsh
> MVP [MS Access]
>

 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      14th Mar 2007
In news:C2779CEF-CB3C-4552-B9F3-(E-Mail Removed),
swas <(E-Mail Removed)> wrote:
> 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.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


 
Reply With Quote
 
=?Utf-8?B?c3dhcw==?=
Guest
Posts: n/a
 
      15th Mar 2007
Thanks Dirk.

I should have known better.


swas

"Dirk Goldgar" wrote:

> In news:C2779CEF-CB3C-4552-B9F3-(E-Mail Removed),
> swas <(E-Mail Removed)> wrote:
> > 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.
>
> --
> Dirk Goldgar, MS Access MVP
> www.datagnostics.com
>
> (please reply to the newsgroup)
>
>
>

 
Reply With Quote
 
=?Utf-8?B?c3dhcw==?=
Guest
Posts: n/a
 
      15th Mar 2007
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

"Dirk Goldgar" wrote:

> In news:C2779CEF-CB3C-4552-B9F3-(E-Mail Removed),
> swas <(E-Mail Removed)> wrote:
> > 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.
>
> --
> Dirk Goldgar, MS Access MVP
> www.datagnostics.com
>
> (please reply to the newsgroup)
>
>
>

 
Reply With Quote
 
Stefan Hoffmann
Guest
Posts: n/a
 
      15th Mar 2007
hi,

swas wrote:
> 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 <--
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reference server objects within custom templates? Agendum Microsoft ASP .NET 4 24th Apr 2010 03:51 PM
How to reference fields/objects on a form =?Utf-8?B?cmljaA==?= Microsoft Access Form Coding 2 24th Jul 2007 05:18 PM
Reference form objects from a module Andy.I Microsoft VB .NET 3 17th Jan 2007 01:34 PM
How to Dynamically Reference Objects on a Form? =?Utf-8?B?RXJpY0I=?= Microsoft Access Forms 2 27th Sep 2006 11:36 PM
Using strings in reference to form objects Chad Peacey Microsoft Access Form Coding 3 19th Sep 2003 08:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:41 AM.