Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Functions called from forms

Status
Not open for further replies.

MackOfTrades

Programmer
Oct 3, 2002
9
US
I was curious if it is possible to create functions that are called by different aspects of the same form. For instance: I would like to be able to have the same function run regardless of which button a user presses (of course there will be other things going on too, so the different buttons are not redundant). Is it possible to write a function that can be called from inside a specific action (user clicks button)? If so, where do you put this function? As it is right now, I have forms that have ballooned in size do to the redundancy of some longer functions and am looking for a way to cut my code down. As always, I appreciate any responses.
Mack
 

do you mean something like this...

place code in form 1 with command button on form

[tt]
Option Explicit

Private Sub Command1_Click
Call DisableControlsInForm(Me)
MsgBox "Controls Disabled"
Call EnableControlsInForm(Me)
End Sub


[/tt]

place code in module

[tt]
Option Explicit

Public Sub DisableControlsInForm As Form

Dim C As Control

For Each C In F
C.Enabled = False
Next

End Sub

Public Sub EnableControlsInForm As Form

Dim C As Control

For Each C In F
C.Enabled = True
Next

End Sub



[/tt]

Then the answer would be yes to your question
 
Hi

Have you looked at the control array facility within VB?

Or are the buttons not related?

e.g. A search facility may make use of 26 buttons (A-Z). Each button obviously has a different caption (1 letter of the alphabet), but ALL the buttons respond to the same event.

If, however, you are looking for an event for buttons / controls that are NOT performing the same function, you will have to look at performing a loop within the form that checks every control on the form when any event occurs.

e.g
Dim ctl As Control

For Each ctl in Controls
If TypeOf ctl = TEXTBOX Then
do something
Endif
Next ctl

Look in the help for TypeOf and I think that may help.

As for which event to put this code in, look at some events for the form.

Regards
Andy
 
vb5prgrmr I think that's what I've been looking for. So you would place to function in the option explicit part of the form?

I really appreciate the quick responses.
 

If you are not using a control array:


Const CMD_ACTION_0% = 0
Const CMD_ACTION_1% = 1

Private Sub Command0_Click()
Call CommandAction(CMD_ACTION_0)
End Sub

Private Sub Command1_Click()
Call CommandAction(CMD_ACTION_1)
End Sub

Private Sub CommandAction(Action As Integer)

Select Case Action
Case CMD_ACTION_0
'Code when user presses Command1
Case CMD_ACTION_1
'Code when user presses Command1
Case Else
'Nothing to do
End Select
End Sub

If you are using a control array then just make this substitution:

Private Sub Command1_Click(Index As Integer)
Call CommandAction(Index)
End Sub

You could Use an Enum instead:

Private Enum eCommand
CMD_ACTION_0 = 0
CMD_ACTION_1
CMD_ACTION_2
End Enum

Private Sub Command0_Click()
Call CommandAction(CMD_ACTION_0)
End Sub

Private Sub Command1_Click()
Call CommandAction(CMD_ACTION_1)
End Sub

Private Sub CommandAction(Action As eCommand)
Select Case Action
Case CMD_ACTION_0
'Code when user presses Command1
Case CMD_ACTION_1
'Code when user presses Command1
Case Else
'Nothing to do
End Select
End Sub
[/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Thanks a lot everybody. That's helped me a lot.

Have a great day
Mack
 
PS: Declare the constants in the Declaration section:

Options Explicit
Private Const CMD_ACTION_0% = 0
Private Const CMD_ACTION_1% = 1


Or, if you use an Enum

Options Explicit
Private Enum eCommand
.
.
.
End Enum


Then place the function: CommandAction, somewhere below the declarations right before or right after
some other Method, property or event. [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top