Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A Better Way to do this?

Status
Not open for further replies.

jontout

Technical User
Dec 29, 2006
95
GB
Hi.
I have a form with numerous text boxes for a data capture solution which has 11 textfields, 3 auto populated, 7 for mandatory replies and 1 for an optional response.
This app works well but I'm wondering if there's a better solution for error trapping blank fields rather than this...

Code:
If txtLogDate.Value = "" Then lblMessage.Caption = "Please Enter Date": Exit Sub
If txtItem.Value = "" Then lblMessage.Caption = "Please Enter Item Number": Exit Sub
If txtCaption.Value = "" Then lblMessage.Caption = "Please Enter A Caption": Exit Sub
If txtObserve.Value = "" Then lblMessage.Caption = "Please Enter Observations": Exit Sub
If txtObsRec.Value = "" Then lblMessage.Caption = "Please Enter Recommendations": Exit Sub
If txtDeadLine.Value = "" Then lblMessage.Caption = "Please Set Deadline": Exit Sub
If txtManResp.Value = "" Then lblMessage.Caption = "Please Enter A Response": Exit Sub

Any ideas? I'm happy with the functionality, but it looks a bit messy as outside of the textboxes, there's also a number of Combo boxes and check boxes that are being trapped in a similar way, and feeding back to a caption label on the capture form.

Cheers,

Jon
 
you could put the message in the controls tag property

if not isdatavalid then
exit sub
else
some code
end if

Code:
Public Function isDataValid() As Boolean
  Dim ctl As Access.Control
  isDataValid = True
  For Each ctl In Me.Controls
    If Not ctl.Tag = "" Then
      If Trim(ctl.Value & " ") = "" Then
        lblMessage.Caption = ctl.Tag
        isDataValid = False
        Exit Function
      End If
    End If
  Next ctl
End Function
 
In Access, empty fields are usually Null, not Zero-Length Strings, which is what you are checking for with the lines like

txtLogDate.Value = ""

To check for Null you can use something like

If Nz(Me.txtLogDate, "") = ""




The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top