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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access the same VB code from multiple forms.

Status
Not open for further replies.

kkitt

Programmer
Dec 1, 2002
122
US
I have multiple forms that have navigation keys on them that perform the same function: Move first record, previous, next, and last. Only one form can be open at a time. I want to make the buttons on these forms access the same VB code.

What happens is one the button is pressed move to the correct record and then I have code to populate the forms controls.

What I have so far is a common sub that is want each key to go though, then bassed off which button was press then call the global codeing in a seperate module.

Code in form is:
Code:
Private Sub cmdButton()

    If Me.cmdMoveFirst Then
        Call glbMoveFirst
    Else
        If Me.cmdMoveLast Then
            Call glbMoveLast
        End If
    else
        if Me.cmdMoveNext then
         ....
         ....
         ....
    End If

    pPopulateFields    ' populate control fields.
End Sub

Code in a separate module handles the checking for BOF, EOF, and such and makes the move to the recordset accordingly.

Whenever I try to attach this code to the buttons on the forms I get a message that the function name "cmdButton" cannot be found.

Any thought on how to get around this. I will be having about 10 forms that will have this same type of navigation added to then and do not want to dup the code on all of then if possible. This will prevent missing a form if the code needs to change for one of these buttons.

Thanks in advance.
Kyle Kittinger




 
My first suggestion would be to make the function public rather than private.
 
Create as snow says a public proc, with one argument of type integer. Create all of your command buttons as a control array (give them the same name, and set the Index property for each). In the Click event handler for this array, call your public proc, and pass the Index argument to it as an argument.

e. g.

Private Sub cmdMyButton_Click(Index as Integer)
DoMyStuff Index
End Sub

Public Sub DoMyStuff(MyArg as Integer)
Select Case MyArg
Case 0
'Do this
Case 1
'Do that
Case 2
'Do the other
(etc)
End Select
End Sub

HTH

Bob
 

How do you set up the control array on the form, whenever I try to assign them the same name access complains that it is already in use?
 
If I remember correctly, control arrays don't exist in Access (A97, A2K anyways). You can sort of simulation control arrays, by using the tag property, etc.

You might want to do a google search for control array access. There is lots of articles out there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top