Make Next button Visible after all Required Fields are entered

  • Thread starter cw via AccessMonster.com
  • Start date
C

cw via AccessMonster.com

I have searched the forums, but cannot get an answer to my question. Most of
the posts deal with using the "BeforeUpdate" event of the Main Form to check
for "Required Fields".

What I need is:
- for the "validation check" to be done on Tabbed Form Page 01 only. (Not the
Main Form)

There are five required text fields, two bound check-boxes, and two bound
drop-downs
- once all required fields have been entered, the Me.Command5 (Next button)
should become "Visible"
- If a field is left Blank, the code needs to move the Focus to that field

Thanks,
cw
 
J

Jeanette Cunningham

Hi cw,
your form can know if tab control page 01 is open by using the value of the
tab control.
sample code-->
If Me.NameOfTabControl = 0 Then
'first page

Assuming there is not a subform on page 01 of tab control -
If the user has this page open, you can run a check on only those particular
required fields on the main form's before update event.

If you are wanting to know when the user has entries in those 5 fields
without using before update, perhaps you could set up some code on the after
update event for each or those particular controls.

Untested air code-->
If Not IsNull(Me.ControlName) Then
If Len(Me.NextCtlName1 & vbNullString) >0 Then
If Len(Me.NextCtlName2 & vbNullString) >0 Then
If Me.chk1 = True and Me.chk2 = True Then
Me.cmdNext.Visible = True
Else
Me.cmdNext.Visible = False
End If
'add the appropriate number of end ifs as needed.



Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
C

cw via AccessMonster.com

Jeanette, Thanks for the post!

If possible, I would like to know if you could answer a small issue I',
having with a solution posted by Dirk Goldgar? (See his post/code below)

- I'm using your code suggestion to just check my Tab Control page 02 (very
helpful!)
- When the user clicks on cmdNext, it checks for Tab Control page02, and then
calls Dirk's code
- Which performs the Validation on all "Required" fields, until all have been
entered

Here's my current cmdNext button
-----------------------------------------------
Private Sub cmdNext_Click()
If Me.TabCtl0 = 1 Then
'page 02
Cancel = fncRequiredFieldsMissing(Me)
End If

'Me.TabCtl0 = Me.TabCtl0 + 1
'Me.cmdNext.Enabled = True
End Sub
-----------------------------------------------

Here is my question, and the place that I'm hung:
- Where do I move my old cmdNext code (see commented lines above) to allow
the User to get to the next page?
- I think I need to move it to Dirk's code, so that if all the Required
fields are completed, then the user is taken to the next Tab Control page?

- But if I change the code in the Module, then it will only work for that
page only, and cannot be "called" if needed for other pages on my Tab Control.
?

Hope this makes sense,
Thanks,
cw

Dirk Goldgar's code:
-------------------------------
Here's a function you can paste into a standard module and call from the
form's BeforeUpdate event. It checks every text box, combo box, list
box, and check box on the form, but only if the control's Tag property
is set to "Required". That lets you designate, by tagging, those fields
that are required. The function is passed a reference to the form to be
checked and returns True if required fields are missing, as well as
displaying a message and setting the focus to the first control that is
required but empty.

'----- start of function code -----
Function fncRequiredFieldsMissing(frm As Form) As Boolean

Dim ctl As Access.Control
Dim strErrCtlName As String
Dim strErrorMessage As String
Dim lngErrCtlTabIndex As Long
Dim blnNoValue As Boolean

lngErrCtlTabIndex = 99999999 'more than max #controls

For Each ctl In frm.Controls
With ctl
Select Case .ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If .Tag = "Required" Then
blnNoValue = False
If IsNull(.Value) Then
blnNoValue = True
Else
If .ControlType = acTextBox Then
If Len(.Value) = 0 Then
blnNoValue = True
End If
End If
End If
If blnNoValue Then
strErrorMessage = strErrorMessage & vbCr & _
" " & .Name
If .TabIndex < lngErrCtlTabIndex Then
strErrCtlName = .Name
lngErrCtlTabIndex = .TabIndex
End If
End If
End If
Case Else
' Ignore this control
End Select
End With
Next ctl

If Len(strErrorMessage) > 0 Then
MsgBox "The following fields are required:" & vbCr & _
strErrorMessage, _
vbInformation, "Required Fields Are Missing"
frm.Controls(strErrCtlName).SetFocus
fncRequiredFieldsMissing = True
Else
fncRequiredFieldsMissing = False
End If

End Function
'----- end of function code -----

To use the function, create a BeforeUpdate event for your form like
this:

'----- start of event procedure code -----
Private Sub Form_BeforeUpdate(Cancel As Integer)

