Check if mandatory text boxes and empty (1 Viewer)

josephbupe

Registered User.
Local time
Tomorrow, 00:25
Joined
Jan 31, 2008
Messages
247
Hi,


I have a form with text boxes for adding new Event. I want to check if the mandatory ones have data in them.


I need this to ensure that the subform used to add participants to the Events is visible only when the mandatory text boxes have data.


How can this be done?
 
Last edited:

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:25
Joined
Aug 30, 2003
Messages
36,125
This should help:

http://www.baldyweb.com/BeforeUpdate.htm

If you have several and want to check them before making the subform visible, I'd make a function and call it from the after update event of the relevant textboxes.
 

josephbupe

Registered User.
Local time
Tomorrow, 00:25
Joined
Jan 31, 2008
Messages
247
This should help:

http://www.baldyweb.com/BeforeUpdate.htm

If you have several and want to check them before making the subform visible, I'd make a function and call it from the after update event of the relevant textboxes.


So, for each control I should include in that code:


Code:
If Len(Me.txtEven & vbNullString) And (Me.txtStartDate & vbNullString) And (Me.txtEndDate & vbNullString) = 0 Then
MsgBox "You need to fill out SomeControl"
  Cancel = True
  Me.SomeControl.SetFocus
End If
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:25
Joined
Aug 30, 2003
Messages
36,125
Well, you have to test each control, you need OR not AND, and you're doing something different, so more like

Code:
If Len(Me.txtEven & vbNullString) = 0 OR Len(Me.txtStartDate & vbNullString) = 0 OR Len(Me.txtEndDate & vbNullString) = 0 Then
  'make subform visible
Else
  'hide it
End If
 

josephbupe

Registered User.
Local time
Tomorrow, 00:25
Joined
Jan 31, 2008
Messages
247
Okay, just to test it, I have put this code on the openform event. The current record on the form already has all the mandatory field populated, but the subform is not visible:


Code:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Form_Open

If Len(Me.txtEvent & vbNullString) = 0 Or Len(Me.txtStartDate & vbNullString) = 0 Or Len(Me.txtEndDate & vbNullString) = 0 Or Len(Me.ComboStaff & vbNullString) = 0 Or Len(Me.ComboCountry & vbNullString) = 0 Or Len(Me.ComboCrime & vbNullString) = 0 Or Len(Me.ComboCategory & vbNullString) = 0 Then
   Me.subfrmParticipants.Visible = True
Else
   Me.subfrmParticipants.Visible = False
End If

  Me.txtEvent_ID.SetFocus

Exit_Form_Open:
    Exit Sub
    
Err_Form_Open:
    MsgBox err.Description
   Resume Exit_Form_Open
End Sub
 

josephbupe

Registered User.
Local time
Tomorrow, 00:25
Joined
Jan 31, 2008
Messages
247
Oh, please pardon me, I am so sorry. It works just fine.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:25
Joined
Aug 30, 2003
Messages
36,125
Yeah, the open event is too soon. Like I said, I'd probably make a form-level function and call it from the current event of the form and the after update event of those 3 controls.
 

Users who are viewing this thread

Top Bottom