simple ? (1 Viewer)

e2cheng

Registered User.
Local time
Today, 01:08
Joined
Dec 13, 2002
Messages
72
how do you run the command of a different object.

for example.

i have a button1 that does something on a click

i have a button 2 that does the same thing on a click

instead of writing out the code twice, how do i reference button2 to do button1_click()???

thanx in advance for the help.
 

Robert Dunstan

Mr Data
Local time
Today, 05:08
Joined
Jun 22, 2000
Messages
291
You need to use the Call event.

In the On_Click event of Button2 put this code

Call Button1_OnClick()

HTH
Rob
 

Lex

Registered User.
Local time
Yesterday, 23:08
Joined
Jan 7, 2003
Messages
11
Write a macro that runs the event and assign it to each button you need.
 

BukHix

Registered User.
Local time
Today, 00:08
Joined
Feb 21, 2002
Messages
379
As an alternative lets say that you have some code that you want to use to toggle the visibilty of text box controls on your forms. Instead of writing the code behind each command button you can write the code once as a function (Public or Private).

In this example I am going to use a Public Function so that every form in my application can use it.

First of all create a new module and put this code in it.
Code:
Public Function HideControls()
Dim ctl  As Control

For Each ctl In Screen.ActiveForm.Controls
    If TypeOf ctl Is TextBox Then
        With ctl
            .Visible = False
        End With
    End If
Next ctl

End Function

'*******************************************

Public Function ShowControls()
Dim ctl  As Control

For Each ctl In Screen.ActiveForm.Controls
    If TypeOf ctl Is TextBox Then
        With ctl
            .Visible = True
        End With
    End If
Next ctl

End Function
There are two functions there, one to hide the controls and the other to show them again.

Now when ever you want to use them you just need to put the name of the function behind your command button like this:
Code:
Private Sub cmdHide_Click()
     [b]HideControls[/b]
End Sub

' or

Private Sub cmdShow_Click()
     [b]ShowControls[/b]
End Sub
Writing code in functions takes a little getting used to but it is definitely worth it especially if you ever need to modify something. You can make the change in one place instead of editing the code behind all your command buttons.
 
Last edited:

bradcccs

Registered User.
Local time
Today, 14:08
Joined
Aug 9, 2001
Messages
461
Sorry to add to a post that appears to have been answered but..


I reference other sub's using the below:


Private Sub Button1_Click()
'Your code in here etc
End Sub


Private Sub Button2_Click()
Button1_Click
End Sub


Note the missing brackets when refering to another sub within a sub.

Brad.
 

sambo

Registered User.
Local time
Yesterday, 21:08
Joined
Aug 29, 2002
Messages
289
I have to go w/ BukHix on this one. Learning to write functions instead of Event Procedures will save a lot of time and is much more efficient.
 

Users who are viewing this thread

Top Bottom