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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkboxes 2

Status
Not open for further replies.

nk9100

Technical User
May 13, 2005
67
GB
After reading the FAQs, they specifically relate to a series of checkboxes which have the same group name, I am wonering how I might get the value of the checkbox from the DB, and get the box to be checked.

I tried an If Statement, which didnt work...

Code:
<%
If IsNull(rsCheckUser("educationrecruitmentdifficulties")) Then
%>
<td width="9%">
<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes">
</td>
<%
Else
%>
<%
If rsCheckUser("educationrecruitmentdifficulties") = "Yes" Then
%>
<td width="9%">
<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes" checked>
</td>
<%
End If
%>

Any Ideas?

Life is a journey that always ends up in the same place
 
What I tend to do is generate a text value in my sql command. Somthing like this

SELECT IIF(educationrecruitmentdifficulties, ' CHECKED', '') as ERD FROM myTable"
set rsCheckUser = myConn.Execute(..........)

Then for your checkbox just put the following:

<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes"<%=rsCheckUser("educationrecruitmentdifficulties")%>>



rsCheckUser("educationrecruitmentdifficulties"))

Mighty
 
Sorry slight mistake there - checkbox code should have been:

<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes"<%=rsCheckUser("ERD")%>>

and ignore the last line in the previous post


Mighty
 
Right...I sort of see where you are coming from, but wont the value of the check need to be CHECKED?, as this is an updateble page, will that work, AND, (sorry for the questions), I have about 8 checkboxes, so will this scale up?, i dont have time to do massive amounts of testing you see, just basic, it was supposed to be live last Tuesday!, got until about 2pm to have it sorted1, thanks for your help in advance

Life is a journey that always ends up in the same place
 
Sorry, you don't really need the value field. Just put:

<input type="checkbox" name="educationrecruitmentdifficulties"<%=rsCheckUser("ERD")%>>

If you have 10 users, just loop through it

sql = "SELECT username, IIF(educationrecruitmentdifficulties, ' CHECKED', '') as ERD FROM myTable"
set rsCheckUser = myConn.Execute(sql, , 1)
%>
<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>
<TR>
<TH>User Name</TH>
<TH>Difficulties</TH>
</TR>
<%
do while not rsCheckUser.bof
%>
<TR>
<TD><%=rsCheckUser("username")%></TD>
<TD><input type="checkbox" name="educationrecruitmentdifficulties"<%=rsCheckUser("ERD")%>></TD>
</TR>
<%
rsCheckUser.MoveNext

Loop
%>
</TABLE>



I hope that gives you a good picture of what's possible. If the educationrecruitmentdifficulties field is ticked in the database then the checkbox on the form will be checked. You can do this for any number of records.

Hope that helps.

Mighty
 
Hi thanks again, it isnt really number of users, more number of boxes, each with a different name/value, so that would be quite a heavy piece of code per box would it not?

Life is a journey that always ends up in the same place
 
Are you pulling all the values with one query. All you need to do is write the query and then use the value from the query to set the Checked property. For example:

sql = "SELECT IIF(educationrecruitmentdifficulties, ' CHECKED', '') as ERD, IIF(otherDifficulty1, ' CHECKED', '') as OD1, IIF(otherDifficulty2, ' CHECKED', '') as OD2 FROM myTable"
set rsCheckUser = myConn.Execute(sql, , 1)

Then your checkbox code will be:

<input type="checkbox" name="educationrecruitmentdifficulties"<%=rsCheckUser("ERD")%>>
<input type="checkbox" name="OtherDifficulty1"<%=rsCheckUser("OD1")%>>
<input type="checkbox" name="OtherDifficulty2"<%=rsCheckUser("OD2")%>>


Does that make sense??

Mighty
 
Yeah, that makes perfect sense!, thanks alot....I will go and try it, just needed that little extra clarification.

Thanks Again

Life is a journey that always ends up in the same place
 