Cancel = fncRequiredFieldsMissing(Me)

End Sub
'----- end of event procedure code -----

Jeanette said:
Hi cw,
your form can know if tab control page 01 is open by using the value of the
tab control.
sample code-->
If Me.NameOfTabControl = 0 Then
'first page

Assuming there is not a subform on page 01 of tab control -
If the user has this page open, you can run a check on only those particular
required fields on the main form's before update event.

If you are wanting to know when the user has entries in those 5 fields
without using before update, perhaps you could set up some code on the after
update event for each or those particular controls.

Untested air code-->
If Not IsNull(Me.ControlName) Then
If Len(Me.NextCtlName1 & vbNullString) >0 Then
If Len(Me.NextCtlName2 & vbNullString) >0 Then
If Me.chk1 = True and Me.chk2 = True Then
Me.cmdNext.Visible = True
Else
Me.cmdNext.Visible = False
End If
'add the appropriate number of end ifs as needed.

Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
I have searched the forums, but cannot get an answer to my question. Most
of
[quoted text clipped - 16 lines]
Thanks,
cw
 
J

Jeanette Cunningham

Dirk's code runs in the before update event.
I'm not sure if that is correct for your form.
Before update fires when moving to a new or different record, when moving
from the main form to a subform.

If you wish to use Dirk's code, cmdNext needs to be visible.
You put the following code on cmdNext-->
If Me.Dirty = True Then
Me.Dirty = False
End If

The above 3 lines of code will trigger the form's before update event to
run.
The before update event needs to call Dirk's code to check for required
fields.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


cw via AccessMonster.com said:
Jeanette, Thanks for the post!

If possible, I would like to know if you could answer a small issue I',
having with a solution posted by Dirk Goldgar? (See his post/code below)

- I'm using your code suggestion to just check my Tab Control page 02
(very
helpful!)
- When the user clicks on cmdNext, it checks for Tab Control page02, and
then
calls Dirk's code
- Which performs the Validation on all "Required" fields, until all have
been
entered

Here's my current cmdNext button
-----------------------------------------------
Private Sub cmdNext_Click()
If Me.TabCtl0 = 1 Then
'page 02
Cancel = fncRequiredFieldsMissing(Me)
End If

'Me.TabCtl0 = Me.TabCtl0 + 1
'Me.cmdNext.Enabled = True
End Sub
-----------------------------------------------

Here is my question, and the place that I'm hung:
- Where do I move my old cmdNext code (see commented lines above) to allow
the User to get to the next page?
- I think I need to move it to Dirk's code, so that if all the Required
fields are completed, then the user is taken to the next Tab Control
page?

- But if I change the code in the Module, then it will only work for that
page only, and cannot be "called" if needed for other pages on my Tab
Control.
?

Hope this makes sense,
Thanks,
cw

Dirk Goldgar's code:
-------------------------------
Here's a function you can paste into a standard module and call from the
form's BeforeUpdate event. It checks every text box, combo box, list
box, and check box on the form, but only if the control's Tag property
is set to "Required". That lets you designate, by tagging, those fields
that are required. The function is passed a reference to the form to be
checked and returns True if required fields are missing, as well as
displaying a message and setting the focus to the first control that is
required but empty.

'----- start of function code -----
Function fncRequiredFieldsMissing(frm As Form) As Boolean

Dim ctl As Access.Control
Dim strErrCtlName As String
Dim strErrorMessage As String
Dim lngErrCtlTabIndex As Long
Dim blnNoValue As Boolean

lngErrCtlTabIndex = 99999999 'more than max #controls

For Each ctl In frm.Controls
With ctl
Select Case .ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If .Tag = "Required" Then
blnNoValue = False
If IsNull(.Value) Then
blnNoValue = True
Else
If .ControlType = acTextBox Then
If Len(.Value) = 0 Then
blnNoValue = True
End If
End If
End If
If blnNoValue Then
strErrorMessage = strErrorMessage & vbCr & _
" " & .Name
If .TabIndex < lngErrCtlTabIndex Then
strErrCtlName = .Name
lngErrCtlTabIndex = .TabIndex
End If
End If
End If
Case Else
' Ignore this control
End Select
End With
Next ctl

If Len(strErrorMessage) > 0 Then
MsgBox "The following fields are required:" & vbCr & _
strErrorMessage, _
vbInformation, "Required Fields Are Missing"
frm.Controls(strErrCtlName).SetFocus
fncRequiredFieldsMissing = True
Else
fncRequiredFieldsMissing = False
End If

End Function
'----- end of function code -----

To use the function, create a BeforeUpdate event for your form like
this:

