A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Using Forms
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Checkbox (default to no) - must be checked before saving/exiting



 
 
Thread Tools Display Modes
  #1  
Old October 26th, 2009, 01:21 PM posted to microsoft.public.access.forms
nicolam1980
external usenet poster
 
Posts: 5
Default Checkbox (default to no) - must be checked before saving/exiting

Hi

I have a feeling this might be a basic question but am new to access so
please be patient!!

I am creating a database for a team at work to use instead of paper
checklists. One such section I am creating concerns priority checks - I have
Yes/No boxes on the form that are defaulted to No.

I need to ensure people cannot save a record or exit the DB if any of these
boxes are left unchecked - can anyone help?

Thanks

Nicola
  #2  
Old October 26th, 2009, 01:55 PM posted to microsoft.public.access.forms
Tom van Stiphout[_2_]
external usenet poster
 
Posts: 1,653
Default Checkbox (default to no) - must be checked before saving/exiting

On Mon, 26 Oct 2009 06:21:02 -0700, nicolam1980
wrote:

The appropriate place to make this check is in Form_BeforeUpdate,
because you can set Cancel=True which prevents user from leaving the
form.
if me.myCheckbox1.value = false
or me.myCheckbox2.value = false
or me.myCheckbox3.value = false
then Cancel = True

If you literally mean "all checkboxes" you could alternatively write:
dim ctl as control
for each ctl in me.controls
if typeof(ctl) is checkbox then
if ctl.Value = false then
Cancel = True
Exit For
end if
end if
next

(of course you replace myObjectNames with yours)

-Tom.
Microsoft Access MVP


Hi

I have a feeling this might be a basic question but am new to access so
please be patient!!

I am creating a database for a team at work to use instead of paper
checklists. One such section I am creating concerns priority checks - I have
Yes/No boxes on the form that are defaulted to No.

I need to ensure people cannot save a record or exit the DB if any of these
boxes are left unchecked - can anyone help?

Thanks

Nicola

  #3  
Old October 26th, 2009, 03:00 PM posted to microsoft.public.access.forms
Arvin Meyer [MVP][_2_]
external usenet poster
 
Posts: 2,310
Default Checkbox (default to no) - must be checked before saving/exiting


"nicolam1980" wrote in message
...
Hi

I have a feeling this might be a basic question but am new to access so
please be patient!!

I am creating a database for a team at work to use instead of paper
checklists. One such section I am creating concerns priority checks - I
have
Yes/No boxes on the form that are defaulted to No.

I need to ensure people cannot save a record or exit the DB if any of
these
boxes are left unchecked - can anyone help?


How about something like (untested):

Sub Form_BeforeUpdate(Cancel As Integer)
Dim i As Integer
For i = 0 to Me.Controls.Count - 1
If TypeOf Me.Controls(i) Is acCheckbox Then
If Me.Controls(i).Value = False Then
MsgBox "Please fill in all Checkboxes", vbOKOnly
Cancel = True
Me.Controls(i).SetFocus
Exit Sub
End If
End If
Next I
End Sub
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com


  #4  
Old October 27th, 2009, 02:45 AM posted to microsoft.public.access.forms
Tom van Stiphout[_2_]
external usenet poster
 
Posts: 1,653
Default Checkbox (default to no) - must be checked before saving/exiting

On Mon, 26 Oct 2009 10:00:35 -0500, "Arvin Meyer [MVP]"
wrote:

Ough, Exit Sub out of a For loop. Efficient and allowed, but doesn't
get the elegance price.

-Tom.
Microsoft Access MVP



"nicolam1980" wrote in message
...
Hi

I have a feeling this might be a basic question but am new to access so
please be patient!!

I am creating a database for a team at work to use instead of paper
checklists. One such section I am creating concerns priority checks - I
have
Yes/No boxes on the form that are defaulted to No.

I need to ensure people cannot save a record or exit the DB if any of
these
boxes are left unchecked - can anyone help?


How about something like (untested):

Sub Form_BeforeUpdate(Cancel As Integer)
Dim i As Integer
For i = 0 to Me.Controls.Count - 1
If TypeOf Me.Controls(i) Is acCheckbox Then
If Me.Controls(i).Value = False Then
MsgBox "Please fill in all Checkboxes", vbOKOnly
Cancel = True
Me.Controls(i).SetFocus
Exit Sub
End If
End If
Next I
End Sub

  #5  
Old October 27th, 2009, 02:30 PM posted to microsoft.public.access.forms
Arvin Meyer [MVP][_2_]
external usenet poster
 
Posts: 2,310
Default Checkbox (default to no) - must be checked before saving/exiting

After lookinig at the code, I see a mistake which needs correcting, try
(also untested):

Sub Form_BeforeUpdate(Cancel As Integer)
Dim i As Integer
For i = 0 to Me.Controls.Count - 1
If Me.Controls(i).ControlType is acCheckbox Then
If Me.Controls(i).Value = False Then
MsgBox "Please fill in all Checkboxes", vbOKOnly
Cancel = True
Me.Controls(i).SetFocus
Exit Sub
End If
End If
Next I
End Sub
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com



