Call a public function, Compile error: Argument not optional (1 Viewer)

Chief

Registered User.
Local time
Today, 02:22
Joined
Feb 22, 2012
Messages
156
Hello,
I wish to call on a Public Function from my Module_General
This is the Function I wish to use:

Code:
' Check all dates on form to ensure they are not less than the delivery date (TxtCustomerPreferredDate).
Public Function DateCheck(form_name As String)
Dim txtmessage As String
txtmessage = ""

    If Forms(form_name).ChkCarcassCut = True Then
        If Forms(form_name).ChkCC_Cut = False And Forms(form_name).TxtCarcassCut_Due > Forms(form_name).TxtCustomerPreferredDate Then
            txtmessage = "Warning! Delivery/Completion Date cannot be before a process." & vbCrLf & txtmessage
            Forms(form_name).TxtCarcassCut_Due.BackColor = vbRed
            Forms(form_name).TxtCarcassCut_Due.ForeColor = vbWhite
            Forms(form_name).TxtCustomerPreferredDate.BackColor = vbRed
            Forms(form_name).TxtCustomerPreferredDate.ForeColor = vbWhite
            Forms(form_name).TxtCarcassCut_Due.SetFocus
        Else
            Forms(form_name).TxtCarcassCut_Due.BackColor = vbWhite
            Forms(form_name).TxtCarcassCut_Due.ForeColor = vbBlack
            Forms(form_name).TxtCustomerPreferredDate.BackColor = vbWhite
            Forms(form_name).TxtCustomerPreferredDate.ForeColor = vbBlack
        End If
        
        
        
    End If
End Function

This is where I am getting the Compile error: I do not know where to put the Call:

Code:
' Check all fields are complete, check process dates are not later than delivery
Private Sub BtnClose_Click()
    If IsNull(CboDeliverPickUp) Then
        MsgBox "Please select Delivery or Pick up", , "Missing Details"
        CboDeliverPickUp.BackColor = vbRed
        CboDeliverPickUp.ForeColor = vbWhite
        CboDeliverPickUp.SetFocus
    ElseIf IsNull(CboSupplyInstall) Then
        MsgBox "Please select Supply or Install", , "Missing Details"
        CboSupplyInstall.BackColor = vbRed
        CboSupplyInstall.ForeColor = vbWhite
        CboSupplyInstall.SetFocus
    ElseIf ChkTwoPakPaint = True And IsNull(CboPaintID) Then
        MsgBox "Please select paint finish", , "Missing Details"
        CboPaintID.BackColor = vbRed
        CboPaintID.ForeColor = vbWhite
        CboPaintID.SetFocus
    ElseIf ChkSkill = True And IsNull(CboSkill) Then
        MsgBox "Please select Skilled Labour required", , "Missing Details"
        CboSkill.BackColor = vbRed
        CboSkill.ForeColor = vbWhite
        CboSkill.SetFocus
    ElseIf IsNull(TxtCustomerPreferredDate) Then
        TxtCustomerPreferredDate.BackColor = vbRed
        TxtCustomerPreferredDate.ForeColor = vbWhite
        TxtCustomerPreferredDate.SetFocus
    Else
        CboDeliverPickUp.BackColor = vbWhite
        CboDeliverPickUp.ForeColor = vbBlack
        CboSupplyInstall.BackColor = vbWhite
        CboSupplyInstall.ForeColor = vbBlack
        CboPaintID.BackColor = vbWhite
        CboPaintID.ForeColor = vbBlack
        CboSkill.BackColor = vbWhite
        CboSkill.ForeColor = vbBlack
        TxtCustomerPreferredDate.BackColor = vbWhite
        TxtCustomerPreferredDate.ForeColor = vbBlack
    Call DateCheck
        DoCmd.Close
    End If
End Sub

Thank you.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:22
Joined
May 7, 2009
Messages
19,170
you add your form's name to DateCheck function, see the function.
also the error message is suggestive.

Call DateCheck("yourFormName")
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:22
Joined
Feb 28, 2001
Messages
27,001
OK, let's split this question. I don't know where to put the call either. But the "Argument not optional" is because when you declared the DateCheck code you (a) specified a function and (b) specified that it needed a form name as an argument. When you actually USED it, you (a) used it as a Sub and (b) did NOT provide a form name.
 

Chief

Registered User.
Local time
Today, 02:22
Joined
Feb 22, 2012
Messages
156
OK, let's split this question. I don't know where to put the call either. But the "Argument not optional" is because when you declared the DateCheck code you (a) specified a function and (b) specified that it needed a form name as an argument. When you actually USED it, you (a) used it as a Sub and (b) did NOT provide a form name.
Thank you,
Learning everyday.
 

Chief

Registered User.
Local time
Today, 02:22
Joined
Feb 22, 2012
Messages
156
Thank you for the suggestions and knowledge,
I think I am out of my depth with this one.

I have added the function to the form, and I'll need to repeat for all of my date checks.

Thanks again, appreciate it.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:22
Joined
Feb 19, 2002
Messages
42,981
More importantly, validation code belongs in the BeforeUpdate event of the form (or called from that event if the code is common). NOT in the click event of a button. If the user doesn't click the button, the validation will not happen and bad data would be saved. Think of the BeforeUpdate event of the form as the flapper at the bottom of a funnel. This event runs for EVERY dirty record. It is the LAST event that runs before the record is saved and it CANNOT be bypassed so regardless of what prompted the save, this code runs and that is why the validation code belongs here. There are reasons you might want validation code in the BeforeUpdate event of a specific field but you STILL need it here as well. I only do this when I want to check for duplicates because I'm not going to let the user save the record and I don't want him to enter any more data before I ensure that a specific field such as SSN is unique. The SSN code still must be checked for null in the form level event if SSN is required since if the user never puts focus into the SSN control, no controlevents would run so you can't ever do the null check in form level events.

I understand the intent to reuse code but I think there might be a better way. Your "common" code has specific field names. That means that it only works on specific fields. If you wanted to do standard tests against different data types, a better way would be to call the procedure from the form's BeforeUpdate event and pass in the form object. Rather than doing it the way you have I use:

Public function CheckDates(frm as DAO.Form) As Boolean

Then the call is:

Code:
If CheckDates(Me) = False Then    ''' error found
    Cancel = True
    Exit Sub
End If

Inside the routine, use a loop that reads through the fields collection of frm. and for datetime fields, do the validation. Your way, you have to remember to call the routine each time and the names all need to be the same. You need to display error messages in the code and highlight the error fields since the code is checking more than one field without exiting Remember, you need to pass back a return code so that the BeforeUpdate procedure can cancel the update and exit without saving.

A very common function that is posted here frequently shows how to check for nulls. Most versions rely on a value in the Tag property of the control and so it only checks controls with a specific value in .Tag. That accommodates having both required and optional fields on the form.

Sorry, I looked for the code but couldn't find it for you. If someone has a link please post it. Thanks.
 
Last edited:

Users who are viewing this thread

Top Bottom