passing control name to sub and back

W

Wally

Hi,
I need to pass a control's name on a form to a sub
procedure. I can do this part, but in the Sub I need to
change this controls value. I'm trying to use the passed
control.name string something like this and of course it
doesn't work.

SUB UpdateMe(controlname as string, controlvalue as date)

if controlvalue > date() then
(here is the part that don't work)
form("myformname").controlname = date()
end if

END SUB

Then I call the Sub from the AfterUpdate event of the
control.

Call UpdateMe(me.control_1.name, me.control_1.value)


The name of the control is getting to the Sub but I just
can't use it to refer back to it to change it's value.

My code is much more complex than this example and I'm
wanting to do away with redundant code by not doing this
in the AfterUpdate event module of many controls.

Thanks for any tips anyone can suggest on this.

Wally
 
S

Sandra Daigle

Hi Wally,

Pass the control itself - this way you don't need a hardcoded reference to
the form name.

Sub UpdateMe(ctlIn as control, controlValue as date)
if ctlIn > date() then
ctlin=date()
endif
End sub

UpdateMe me.MyCtl

Also, just so you'll know, the way to use a variable as part of a control
reference is as follows:

dim strCtlName as string
strCtlName="MyCtl"

me.controls(strCtlName)

So in your code, you would have:

form("myformname").controls(controlname) = date()
 
D

dchendrickson

Wally,

I think the underlying culprit in your code is the use of
the 'dot' (.) operator instead of the 'bang' (!) operator
to reference a control on a form.

As an alternate to Sandra's approach, you might try this:

if controlvalue > date() then
(here is the part that don't work)
form("myformname")!controlname = date()
end if

With the structure of form("myformname").controlname,
your code is trying to find a method or property of
myform named controlname (which doesn't exist and causes
the problem). With the structure of form("myformname")!
controlname your code will recognize it a control named
controlname in the controls collection of the form myform.

Hope this helps you understand why your code wasn't
working. With that in mind, Sandra's approach of passing
a reference to the form to your Sub instead of the
individual properties fo the form is good.

-dc
-----Original Message-----
Hi Wally,

Pass the control itself - this way you don't need a hardcoded reference to
the form name.

Sub UpdateMe(ctlIn as control, controlValue as date)
if ctlIn > date() then
ctlin=date()
endif
End sub

UpdateMe me.MyCtl

Also, just so you'll know, the way to use a variable as part of a control
reference is as follows:

dim strCtlName as string
strCtlName="MyCtl"

me.controls(strCtlName)

So in your code, you would have:

form("myformname").controls(controlname) = date()


--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Hi,
I need to pass a control's name on a form to a sub
procedure. I can do this part, but in the Sub I need to
change this controls value. I'm trying to use the passed
control.name string something like this and of course it
doesn't work.

SUB UpdateMe(controlname as string, controlvalue as date)

if controlvalue > date() then
(here is the part that don't work)
form("myformname").controlname = date()
end if

END SUB

Then I call the Sub from the AfterUpdate event of the
control.

Call UpdateMe(me.control_1.name, me.control_1.value)


The name of the control is getting to the Sub but I just
can't use it to refer back to it to change it's value.

My code is much more complex than this example and I'm
wanting to do away with redundant code by not doing this
in the AfterUpdate event module of many controls.

Thanks for any tips anyone can suggest on this.

Wally

.
 
S

Sandra Daigle

Hi dc -

The bang vs. dot argument is an old one (Use google to search for "Bang vs.
Dot" for more on this) but in this case, the dot is fine - the problem is
that you can't use a variable within a reference (any reference) unless you
go through the collection and refer to it by the key for the member (the key
to the controls collection is the Control Name).

Since ControlName is a a parameter to the procedure, it will not work in the
following. Instead the code will try to find a control which is named
"ControlName" rather than a control which has the name that is the value of
the controlname parameter.

forms("myformname")!controlname = date()

This will work:

forms("myformname").controls(controlname) = date()

For that matter, we could have used a variable in place of "myformName"

dim strFormName as string
strFormName = "myFormName"
forms(strFormName).controls(controlName) = date()

FWIW, all of this seems more clear to me when we use tags on vba variable
names - then instead of ControlName as a variable, we would have
strControlName.

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Wally,

I think the underlying culprit in your code is the use of
the 'dot' (.) operator instead of the 'bang' (!) operator
to reference a control on a form.

As an alternate to Sandra's approach, you might try this:

if controlvalue > date() then
(here is the part that don't work)
form("myformname")!controlname = date()
end if

With the structure of form("myformname").controlname,
your code is trying to find a method or property of
myform named controlname (which doesn't exist and causes
the problem). With the structure of form("myformname")!
controlname your code will recognize it a control named
controlname in the controls collection of the form myform.

Hope this helps you understand why your code wasn't
working. With that in mind, Sandra's approach of passing
a reference to the form to your Sub instead of the
individual properties fo the form is good.

-dc
-----Original Message-----
Hi Wally,

Pass the control itself - this way you don't need a hardcoded
reference to the form name.

Sub UpdateMe(ctlIn as control, controlValue as date)
if ctlIn > date() then
ctlin=date()
endif
End sub

UpdateMe me.MyCtl

Also, just so you'll know, the way to use a variable as part of a
control reference is as follows:

dim strCtlName as string
strCtlName="MyCtl"

me.controls(strCtlName)

So in your code, you would have:

form("myformname").controls(controlname) = date()


--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Hi,
I need to pass a control's name on a form to a sub
procedure. I can do this part, but in the Sub I need to
change this controls value. I'm trying to use the passed
control.name string something like this and of course it
doesn't work.

SUB UpdateMe(controlname as string, controlvalue as date)

if controlvalue > date() then
(here is the part that don't work)
form("myformname").controlname = date()
end if

END SUB

Then I call the Sub from the AfterUpdate event of the
control.

Call UpdateMe(me.control_1.name, me.control_1.value)


The name of the control is getting to the Sub but I just
can't use it to refer back to it to change it's value.

My code is much more complex than this example and I'm
wanting to do away with redundant code by not doing this
in the AfterUpdate event module of many controls.

Thanks for any tips anyone can suggest on this.

Wally

.
 
G

Guest

Sandra,

You are absolutely right. I misinterpreted
the 'controlname' as psuedospeak for the actual control
name, not a variable holding the name of the control.

-dc
-----Original Message-----
Hi dc -

The bang vs. dot argument is an old one (Use google to search for "Bang vs.
Dot" for more on this) but in this case, the dot is fine - the problem is
that you can't use a variable within a reference (any reference) unless you
go through the collection and refer to it by the key for the member (the key
to the controls collection is the Control Name).

Since ControlName is a a parameter to the procedure, it will not work in the
following. Instead the code will try to find a control which is named
"ControlName" rather than a control which has the name that is the value of
the controlname parameter.

forms("myformname")!controlname = date()

This will work:

forms("myformname").controls(controlname) = date()

For that matter, we could have used a variable in place of "myformName"

dim strFormName as string
strFormName = "myFormName"
forms(strFormName).controls(controlName) = date()

FWIW, all of this seems more clear to me when we use tags on vba variable
names - then instead of ControlName as a variable, we would have
strControlName.

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Wally,

I think the underlying culprit in your code is the use of
the 'dot' (.) operator instead of the 'bang' (!) operator
to reference a control on a form.

As an alternate to Sandra's approach, you might try this:

if controlvalue > date() then
(here is the part that don't work)
form("myformname")!controlname = date()
end if

With the structure of form("myformname").controlname,
your code is trying to find a method or property of
myform named controlname (which doesn't exist and causes
the problem). With the structure of form("myformname")!
controlname your code will recognize it a control named
controlname in the controls collection of the form myform.

Hope this helps you understand why your code wasn't
working. With that in mind, Sandra's approach of passing
a reference to the form to your Sub instead of the
individual properties fo the form is good.

-dc
-----Original Message-----
Hi Wally,

Pass the control itself - this way you don't need a hardcoded
reference to the form name.

Sub UpdateMe(ctlIn as control, controlValue as date)
if ctlIn > date() then
ctlin=date()
endif
End sub

UpdateMe me.MyCtl

Also, just so you'll know, the way to use a variable as part of a
control reference is as follows:

dim strCtlName as string
strCtlName="MyCtl"

me.controls(strCtlName)

So in your code, you would have:

form("myformname").controls(controlname) = date()


--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.


Wally wrote:
Hi,
I need to pass a control's name on a form to a sub
procedure. I can do this part, but in the Sub I need to
change this controls value. I'm trying to use the passed
control.name string something like this and of course it
doesn't work.

SUB UpdateMe(controlname as string, controlvalue as date)

if controlvalue > date() then
(here is the part that don't work)
form("myformname").controlname = date()
end if

END SUB

Then I call the Sub from the AfterUpdate event of the
control.

Call UpdateMe(me.control_1.name, me.control_1.value)


The name of the control is getting to the Sub but I just
can't use it to refer back to it to change it's value.

My code is much more complex than this example and I'm
wanting to do away with redundant code by not doing this
in the AfterUpdate event module of many controls.

Thanks for any tips anyone can suggest on this.

Wally

.

.
 
S

Sandra Daigle

I thought as much :)

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.


Sandra,

You are absolutely right. I misinterpreted
the 'controlname' as psuedospeak for the actual control
name, not a variable holding the name of the control.

-dc
-----Original Message-----
Hi dc -

The bang vs. dot argument is an old one (Use google to search for
"Bang vs. Dot" for more on this) but in this case, the dot is fine - the problem is
that you can't use a variable within a reference (any reference)
unless you go through the collection and refer to it by the key for
the member (the key to the controls collection is the Control Name).

Since ControlName is a a parameter to the procedure, it will not
work in the following. Instead the code will try to find a control
which is named "ControlName" rather than a control which has the
name that is the value of the controlname parameter.

forms("myformname")!controlname = date()

This will work:

forms("myformname").controls(controlname) = date()

For that matter, we could have used a variable in place of
"myformName"

dim strFormName as string
strFormName = "myFormName"
forms(strFormName).controls(controlName) = date()

FWIW, all of this seems more clear to me when we use tags on vba
variable names - then instead of ControlName as a variable, we would
have strControlName.

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Wally,

I think the underlying culprit in your code is the use of
the 'dot' (.) operator instead of the 'bang' (!) operator
to reference a control on a form.

As an alternate to Sandra's approach, you might try this:

if controlvalue > date() then
(here is the part that don't work)
form("myformname")!controlname = date()
end if

With the structure of form("myformname").controlname,
your code is trying to find a method or property of
myform named controlname (which doesn't exist and causes
the problem). With the structure of form("myformname")!
controlname your code will recognize it a control named
controlname in the controls collection of the form myform.

Hope this helps you understand why your code wasn't
working. With that in mind, Sandra's approach of passing
a reference to the form to your Sub instead of the
individual properties fo the form is good.

-dc

-----Original Message-----
Hi Wally,

Pass the control itself - this way you don't need a hardcoded
reference to the form name.

Sub UpdateMe(ctlIn as control, controlValue as date)
if ctlIn > date() then
ctlin=date()
endif
End sub

UpdateMe me.MyCtl

Also, just so you'll know, the way to use a variable as part of a
control reference is as follows:

dim strCtlName as string
strCtlName="MyCtl"

me.controls(strCtlName)

So in your code, you would have:

form("myformname").controls(controlname) = date()


--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this
newsgroup.


Wally wrote:
Hi,
I need to pass a control's name on a form to a sub
procedure. I can do this part, but in the Sub I need to
change this controls value. I'm trying to use the passed
control.name string something like this and of course it
doesn't work.

SUB UpdateMe(controlname as string, controlvalue as date)

if controlvalue > date() then
(here is the part that don't work)
form("myformname").controlname = date()
end if

END SUB

Then I call the Sub from the AfterUpdate event of the
control.

Call UpdateMe(me.control_1.name, me.control_1.value)


The name of the control is getting to the Sub but I just
can't use it to refer back to it to change it's value.

My code is much more complex than this example and I'm
wanting to do away with redundant code by not doing this
in the AfterUpdate event module of many controls.

Thanks for any tips anyone can suggest on this.

Wally

.

.
 

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