"Arvin Meyer [MVP]" wrote in message
...

"nicolam1980" wrote in message
...
Hi

I have a feeling this might be a basic question but am new to access so
please be patient!!

I am creating a database for a team at work to use instead of paper
checklists. One such section I am creating concerns priority checks - I
have
Yes/No boxes on the form that are defaulted to No.

I need to ensure people cannot save a record or exit the DB if any of
these
boxes are left unchecked - can anyone help?


How about something like (untested):

Sub Form_BeforeUpdate(Cancel As Integer)
Dim i As Integer
For i = 0 to Me.Controls.Count - 1
If TypeOf Me.Controls(i) Is acCheckbox Then
If Me.Controls(i).Value = False Then
MsgBox "Please fill in all Checkboxes", vbOKOnly
Cancel = True
Me.Controls(i).SetFocus
Exit Sub
End If
End If
Next I
End Sub
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com



  #6  
Old October 27th, 2009, 02:39 PM posted to microsoft.public.access.forms
Arvin Meyer [MVP][_2_]
external usenet poster
 
Posts: 2,310
Default Checkbox (default to no) - must be checked before saving/exiting

Still messed it up (same line too, but this time it's tested):

If Me.Controls(i).ControlType is acCheckbox Then

should be:

If Me.Controls(i).ControlType = acCheckbox Then

--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com


"Arvin Meyer [MVP]" wrote in message
...
After lookinig at the code, I see a mistake which needs correcting, try
(also untested):

Sub Form_BeforeUpdate(Cancel As Integer)
Dim i As Integer
For i = 0 to Me.Controls.Count - 1
If Me.Controls(i).ControlType is acCheckbox Then
If Me.Controls(i).Value = False Then
MsgBox "Please fill in all Checkboxes", vbOKOnly
Cancel = True
Me.Controls(i).SetFocus
Exit Sub
End If
End If
Next I
End Sub
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com



"Arvin Meyer [MVP]" wrote in message
...

"nicolam1980" wrote in message
...
Hi

I have a feeling this might be a basic question but am new to access so
please be patient!!

I am creating a database for a team at work to use instead of paper
checklists. One such section I am creating concerns priority checks - I
have
Yes/No boxes on the form that are defaulted to No.

I need to ensure people cannot save a record or exit the DB if any of
these
boxes are left unchecked - can anyone help?


How about something like (untested):

Sub Form_BeforeUpdate(Cancel As Integer)
Dim i As Integer
For i = 0 to Me.Controls.Count - 1
If TypeOf Me.Controls(i) Is acCheckbox Then
If Me.Controls(i).Value = False Then
MsgBox "Please fill in all Checkboxes", vbOKOnly
Cancel = True
Me.Controls(i).SetFocus
Exit Sub
End If
End If
Next I
End Sub
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com





  #7  
Old October 27th, 2009, 02:42 PM posted to microsoft.public.access.forms
Arvin Meyer [MVP][_2_]
external usenet poster
 
Posts: 2,310
Default Checkbox (default to no) - must be checked before saving/exiting

"Tom van Stiphout" wrote in message
...
On Mon, 26 Oct 2009 10:00:35 -0500, "Arvin Meyer [MVP]"
wrote:

Ough, Exit Sub out of a For loop. Efficient and allowed, but doesn't
get the elegance price.

-Tom.
Microsoft Access MVP


Perhaps not, I could have also used:

Exit For

to get out of the loop. Surely you were not suggesting that the loop
continue through every control, even after it found 1?
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com


  #8  
Old October 27th, 2009, 07:51 PM posted to microsoft.public.access.forms
David W. Fenton
external usenet poster
 
Posts: 3,373
Default Checkbox (default to no) - must be checked before saving/exiting

"Arvin Meyer [MVP]" wrote in
:

After lookinig at the code, I see a mistake which needs
correcting, try (also untested):

Sub Form_BeforeUpdate(Cancel As Integer)
Dim i As Integer
For i = 0 to Me.Controls.Count - 1
If Me.Controls(i).ControlType is acCheckbox Then
If Me.Controls(i).Value = False Then
MsgBox "Please fill in all Checkboxes", vbOKOnly
Cancel = True
Me.Controls(i).SetFocus
Exit Sub
End If
End If
Next I
End Sub


I'd still change it according to Tom's comment about Exit Sub in the
middle of a Loop.

Dim ctl As Control
For Each ctl in Me.Controls
If ctl.ControlType = acCheckbox Then
If Not (ctl) Then
MsgBox "Please fill in all Checkboxes", vbOKOnly
Cancel = True
ctl.SetFocus
End If
If Cancel = True Then
Exit For
End If
End If
Set ctl = Nothing
Next I

Also, this is exactly the kind of place where it would be vastly
speeded up if you populated a custom collection in the form's OnLoad
event, and walked it instead of the whole Controls collection. You
then wouldn't have to check anything but the value.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 12:55 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.