Multiple Ctrls running same code

A

Airkon

I have a form with alot of textboxes. Each of these textboxes has an up and
down arrow keys to increment or decrement the value and the function that
does this is in a general module.

Is there a way to make all these command buttons's afterupdate to run the
same function? Thanks in advance.
 
T

Tom Ellison

Dear Airkon:

The events for different controls are necessarily different events. So each
must be separate.

However, each one of these could call a Sub that performs the necessary
task. You would likely have to pass the control object from each to the
Sub, so I could reference the specific text box and do its work there. So,
there would be some alteration of the code, as Me would not be usable to
refer to the control outside the form's module.

Tom Ellison
 
K

Klatuu

Here is some code that does exactly that. All my forms that use navigation
buttons use this routine. The Click Event of each button calls SetNavButtons:

Private Sub cmdNextRec_Click()
On Error GoTo cmdNextRec_Click_Error

On Error GoTo Err_cmdNextRec_Click

DoCmd.GoToRecord , , acNext
Call SetNavButtons(Me)
Exit_cmdNextRec_Click:
Exit Sub
*****************
Sub SetNavButtons(SomeForm As Form)
Dim lngPosition As Long

On Error GoTo SetNavButtons_Error

With SomeForm
If .Recordset.AbsolutePosition = 0 Then
lngPosition = 0 'At first record
ElseIf .Recordset.AbsolutePosition = .Recordset.RecordCount - 1 Then
lngPosition = 2 'At last record
Else
lngPosition = 1 'Somewhere in the middle
End If

Select Case lngPosition
Case 0 'First Record
.cmdNextRec.Enabled = True
.cmdLastRec.Enabled = True
.cmdNextRec.SetFocus
.cmdFirstRec.Enabled = False
.cmdPreviousRec.Enabled = False
Case 1 'Somewhere in the middle
.cmdFirstRec.Enabled = True
.cmdPreviousRec.Enabled = True
.cmdNextRec.Enabled = True
.cmdLastRec.Enabled = True
Case 2 'Last Record
.cmdFirstRec.Enabled = True
.cmdPreviousRec.Enabled = True
.cmdPreviousRec.SetFocus
.cmdNextRec.Enabled = False
.cmdLastRec.Enabled = False
End Select
End With
SetNavButtons_Exit:
 
M

Marshall Barton

Airkon said:
I have a form with alot of textboxes. Each of these textboxes has an up and
down arrow keys to increment or decrement the value and the function that
does this is in a general module.

Is there a way to make all these command buttons's afterupdate to run the
same function? Thanks in advance.


In addition to other's comments, you can avoid creating an
event procedure for each control by setting the event
**property** of each control to a function call:
=commoneventcode()

You can even do that for all the controls at once by
selecting all the desired controls by dragging a selection
box around the controls or holding down the Shift key and
clicking the desired controls.
 
A

Airkon

Dear Marshall Barton

In my case, I have to pass a value into the function (to increment the
value). Is it still possible to use a common function call?

My function looks something like this incrementValue(Me!Control.Tag, 1)

I tried adding a common macro to all the control that calls this function
with the RunCode action but it doesnt seem to work. It gave me an error
saying the object doesnt contain the automation object 'Me'. How can I fix
this?

Thanks in advance.
 
M

Marshall Barton

You can do that by just getting rid of the Me. If you feel
the need to put something there you can use Form instead of
Me.

But don't forget that if the control you want to pass as an
argument is the same control with the event property, then
you do not need to use an argument. The function can refer
to Me.ActiveControl to get to its properties.
 
A

Airkon

Dear Marshall Barton

I tried all the possible parameters but none of them seem to work.
Basically, I stored all the Command Buttons's Tag property to the textbox
value. So when I click on those buttons, the value in the textbox is
incremented. Problem now is that I am unable to send the textbox data info to
the macro calling the general function. Is using VBA code behind every
command button the only solution?

Marshall Barton said:
You can do that by just getting rid of the Me. If you feel
the need to put something there you can use Form instead of
Me.

But don't forget that if the control you want to pass as an
argument is the same control with the event property, then
you do not need to use an argument. The function can refer
to Me.ActiveControl to get to its properties.
--
Marsh
MVP [MS Access]

In my case, I have to pass a value into the function (to increment the
value). Is it still possible to use a common function call?

My function looks something like this incrementValue(Me!Control.Tag, 1)

I tried adding a common macro to all the control that calls this function
with the RunCode action but it doesnt seem to work. It gave me an error
saying the object doesnt contain the automation object 'Me'. How can I fix
this?
 
M

Marshall Barton

You have completely lost me here. I have no idea what
"stored all the Command Buttons's Tag property to the
textbox value" means.

I can not even attempt to deduce a solution to "tried all
the possible parameters". How about posting the actual
event property you tried, the code you are using and
whatever result it produced.
 

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