Private vs Public Procedure

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a Private Sub btnBooksMsg for a button on a form. I would like to
make that same procedure available to all forms. I tried changing "Private"
to "Public" but the button on the second form did not perform the procedure.
What am I doing incorrectly?

Thanks.
 
Hi, Rod.
What am I doing incorrectly?

If you want this procedure to be available to all forms, then it's best to
place the public procedure in a standard module. However, it's possible for
the other forms to use this form's procedure as you have it now. Ensure that
the form containing the procedure is open, then use the following syntax:

Call Form_MyForm.btnBooksMsg

.. . . where MyForm is the name of the form containing the public btnBooksMsg
procedure.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact info.
 
'69 Camaro said:
If you want this procedure to be available to all forms, then it's best to
place the public procedure in a standard module. However, it's possible for
the other forms to use this form's procedure as you have it now. Ensure that
the form containing the procedure is open, then use the following syntax:

Call Form_MyForm.btnBooksMsg

. . . where MyForm is the name of the form containing the public btnBooksMsg
procedure.


I'm probably just being picky here Gunny, but that syntax
calls the procedure in the form's default instance. If the
procedure relies on any module level variables, it may not
get the same values as in the open form's instance. To be
safe the sybtax should be:

Forms!myform.btnBooksMsg
 
I get a syntax error when I press the button using the follwoing:

Private Sub Toggle548_Click()
Call Forms!btnBooksMsg_Click ()
End Sub

What should "Forms!" be? Is this my error?

I have created my first module:
Option Compare Database
Option Explicit

'Status Modules:
Public Sub btnBooksMsg_Click()
On Error GoTo Err_Handler
'Need to work out this area
Dim Dated As Boolean

Dated = False
' If (Dated = False And Day(Date) > 7) Or (FirstPass = True And
Day(Date) > 7) Then...
End if
Exit_Handler:
Exit Function

Err_Handler:
MsgBox "Error " & Err.NUMBER & " - " & Err.Description, vbExclamation,
Resume Exit_Handler

End Sub




'69 Camaro said:
Thanks, Marsh. I never noticed that difference in usage between the two
syntaxes.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact
info.
 
Hi, Rod.
What should "Forms!" be? Is this my error?

Yes. It's one of them. Your syntax is missing the name of the form.
Marsh's syntax is in the correct format. If you're trying to use the click
event of the btnBooksMsg button, try:

Call Forms!MyForm.btnBooksMsg_Click

.. . . where MyForm is the name of the form, btnBooksMsg is the name of the
button, and this button's click event is what's going to be executed.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact
info.


Rod said:
I get a syntax error when I press the button using the follwoing:

Private Sub Toggle548_Click()
Call Forms!btnBooksMsg_Click ()
End Sub

What should "Forms!" be? Is this my error?

I have created my first module:
Option Compare Database
Option Explicit

'Status Modules:
Public Sub btnBooksMsg_Click()
On Error GoTo Err_Handler
'Need to work out this area
Dim Dated As Boolean

Dated = False
' If (Dated = False And Day(Date) > 7) Or (FirstPass = True And
Day(Date) > 7) Then...
End if
Exit_Handler:
Exit Function

Err_Handler:
MsgBox "Error " & Err.NUMBER & " - " & Err.Description, vbExclamation,
Resume Exit_Handler

End Sub
 
Hi Gunny,

I am still getting a syntax error (red letter) in VB. The name of the
button comes from Properties/All/Name, correct? I have "btnDialNumber" there.
 
Hi, Rod.
The name of the
button comes from Properties/All/Name, correct?

Open the form in Design View and select the button on the form, then open
the Properties dialog window. Select the "Other" tab and look at the "Name"
Property. That's the name of the button to use in your code. As for VBA
code, try:

Call Forms("MyForm").btnDialNumber_Click

.. . . where MyForm is the name of the form that has the button you are
interested in, and btnDialNumber is the name of the button that you want to
execute its click event from the other forms.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact info.
 
Back
Top