CommandButton array

D

DZ

I have many CommandButton in a form header. Each one updates a specific field
from a value in an unbound textbox located in the form header.

The only difference between the Click subs for each button are 2 references
to the unbound text box (values coming from) and the field (value going to)
it is updating.

I carefully named each command button and unbound textbox the same as the
ControlSouirce it is affecting, except for a prefix (txt and cmd)

It would great if there was a way for each button click sub call the same
Sub that somehow could get the command button name, because the command
button name could be easily parsed and used for the unbound text box and the
field reference in the sub.

This is sort of like the CommandButton Array concept in VB6, but I'm not
sure if this is possible in Access.

Any ideas would be welcome
 
P

Piet Linden

I have many CommandButton in a form header. Each one updates a specific field
from a value in an unbound textbox located in the form header.

The only difference between the Click subs for each button are 2 references
to the unbound text box (values coming from) and the field (value going to)
it is updating.

I carefully named each command button and unbound textbox the same as the
ControlSouirce it is affecting, except for a prefix (txt and cmd)

It would great if there was a way for each button click sub call the same
Sub that somehow could get the command button name, because the command
button name could be easily parsed and used for the unbound text box and the
field reference in the sub.

This is sort of like the CommandButton Array concept in VB6, but I'm not
sure if this is possible in Access.

Any ideas would be welcome

Here. let Dev explain it to you...
http://www.mvps.org/access/forms/frm0038.htm
 
K

Klatuu

I would do it differently.

Private Function CommandButttonUpdate()
Dim strCtlName As String


strCtlName = Me.ActiveControl.Name
strCtlName = Replace(strCtlName, "cmd", "txt")

Me.Controls(strCtlName) = ....


That would be the basics of it. Then in the Click Event dialog box for eacm
command button, All you need is
=CommandButtonUpdate()
I don't know exactly what you are doing, but what you see above will get you
started. Here is how it breaks down:

Assign the name of the command button that was clicked to a string variable
strCtlName = Me.ActiveControl.Name

Replace the cmd prefix with the txt prefix so you can address the
appropriate text box
strCtlName = Replace(strCtlName, "cmd", "txt")

Use the form's controls collection to assign it a value
Me.Controls(strCtlName) = ....

Now, if you want to get cute with it, you could use:

Me.Controls(Replace(Me.ActiveControlName, "cmd", "txt")) =
 

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