'----- start of event procedure code -----
Private Sub Form_BeforeUpdate(Cancel As Integer)

Cancel = fncRequiredFieldsMissing(Me)

End Sub
'----- end of event procedure code -----

Jeanette said:
Hi cw,
your form can know if tab control page 01 is open by using the value of
the
tab control.
sample code-->
If Me.NameOfTabControl = 0 Then
'first page

Assuming there is not a subform on page 01 of tab control -
If the user has this page open, you can run a check on only those
particular
required fields on the main form's before update event.

If you are wanting to know when the user has entries in those 5 fields
without using before update, perhaps you could set up some code on the
after
update event for each or those particular controls.

Untested air code-->
If Not IsNull(Me.ControlName) Then
If Len(Me.NextCtlName1 & vbNullString) >0 Then
If Len(Me.NextCtlName2 & vbNullString) >0 Then
If Me.chk1 = True and Me.chk2 = True Then
Me.cmdNext.Visible = True
Else
Me.cmdNext.Visible = False
End If
'add the appropriate number of end ifs as needed.

Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
I have searched the forums, but cannot get an answer to my question. Most
of
[quoted text clipped - 16 lines]
Thanks,
cw
 
C

cw via AccessMonster.com

When I use:
------------------------
If Me.TabCtl0 = 1 Then
'page 02
Cancel = fncRequiredFieldsMissing(Me)
End If
------------------------

It properly validates the empty fields..that part works great!
But once all the fields pass validation, I need to move the user on to Page
03..

Here is the flow:
--> User presses cmdNext
--> Code pops up a box saying "Text1 is Empty"
--> User fills in Text1 field, clicks cmdNext
--> This time the code runs thru w/o prompting and moves on to Page 03
--> The code to move to Page 03 is: Me.TabCtl0 = Me.TabCtl0 + 1

By the way, with Dirk's code, I do not need to make cmdNext invisible (I
originally only did that to keep the user from moving on until the last field
on the form was filled in)

Thanks again,
cw



Jeanette said:
Dirk's code runs in the before update event.
I'm not sure if that is correct for your form.
Before update fires when moving to a new or different record, when moving
from the main form to a subform.

If you wish to use Dirk's code, cmdNext needs to be visible.
You put the following code on cmdNext-->
If Me.Dirty = True Then
Me.Dirty = False
End If

The above 3 lines of code will trigger the form's before update event to
run.
The before update event needs to call Dirk's code to check for required
fields.

Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
Jeanette, Thanks for the post!
[quoted text clipped - 152 lines]
 
J

Jeanette Cunningham

Put the code to move to page 03 on the code for cmdNext.
-->
If Me.Dirty = True Then
Me.Dirty = False
End If
Me.TabCtl0 = Me.TabCtl0 + 1


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia



cw via AccessMonster.com said:
When I use:
------------------------
If Me.TabCtl0 = 1 Then
'page 02
Cancel = fncRequiredFieldsMissing(Me)
End If
------------------------

It properly validates the empty fields..that part works great!
But once all the fields pass validation, I need to move the user on to
Page
03..

Here is the flow:
--> User presses cmdNext
--> Code pops up a box saying "Text1 is Empty"
--> User fills in Text1 field, clicks cmdNext
--> This time the code runs thru w/o prompting and moves on to Page 03
--> The code to move to Page 03 is: Me.TabCtl0 = Me.TabCtl0 + 1

By the way, with Dirk's code, I do not need to make cmdNext invisible (I
originally only did that to keep the user from moving on until the last
field
on the form was filled in)

Thanks again,
cw



Jeanette said:
Dirk's code runs in the before update event.
I'm not sure if that is correct for your form.
Before update fires when moving to a new or different record, when moving
from the main form to a subform.

If you wish to use Dirk's code, cmdNext needs to be visible.
You put the following code on cmdNext-->
If Me.Dirty = True Then
Me.Dirty = False
End If

The above 3 lines of code will trigger the form's before update event to
run.
The before update event needs to call Dirk's code to check for required
fields.

Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
Jeanette, Thanks for the post!
[quoted text clipped - 152 lines]
Thanks,
cw
 
C

cw via AccessMonster.com

Jeanette, you have done it again!

That works perfectly..Thanks!

Sorry I was stuck on thinking I had to use the "Cancel =
fncRequiredFieldsMissing(Me)"..?

Jeanette said:
Put the code to move to page 03 on the code for cmdNext.
-->
If Me.Dirty = True Then
Me.Dirty = False
End If
Me.TabCtl0 = Me.TabCtl0 + 1

Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
When I use:
------------------------
[quoted text clipped - 47 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top