Improving code to check that certain controls on a form have values (1 Viewer)

brharrii

Registered User.
Local time
Today, 15:14
Joined
May 15, 2012
Messages
272
I have a bit of code that uses controls on a form to batch add records to the tables. Before the code executes, it needs to do a check to make sure that all of the needed data has been supplied. I've set something up that works, but I wonder if there might be a more preferable way to do it or if this looks good, thoughts?

Thanks!


Code:
    If IsNull(txtDateAdded) Then
        MsgBox "Please supply the date that the Label record was created"
        Exit Sub
    Else
 
    If IsNull(cboRecordCreator) Then
        MsgBox "Please supply the name of the person creating the records"
        Exit Sub
    Else
 
    If IsNull(cboRecordRequester) Then
        MsgBox "Please supply the name of the person who authorized / requested the record creation."
        Exit Sub
 
    Else
 
    If IsNull(txtNotes) Then
        MsgBox "Please supply a note regarding the creationg of these records"
        Exit Sub
 
    Else
 
    If MsgBox("Are you sure you want to upload " &  Me.List2.ItemsSelected.Count & " Records?", vbYesNo) = vbYes Then
 
 
 '''''code irrelevent to my question goes here'''''
 
 
    MsgBox ("Upload Complete.  Please verify your records.")
 
    Else
 
    MsgBox ("Upload Canceled")
 
    End If
    End If
    End If
    End If
    End If
 

Rx_

Nothing In Moderation
Local time
Today, 16:14
Joined
Oct 22, 2009
Messages
2,803
http://www.access-programmers.co.uk/forums/showthread.php?t=189844&highlight=validation

http://allenbrowne.com/highlight.html (highlight reqired fields on a form)

http://www.access-programmers.co.uk/forums/showthread.php?t=225778
Here is a useful discussion. The form has a Cancel = True for the form so validation can take place.

Another method to consider is to put a TAG in a forms control property.
Then, use code to loop throught the Forms collection of controls, filter out the ones with the correct TAG and do something. Something such as make sure they have data. So, the controls with a tag of "required"
would probably look something like this:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer) 
Dim ctrl As Control 
For Each ctrl In Me.Controls If ctrl.Tag "required" Then 
If ctrl.ControlType = acComboBox Or acBoundObjectFrame Then 
If IsNull(ctrl) Then 
MsgBox ctrl.Name & " Cannot Be Left Empty!" 
Cancel = True 
ctrl.SetFocus 
Exit Sub 
End If 
End If 
End If 
Next 
MsgBox "Record Saved!" 
End Sub
 

Users who are viewing this thread

Top Bottom