i would suggest doing this as you mentioned in the original post....but a little different
Code:
<input type="checkbox" name="yourname1" <%=if rs("yourname1")<>"" Then response.write "CHECKED" value="Yes">

<input type="checkbox" name="yourname2" <%=if rs("yourname2")<>"" Then response.write "CHECKED" value="Yes">

-DNG
 
oops...i missed ending tags...

Code:
<input type="checkbox" name="yourname1" <%=if rs("yourname1")<>"" Then response.write "CHECKED"%> value="Yes">

<input type="checkbox" name="yourname2" <%=if rs("yourname2")<>"" Then response.write "CHECKED"%> value="Yes">

Mighty, How would you do the same query in SQL Server...i dont thin IIF works...may be case statements...

if you are having multiple checkboxes just doing the check at the input level is better than doing at the query level...and if there are around 20 checkboxes...just imagine how your query looks with all those case statements...

-DNG
 
Hi DNG,

Don't know about SQL Server - have never used it so not sure about syntax.

With a lot of checkboxes the sql query will end up looking a little crazy. I suppose it ends being being a personal thing - which way do you prefer to do it!! I use that code quite a lot and it works fine for me.

Mighty
 
yes, its a personal thing...i agree...

the equivalent of IIF in SQL Server is case...if nk9100 wants to write it...then it would look something like this
Code:
SELECT
(Case field1 WHEN IS NULL then 'CHECKED' ELSE '' END) AS myfield1
(Case field2 WHEN IS NULL then 'CHECKED' ELSE '' END) AS myfield2
....
...

-DNG
 
Hi, without going against the flow or anything, I did it like this,

Code:
<%
If IsNull(rsCheckUser("educationrecruitmentdifficulties")) Then
%>
<p>
<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes">
</p>
<%
Else
%>
<%
If rsCheckUser("educationrecruitmentdifficulties") = "Yes" Then
%>
<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes" Checked>

<%
else 

%>
<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes">

<%
End If
End If
%>
<%
If IsNull(rsCheckUser("educationrecruitmentdifficulties")) Then
%>
<p>
<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes">
</p>
<%
Else
%>
<%
If rsCheckUser("educationrecruitmentdifficulties") = "Yes" Then
%>
<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes" Checked>

<%
else 

%>
<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes">

<%
End If
End If
%>

The If isNull didnt work first time, so the last unchecked box is displayed at the end, and it works fine,

Life is a journey that always ends up in the same place
 
It would have done if I hadnt of just copied and pasted the code twice. Sorry guys

Code:
<%
If IsNull(rsCheckUser("educationrecruitmentdifficulties")) Then
%>
<p>
<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes">
</p>
<%
Else
%>
<%
If rsCheckUser("educationrecruitmentdifficulties") = "Yes" Then
%>
<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes" Checked>

<%
else 

%>
<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes">

<%
End If
End If
%>

Life is a journey that always ends up in the same place
 
nk9100,

did you consider doing it the way i suggested...you dont need else conditions...

-DNG
 
i mean you can just do this:

<input type="checkbox" name="educationrecruitmentdifficulties" <%If IsNull(rsCheckUser("educationrecruitmentdifficulties")) THEN response.write "CHECKED"%> value="Yes">

-DNG
 
Yeah, I was experimenting, I am going to have go with that.

I have the problem that an empty field in Access is actually "", rather than just blank.

So, to come over that I had to put
Code:
<%
If IsNull(rsCheckUser("educationrecruitmentdifficulties")) or rsCheckUser("educationrecruitmentdifficulties") = "" Then
%>
<p>
<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes">ghgfdgfd
</p>
<%
Else
%>
<%
If rsCheckUser("educationrecruitmentdifficulties") = "Yes" Then
%>
<input type="checkbox" name="educationrecruitmentdifficulties" value="Yes" Checked>


<%
End If
End If
%>



Life is a journey that always ends up in the same place
 
DotNetGnat -

Your solution works straight away, without the "" part included.....why is that?

Life is a journey that always ends up in the same place
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top