Unable to call Module?

P

Pat Dools

Hello,

I have a Command Button in which I do some checks on some required fields,
and if they are all filled in, I would like to call the 'SaveCloseForm'
module, which will attempt to save the record, and, if there is a Key
violation, will alert the user. Here is the code behind the Command Button:

Private Sub CommandCloseAndSave_Click()
If Len(Me.site.Value & "") = 0 Then
MsgBox "You must enter a value into Site Number."
Me.site.SetFocus
ElseIf Len(Me.id.Value & "") = 0 Then
MsgBox "You must enter a value into Patient Number."
Me.id.SetFocus
ElseIf Len(Me.ptin.Value & "") = 0 Then
MsgBox "You must enter a value into Patient Initials."
Me.ptin.SetFocus
ElseIf Len(Me.cyclenum.Value & "") = 0 Then
MsgBox "You must enter a value into Cycle."
Me.cyclenum.SetFocus
ElseIf Len(Me.examday.Value & "") = 0 Then
MsgBox "You must enter a value into Cycle."
Me.examday.SetFocus
ElseIf Len(Me.data1_init.Value & "") = 0 Then
MsgBox "You must enter a value into Data Entry 1 Initials."
Me.data1_init.SetFocus
Else
'Me.Refresh
'Call DoCmd.Close
Call SaveCloseForm
End If
End Sub

When I call to a Module, I get the error 'Expected variable or procedure,
not module', when I attempt to call 'SaveCloseForm' (see code):

Public Sub SaveCloseForm()
On Error GoTo Err_SaveCloseForm

DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close

Exit_SaveCloseForm:
Exit Sub

Err_SaveCloseForm:
MsgBox Err.Description
Resume Exit_SaveCloseForm

End Sub

However, if I insert this as a Private Sub on the same Form and call it, it
works. However, this means I have to insert the same code on every form,
instead of calling it from one place. Why am I not allowed to call a Module
after the 'Else.. ' in my If Statement above?

Thanks.
 
D

Dirk Goldgar

Pat Dools said:
Hello,

I have a Command Button in which I do some checks on some required
fields, and if they are all filled in, I would like to call the
'SaveCloseForm' module, which will attempt to save the record, and,
if there is a Key violation, will alert the user. Here is the code
behind the Command Button:

Private Sub CommandCloseAndSave_Click()
If Len(Me.site.Value & "") = 0 Then
MsgBox "You must enter a value into Site Number."
Me.site.SetFocus
ElseIf Len(Me.id.Value & "") = 0 Then
MsgBox "You must enter a value into Patient Number."
Me.id.SetFocus
ElseIf Len(Me.ptin.Value & "") = 0 Then
MsgBox "You must enter a value into Patient Initials."
Me.ptin.SetFocus
ElseIf Len(Me.cyclenum.Value & "") = 0 Then
MsgBox "You must enter a value into Cycle."
Me.cyclenum.SetFocus
ElseIf Len(Me.examday.Value & "") = 0 Then
MsgBox "You must enter a value into Cycle."
Me.examday.SetFocus
ElseIf Len(Me.data1_init.Value & "") = 0 Then
MsgBox "You must enter a value into Data Entry 1 Initials."
Me.data1_init.SetFocus
Else
'Me.Refresh
'Call DoCmd.Close
Call SaveCloseForm
End If
End Sub

When I call to a Module, I get the error 'Expected variable or
procedure, not module', when I attempt to call 'SaveCloseForm' (see
code):

Public Sub SaveCloseForm()
On Error GoTo Err_SaveCloseForm

DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close

Exit_SaveCloseForm:
Exit Sub

Err_SaveCloseForm:
MsgBox Err.Description
Resume Exit_SaveCloseForm

End Sub

However, if I insert this as a Private Sub on the same Form and call
it, it works. However, this means I have to insert the same code on
every form, instead of calling it from one place. Why am I not
allowed to call a Module after the 'Else.. ' in my If Statement above?

Thanks.

Did you by any chance name the *module* "SaveCloseForm", as well as the
Sub procedure inside it? You cannot call a module, which is just a
container for code; you can only call the procedures *in* the module.
If your module is named "SaveCloseForrm", change that to
"modSaveCloseForm", or "basSaveCloseForm", or any other unique name.
 
J

Jologs

Hi Pat:

I'm having exactly the same problem. I created a module called
'modSendMail' which contains the 'Public Sub SendNotesMail(Subject As String,
Attachement As String, Recipient As String, Body As String, SaveIt As
Boolean)'. I'm trying to call the procedures on my OnClick Event of the
cmdbutton 'Call modSendMail', but I got the same error similar to you. What
exactly did you do. Thanks.
 
D

Dirk Goldgar

Jologs said:
Hi Pat:

I'm having exactly the same problem. I created a module called
'modSendMail' which contains the 'Public Sub SendNotesMail(Subject As
String, Attachement As String, Recipient As String, Body As String,
SaveIt As Boolean)'. I'm trying to call the procedures on my OnClick
Event of the cmdbutton 'Call modSendMail', but I got the same error
similar to you. What exactly did you do. Thanks.

You shouldn't be trying to call the *module*; modules are just
containers for code, and are not callable directly. Instead, you should
be calling the sub itself:

Call SendNotesMail( _
"your subject", _
"your attachment", _
"your message body", _
True) ' or False
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top