I've got a function that iterates through each control on the form and looks for any required fields that aren't filled in. The function worked great initially by doing the check in the module and the control iteration in the form. However, this results in the recordset (table) being opened every time the control iterates in the form and then calls the function (which opens the recordset). Here's the code below:
The form button is tied to this which starts the iteration and calls the function:
The original function ReqCheck is below:
It seems that moving the form part of the iteration to the function would improve the speed since it would only open the recordset once, then do all the searches, then close the recordset.
My issue is if I change the function to be called like this:
and call it from the form like this:
it errors out (type mismatch). Any idea what i'm doing wrong? How do you call a function with a variable as form?
Thanks.
The form button is tied to this which starts the iteration and calls the function:
Code:
Private Sub cbNext_Click()
Dim strForm As String
Dim strField As String
Dim strMissing As String
Dim intMissing As Integer
strMissing = ""
intMissing = 0
strForm = Me.Name
For Each Control In Me.Controls
strField = Control.Name
Select Case ReqCheck(strForm, strField)
Case Is = True
strMissing = strMissing & strField & Chr(10)
intMissing = intMissing + 1
End Select
Next Control
If intMissing > 0 Then
MsgBox "You need to fill in the following " & intMissing & " fields:" & Chr(10) & strMissing, vbCritical, "Missing Required Fields"
End
End If
The original function ReqCheck is below:
Code:
Public Function ReqCheck(strForm As String, strField As String)
'This function evaluates whether a field passed to it is requred or not.
'It returns TRUE for a required field, and FALSE for a non-required field.
Dim rs As Recordset
Dim db As Database
Set db = CurrentDb
Set rs = db.OpenRecordset("tbl_Required_Controls", dbOpenSnapshot).Clone
rs.MoveFirst
rs.FindFirst ("[RequiredControls] = '" & strField & "' AND [Form] = '" & strForm & "'")
Select Case rs.NoMatch
Case Is = True
ReqCheck = False
Debug.Print "ReqCheck - " & strForm & " - " & strField & ": " & ReqCheck
rs.Close
Case Is = False
ReqCheck = True
Debug.Print "ReqCheck - " & strForm & " - " & strField & ": " & ReqCheck
rs.Close
End Select
End Function
It seems that moving the form part of the iteration to the function would improve the speed since it would only open the recordset once, then do all the searches, then close the recordset.
My issue is if I change the function to be called like this:
Code:
ReqCheck(frm as Form)
Code:
ReqCheck(Me.Form.Name)
it errors out (type mismatch). Any idea what i'm doing wrong? How do you call a function with a variable as form?
Thanks.
Last edited: