Qckst Question - VERY NEWBIE Call Function

esipp

Registered User.
Local time
Today, 14:14
Joined
Nov 5, 2004
Messages
29
:eek:
I am new to VBA for Access. I want to have one set of functions for all the buttons on my forms.

For example, I have a button to go to the next record (btnNext) on many forms.

I do not want the same code in every form for the OnClick event of these buttons.

I want to call a function (that store in a module as a Public Function. See function below):

HOW and WHERE do I call this function? Why can't I just put "btnGoToNextRecord()" in the OnClick event box? Do I need to make it an [Event Procedure] and call it? If so, what is the syntax?

THANK YOU! :rolleyes:

Public Function btnGoToNextRecord()
On Error GoTo Err_btnGoToNextRecord

DoCmd.GoToRecord , , acNext

Exit_btnGoToNextRecord:
Exit Function

Err_btnGoToNextRecord:
MsgBox Err.Description
Resume Exit_btnGoToNextRecord

End Function
 
You need to create a seperate Module

1. Go to the Modules Tab
2. Create a new Module
3. Past the Move code into the module

Example
Code:
Public Sub GotoNextRecord()
On Error GoTo ErrorHandler

    DoCmd.GoToRecord , , acNext

Exit_GotoNextRecord:
    Exit Sub

ErrorHandler:
    MsgBox Err.Description
    Resume Exit_GotoNextRecord
    
End Sub

4. Now anytime you want to call the goto next record on a form, just type GotoNextRecord
 
where EXACTLY (remember, I am dumb) do I put "GotoNextRecord"

I am going to the properties window for the button, and putting "GotoNextRecord" in the On Click event.

I get this error:

"DB can't find the Macro 'GotoNextRecord.' . . . "
 
See steps 1 and 2 above.

You need to create what is called a Public Module

You will place the GotoNextRecord procedure in this module.

Notice that it is "Public", this allows all code modules to be able to see this procedure.
 
Thank you for your help. It works beautifully.

Since you are a guru, what is the single biggest thing I can do to speed up the opening of my Access application. It uses one main, big form with subforms and lots of queried data, but all of it is essential. As is, it's getting to a point where it opens too slowly to be acceptable.

I appreciate any insight you can offer.
 
Do you have to be linked to all of the data?

Access is notorious for bringing all of the data accrossed (even if you only want a single Record).

One posiblity (won't fix the speed issue, but it could mask it) is to have the form open Not Visible to the User and have a Nice Splash Screen. Another idea is to open a Switchboard type form and have the actual form open in the background (not visible) and have it become visible and invisible instead of opening and closing it. Again this does not speed up the application, but it does mask the perception of the speed slow down.
 

Users who are viewing this thread

Back
Top Bottom