Hi Experts, I am sure this is an easy one. What is the best way to handle using the codes on one form in another. I figure doing this, will reduce the size of the database and also speed up the processing times. I have some validation rules on the form level, that applies to several other 1 or 2 other forms. I thought i code put the code in a module, but not sure how that would work. Below are some of the validation rules that I have setup on my main form. Can some give me an example how I can set this up. Thanks in advance.
Private Sub Form_BeforeUpdate(Cancel As Integer) On Error GoTo Form_BeforeUpdate_Err
'Forces user to enter FAT if entry in closing date If IsNull(Me.cboFat) And Not IsNull(Me.ClosingDate) Then MsgBox "Final Action Taken is required if a Closing Date is entered,", vbCritical, "Missing Data!" Cancel = True cboFat.SetFocus End If
'Forces user to enter Closing Date if entry in FAT If Not IsDate(ClosingDate) Then
Select Case Me.cboFat Case 1, 2, 3, 4, 5, 7 MsgBox "Based on your selection in the Final Action Taken," & _ vbCrLf & " Closing Date is a required field!", vbCritical, "Missing Data!" Cancel = True End Select End If
' Forces user to enter Actual Amount if FAT = 1 If Me.cboFat = 1 And IsNull(Me.ActualAmount) Then MsgBox "Based on your selection in the Final Action Taken," & _ vbCrLf & " Actual Amount is a required field!", vbCritical, "Missing Data!" Cancel = True ActualAmount.SetFocus End If
If IsNull(Me.cboLTV) Then
Select Case Me.cmbMtgType Case "2 Yr ARM", "15 Yr", "20 Yr", "25 Yr", "30 Yr" MsgBox "The selected Mortgage Type requires a LTV selection.", vbCritical, "Missing Data!" Cancel = True cboLTV.SetFocus End Select End If
public Sub validationform() 'Forces user to enter FAT if entry in closing date If IsNull(Me.cboFat) And Not IsNull(Me.ClosingDate) Then MsgBox "Final Action Taken is required if a Closing Date is entered,", vbCritical, "Missing Data!" Cancel = True cboFat.SetFocus End If
'Forces user to enter Closing Date if entry in FAT If Not IsDate(ClosingDate) Then
Select Case Me.cboFat Case 1, 2, 3, 4, 5, 7 MsgBox "Based on your selection in the Final Action Taken," & _ vbCrLf & " Closing Date is a required field!", vbCritical, "Missing Data!" Cancel = True End Select End If
' Forces user to enter Actual Amount if FAT = 1 If Me.cboFat = 1 And IsNull(Me.ActualAmount) Then MsgBox "Based on your selection in the Final Action Taken," & _ vbCrLf & " Actual Amount is a required field!", vbCritical, "Missing Data!" Cancel = True ActualAmount.SetFocus End If
If IsNull(Me.cboLTV) Then
Select Case Me.cmbMtgType Case "2 Yr ARM", "15 Yr", "20 Yr", "25 Yr", "30 Yr" MsgBox "The selected Mortgage Type requires a LTV selection.", vbCritical, "Missing Data!" Cancel = True cboLTV.SetFocus End Select End If End Sub
But how do I reference that code in the other forms? Sorry really new to this stuff.
1) Put that sub in a regular VBA module, not in a form or report module
2) You may need to pass the name of the form as an argument. So your "public sub may look like:
Sub Validate(NameOfForm As String)
'some code here
End Sub
and then you call it like this from a Form event:
Private Sub cmdGo_Click()
Validate "NameOfFormGoesHere"
End Sub
Passing the form name as a parameter will facilitate being able to reference specific controls and properties of the form you need to validate/manipulate.
you can create a separate module for these subs and functions. As long as you set them to be public you'll be able to access them from any form in the usual manner.
Although you'll probably want to modify your subs to be functions that you can pass variables into for where the code is refering to controls.
'Keep this in the forms. Private Sub Form_BeforeUpdate(Cancel As Integer) On Error GoTo Form_BeforeUpdate_Err
Dim result as string
result = ValidateForm(me.cboFat,me.ClosingDate,me.cboLTV,me.ActualAmount,me.cmbMtgType)
if result <> 'Success' then Cancel = True me.controls(result).setfocus end if
Public Function ValidateForm(Fat,ClosingDate,LTV,ActualAmount,MtgType) as string 'Forces user to enter FAT if entry in closing date If IsNull(Fat) And Not IsNull(ClosingDate) Then MsgBox "Final Action Taken is required if a Closing Date is entered,", vbCritical, "Missing Data!" ValidateForm = "cboFat" exit function End If
'Forces user to enter Closing Date if entry in FAT If Not IsDate(ClosingDate) Then
Select Case Fat Case 1, 2, 3, 4, 5, 7 MsgBox "Based on your selection in the Final Action Taken," & _ vbCrLf & " Closing Date is a required field!", vbCritical, "Missing Data!" ValidateForm = "ClosingDate" exit function End Select End If
' Forces user to enter Actual Amount if FAT = 1 If Fat = 1 And IsNull(ActualAmount) Then MsgBox "Based on your selection in the Final Action Taken," & _ vbCrLf & " Actual Amount is a required field!", vbCritical, "Missing Data!" ValidateForm = "ActualAmount" exit function End If
If IsNull(LTV) Then
Select Case MtgType Case "2 Yr ARM", "15 Yr", "20 Yr", "25 Yr", "30 Yr" MsgBox "The selected Mortgage Type requires a LTV selection.", vbCritical, "Missing Data!" ValidateForm = "cboLTV" exit function End Select End If
I think I ran into a problem. The post above I am sure will solve my problems, but I am not sure how to handle using the code below without complicating the codes. This is what I have before I posted this question. I am trying to use this same codes on another form. If I put it in a module I am not sure how it will affect the current form that holds that code. Please advise. Thanks
Private Function FormDoesntValidate() As Boolean On Error GoTo FormDoesntValidate_Err FormDoesntValidate = False
'Forces user to enter FAT if entry in closing date If IsNull(Me.cboFat) And Not IsNull(Me.ClosingDate) Then MsgBox "Final Action Taken is required if a Closing Date is entered,", vbCritical, "Missing Data!" FormDoesntValidate = True 'Me.cboFat.SetFocus End If
' Forces user to enter Actual Amount if FAT = 1 If Me.cboFat = 1 And IsNull(Me.ActualAmount) Then MsgBox "Based on your selection in the Final Action Taken," & _ vbCrLf & " Actual Amount is a required field!", vbCritical, "Missing Data!" FormDoesntValidate = True 'ActualAmount.SetFocus End If
'Forces user to enter Closing Date if entry in FAT If Not IsDate(Me.ClosingDate) Then Select Case Me.cboFat Case 1, 2, 3, 4, 5, 7 MsgBox "Based on your selection in the Final Action Taken," & _ vbCrLf & " Closing Date is a required field!", vbCritical, "Missing Data!" FormDoesntValidate = True End Select End If
If IsNull(Me.cboLTV) Then Select Case Me.cmbMtgType Case "2 Yr ARM", "15 Yr", "20 Yr", "25 Yr", "30 Yr" MsgBox "The selected Mortgage Type requires a LTV selection.", vbCritical, "Missing Data!" FormDoesntValidate = True 'cboLTV.SetFocus End Select End If
FormDoesntValidate_Exit: Exit Function
FormDoesntValidate_Err: MsgBox Err.Number & " - " & Err.Description Resume FormDoesntValidate_Exit End Function