Solved

Validateion text needed - one of two fields on form must be filled out

Posted on 2008-11-02
9
252 Views
Last Modified: 2008-11-02
Hi experts!  I have a form with two checkboxes, each with their own field.  They are "Male" and "Female."  I need to ensure that before closing the form that one of the fields has been checked.  Any ideas?
0
Comment
Question by:slamboy
  • 4
  • 3
  • 2
9 Comments
 
LVL 4

Expert Comment

by:Rodger
ID: 22861495
You would need to but some code on the onClose Event of the form.  I would also remove the close button on the form.  Is this the only form or is it part of larger form?
As a note to making sure you have the structure correct.  Is male and female their own fields or do you have one field for gender?  Also you might want to use radio button in a group that way the user can only select one.  A checkbox implies that the user can make more then one selection.
0
 
LVL 7

Expert Comment

by:Valleriani
ID: 22861512
Hey there mate!

I provided a sample, javascript will be used here to validate the script.  You can try the script out. I used radio buttons, as I suppose I think you ment that? Checkboxes allow multiple selections, and I don't think they can be both male and female :)

Some small things:

When declaring, use 'document.myformname.radiobuttonname[x] ... In my example you will see myformname is 'myform', radiobuttonname is 'Gender' and [x] will be 0 and 1, because we have TWO optionbuttons. If we had more, we would continue this, [2],[3] etc.

If you want to validate more then just radio buttons, check out this:

http://www.yourhtmlsource.com/javascript/formvalidation.html

Hope this helps!
<form name="myform" method="post" onsubmit="return checkform(this);">
<input type="radio" name="Gender" value="Male"> Male<br>
<input type="radio" name="Gender" value="Female"> Female<br>
<input type="submit" value="GO">
</form>
 
<script language="JavaScript" type="text/javascript">
<!--
function checkform ( form )
{
  // ** START **
	if (!document.myform.Gender[0].checked &&
	!document.myform.Gender[1].checked) {
			alert( "Please enter gender!" );
		return false;
	}
 
  // ** END **
  return true ;
}
//-->
</script>

Open in new window

0
 

Author Comment

by:slamboy
ID: 22861539
RodgerDjr,

I am looking for a code example here.  Yes, they are separate fields.  I should have probably used radio buttons but I'm too far down the road to change things now (lots & lots of queries).  I have this situation with forms and subforms.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 7

Expert Comment

by:Valleriani
ID: 22861617
Here then, if you want for checkboxes, don't think its a good idea, but I added a extra check to make sure that both are not checked as well.
<form name="myform" method="post" onsubmit="return checkform(this);">
<INPUT TYPE=CHECKBOX NAME="Male">Male<P>
<INPUT TYPE=CHECKBOX NAME="Female">Female<P>
<input type="submit" value="GO">
</form>
 
<script language="JavaScript" type="text/javascript">
<!--
function checkform ( form )
{
  // ** START **
	if (!document.myform.Male.checked && !document.myform.Female.checked) {
		 alert( "Please enter gender!" );
		return false;
	}
	if (document.myform.Male.checked && document.myform.Female.checked) {
		 alert( "You cannot select both genders!" );
		return false;
	}
  // ** END **
  return true ;
}
//-->
</script>

Open in new window

0
 
LVL 4

Expert Comment

by:Rodger
ID: 22861698
i still would use radio buttons, they will not harm the queries.
You would just need to use an if statement.
If isNull(me.checkbox1) or isNull(me.checkbox2) then
     msgbox('You must select male or female", vbOKOnly, "Your Title here")
     docmd.CancelEvent
else
end if
0
 
LVL 4

Expert Comment

by:Rodger
ID: 22861737
Vallerini, this is an MS Access fourm not Javascript

:)
0
 
LVL 7

Expert Comment

by:Valleriani
ID: 22861750
Ack! Sorry! I totally must of clicked the wrong area, my bad.
0
 

Author Comment

by:slamboy
ID: 22861796
RodgerDjr, this is exactly what I was looking for.  However I forgot I have a macro that runs when the form closes.  Any ideas on how I could still run this check?
0
 
LVL 4

Accepted Solution

by:
Rodger earned 150 total points
ID: 22861813
Have the macro run the code.  I do not do much with Macors just VBA Code.  I am pretty sure there is a command RunCode.  Or just put that code on the obClose event of the form.  Macros and code can work together.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question