Problem with calling a public function

snakie

Registered User.
Local time
Today, 08:46
Joined
Jun 18, 2009
Messages
14
Hi,

I hope someone can help me. I've never done any public functions previously, but I have a short procedure which is going to be used in various part of the database so I'm hoping to put it into public function. However I am unable to call it (either the public function is wrong, or the way I call it is wrong).

This is what I have got so far:

Public Function modFormCompletionCheck(strValueEntered As Variant)

strValueEntered = Nz(Me.LocationID.Value, "")

If strValueEntered = "" Then
MsgBox "The location field should not be blank. Please re-enter.", _
vbExclamation, "Incomplete Form"
Exit Function
End If

End Function

--
Private Sub CreateRptUnderView_Click()
On Error GoTo Err_CreateRptUnderView_Click

Dim stDocName As String

Call Form_frmQuotesGenerator2.modFormCompletionCheck

'Create quote

stDocName = "Quotation 250"
DoCmd.OpenReport stDocName, acPreview

Exit_CreateRptUnderView_Click:
Exit Sub

Err_CreateRptUnderView_Click:
MsgBox Err.Description
Resume Exit_CreateRptUnderView_Click

End Sub

--

Can someone suggest what I have done wrong? The first part was the actual function. The second part was a procedure on a form trying to call that module. When I try to run it, it said there is a problem with this line:

Call Form_frmQuotesGenerator2.modFormCompletionCheck

Thanks in advance.
 
Just change:

Call Form_frmQuotesGenerator2.modFormCompletionCheck

to this:

Call modFormCompletionCheck ("SomeVariant")
 
Just change:

Call Form_frmQuotesGenerator2.modFormCompletionCheck

to this:

Call modFormCompletionCheck ("SomeVariant")

Hello and thanks for this.

I have changed it to:

Dim strValueEntered As Variant
Call modFormCompletionCheck(strValueEntered)

However it doesn't work. It said "Expected variable or procedure, not module".

Why is that?

Joe
 
One of the main problems may be is that the module (container) has a public function inside it that has the same name as the module. This is not permitted. If this is the case I strongly suggest you rename the module by prefixing it with "bas".

David
 
One of the main problems may be is that the module (container) has a public function inside it that has the same name as the module. This is not permitted. If this is the case I strongly suggest you rename the module by prefixing it with "bas".

David

Hi,

Erm... I have checked and it doesn't make any difference.

Am I right calling modFormCompletionCheck in the correct way? (see above)

Joe
 
Code:
Public Function modFormCompletionCheck(strValueEntered As Variant)

strValueEntered = Nz(Me.LocationID.Value, "")

If strValueEntered = "" Then
MsgBox "The location field should not be blank. Please re-enter.", _
vbExclamation, "Incomplete Form"
Exit Function
End If

End Function

Ok - step by step

You have a form open and in that form you have a textbox named LocationID and you want to test if this has been populated or not. So on you Form UnLoad event you decide to test this by way of a public function. So now lets create a function to do that.

First we either design an existing module or create a new one.

Next we declare the function and assign parameter(s)

Code:
Public Function HasLocation([COLOR="Red"]Location [/COLOR]As Long) As [COLOR="SeaGreen"]Boolean[/COLOR]

    HasLocation = IIF(Location = 0,False,True)

End Function

The Location is the value we are passing to the function and the Boolean is the results of the test.

So back to our form UnLoad event. What we do now is call the function by the most appropriate method. In our case.

Code:
Private Sub Form_Unload(Cancel As Integer)


If HasLocation(Nz(Me.LocationID,0)) = False Then
   MsgBox "The location field should not be blank. Please re-enter.",vbExclamation, "Incomplete Form"
   Cancel = True
Else
   DoCmd.Close
End If


End Sub

Another way to check if the location has been selected is

Code:
Dim bFlag As Boolean

bFlag = HasLocation(Nz(Me.LocationID,0))

If bFlag = False Then
   'Do something here
Else
   'Do something here
End If

Hope this explains how to use functions more clearly.

David
 
Hi and many thanks for the explanation. It's very useful.

This public function is going to be used in 12 locations in the database because there are 12 separate reports to be linked to.

Is there a way to put everything into the public function and just use one line to call the function over in a procedure?

Joe
 
Can you give me a more detailed explanation as per an example as I don't quite grasp what you are getting at.

David
 
Can you give me a more detailed explanation as per an example as I don't quite grasp what you are getting at.

David

Hello,

Basically, if I put the whole thing in public function so that I can call it anywhere in the database:

Code:
Public Function modFormCompletionCheck(strValueEntered As Variant)

    strValueEntered = Nz(Me.LocationID.Value, "")

    If strValueEntered = "" Then
        MsgBox "The location field should not be blank. Please re-enter.", _
               vbExclamation, "Incomplete Form"
        Exit Function
    End If

End Function

The above function is to be called from several forms within the same database as they all link to different reports.

Instead of putting large amount of codes behind each of the OnClick procedure on various forms, is it possible to simply call the above function through one line or two so that the majority of the codes stay in the public function?

Joe
 
At each point where you want to test the location

Call TestLocation


TestLocation will be a Public sub in a module

Code:
Public Sub TestLocation()


If HasLocation(Nz(Me.LocationID,0)) = False Then
   MsgBox "The location field should not be blank. Please re-enter.",vbExclamation, "Incomplete Form"

End If


End Sub

The only problem you will have is that the function does not know where it is being called from (unless you tell it) also the message may not be appropriate form the location is it being called from.


David
 
Hello and many thanks for this.

Somehow I still got this error message when trying to run it (regardless of whether it is a Public Function or Public Sub.

The error message is "Compile Error: Expected variable or procedure, not module".

Do you know why this is the case?

Thanks in advance.

Joe
 
The statement "Not Module" stands out. Is your module the same name as a function or sub routine. Sounds like a conflict in names.
 

Users who are viewing this thread

Back
Top Bottom