Is Null Fields - Message Box

on-2

Registered User.
Local time
Today, 20:52
Joined
Nov 24, 2002
Messages
34
Does anyone know a nicer way of writing this. I basically want the form to quickly check all the fields to make sure they are filled in when the print button is clicked, different fields need to be filled in for different print buttons so i used this which works but seems very messy.
Private Sub Command0_Click()

If IsNull(reg) Then

MsgBox "You must enter Reg", vbCritical, "Data Required"

Else

If IsNull(make) Then

MsgBox "You must enter Make", vbCritical, "Data Required"

Else

If IsNull(model) Then

MsgBox "You must enter Model", vbCritical, "Data Required"

End If
End If

Exit Sub
End If



End Sub
 
AFAIK the only nicer method would be to show all of the missing information and set the focus to one of the controls that needs to be filled in:
Code:
Private Sub Form_Unload(Cancel As Integer)
'-- Make sure at least the minimum fields are completed
On Error GoTo Err_Form_Unload

Dim MyMessage As String, ctlWithFocus As Control, Answer As Integer
MyMessage = ""

If Not Deleted Then
    If Nz(Me.optStorage, 0) = 0 Then
        MyMessage = "The Storage Requirements must be set!" & vbCrLf & vbCrLf & MyMessage
        Set ctlWithFocus = Me.optStorage
    End If
    If Me.txtUnitCase = 0 Then
        MyMessage = "The Units per case must be set!" & vbCrLf & vbCrLf & MyMessage
        Set ctlWithFocus = Me.txtUnitCase
    End If
    If (Me.cboUnits & "") = "" Then
        MyMessage = "The Measurement Unit must be set!" & vbCrLf & vbCrLf & MyMessage
        Set ctlWithFocus = Me.cboUnits
    End If
    If Me.cboCategory.Column(0) = 27 Then
        MyMessage = "The Expensing Category has not been set!" & vbCrLf & vbCrLf & MyMessage
        Set ctlWithFocus = Me.cboCategory
    End If
    If (Me.txtSupplierDesc & "") = "" Then
        MyMessage = "The Description has not been completed!" & vbCrLf & vbCrLf & MyMessage
        Set ctlWithFocus = Me.txtSupplierDesc
    End If
    If (Me.txtProdName & "") = "" Then
        MyMessage = "The Product Name has not been completed!" & vbCrLf & vbCrLf & MyMessage
        Set ctlWithFocus = Me.txtProdName
    End If
    If Len(MyMessage) <> 0 Then
        MyMessage = MyMessage & vbCrLf & vbCrLf & _
                    "Do you wish to DELETE this Product?"
        Answer = MsgBox(MyMessage, vbYesNo)
        If Answer = vbNo Then
            ctlWithFocus.SetFocus
            If (ctlWithFocus = Me.cboCategory) Or (ctlWithFocus = Me.cboUnits) Then
                ctlWithFocus.Dropdown
            End If
            Cancel = True
        Else
            Cancel = Not DeleteProduct()
        End If
    End If
Else
    Cancel = False
End If

If Not Cancel Then
    If Not IsNull(Me.OpenArgs) Then
        Forms(CallingForm).UpdateProductList
        Forms(CallingForm).Visible = True
    End If
End If

Exit_Form_Unload:
    On Error Resume Next
    Set ctlWithFocus = Nothing
    Exit Sub

Err_Form_Unload:
    Call LogError(Err.Number, Err.Description, "Form_Unload() in " & Me.Name)
    Resume Exit_Form_Unload

End Sub
I just copied this out of one of my systems. It has extra Stuff in it that makes no sense so just ignore it. Basically you get the idea.
 
If these fields are required, all the code belongs in your form's BeforeUpdate event. Just before you open a form or report, it is necessary to save the current record. Otherwise the form/report will not pick it up. To save the current record:
DoCmd.RunCommand acCmdSaveRecord

Saving the record will cause the BeforeUpdate event to run and that will cause your edits to run.
 

Users who are viewing this thread

Back
Top Bottom