Home|Sitemap|Contact

How can you retrieve a YES/NO (TRUE/FALSE) field from a MS Access database and show its value in a checkbox on a web form?

In the example database there are two columns, Name and Understands. Understands is a YES/NO field.  From the drop down list you may select a name which is in the example table, submit the form and the value of the Understands column will be shown in the checkbox.  You may also choose to change the checkbox's value and submit the change to the database.  The relevant code is below the example.

  Understands (YES=checked  NO=unchecked)



<!--METADATA TYPE="typelib" uuid="00000205-0000-0010-8000-00AA006D2EA4" -->
<%
Option Explicit
Session.CodePage=65001
Response.Charset="UTF-8"
%>
<!--#include virtual="include/paramSQL.asp"-->
<%
' We will get the selected name from the form
' and use it to look up the value of the 
' YES/No (TRUE/FALSE) field in the database.
' If the field is "Yes" (or "True")
' we will make the form's checkbox checked.

Dim locName, checked

' get the name submitted
if request.form("name")<>"" then
    locName = LCase(Trim(request.form("name")))
else
    ' default to John
    locName = "John"
end if

if request.form("change")="Submit Change to Database" then
  ' submit change
  locUnderstands = request.form("understands")
  if locUnderstands<>"True" then
    locUnderstands="False"
  end if
  submitChanges locName, locUnderstands
end if

' look it up in the database and return an
' empty string or "CHECKED".
checked = getCheckedByName(locName)
function getCheckedByName(pName)
  ' This function uses the paramSQL.asp library
  ' The library just simplifies database access,
  ' enforces error handling and adds some debug
  ' information for the developer. A link is
  ' provided at the end of the code.
  Dim cmdText
  getCheckedByName=""

  cmdText = "SELECT understands FROM yesnoexample WHERE ([name] = @name);"

  openCommand Application("examples"),"getCheckedByName"
  addParam "@name",adVarChar,adParamInput,CLng(50),pName,"getCheckedByName 1"
  getRS db_rs, cmdText, "getCheckedByName"
  if NOT (db_rs.BOF AND db_rs.EOF) then
    '**************************************************
    '* The important part. If "True" return "CHECKED" *
    '**************************************************
    if db_rs("understands") = "True" then
      getCheckedByName="CHECKED"
    end if
  end if 
  closeCommand
end function

sub submitChanges(pName, pUnderstands)
  Dim cmdText

  ' Don't try to set a parameter for the True/False (Yes/No) value.
  ' Hard write the value into the SQL command.
  if CBool(pUnderstands)=true then
    cmdText = "UPDATE yesnoexample SET understands=True WHERE ([name] = @name);"
  else
    cmdText = "UPDATE yesnoexample SET understands=False WHERE ([name] = @name);"
  end if
  openCommand Application("examples"),"submitChanges 1"
  addParam "@name",adVarChar,adParamInput,CLng(50),pName,"submitChanges 2"
  execCmd cmdText
  closeCommand
end sub
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="author" content="Roderick Divilbiss">
<meta name="copyright" content="© 2005, 2006 Roderick Divilbiss">
<title>display access checkboxes in a web form</title>
</head>
<body>
<form method="POST" action="access_checkbox.asp">
<p><input type="text" size="20" name="name" value="<%=locName%>&nbsp;Understands&nbsp;
   <input type="checkbox" name="understands" value="true" <%=checked%>><br>
   <input type="submit" value="Get the Yes/No value for this name" name="getvalue">
   <input type="submit" value="Submit Change to Database" name="change">
</form>
</body>
</html>
Download: paramSQL.asp (zip)


Most Popular Pages On rodsdot.com

Client Side Includes Using JavaScript
Creating A JavaScript Slider Input in JavaScript
Creating Conditional Links With ASP
Dynamically Adding Rows and Fields To A Table Based Form
Geo-Coding IP Addresses
How To Make A Pop Over Menu
JavaScript Date Validation DD/MM/YYYY
JavaScript Date Validation MM/DD/YYYY
Parameterized SQL Using Multiple Form Inputs and Filtering
Play A WAV File On Mouseover
Pop Over Form
Resorting A Multi-Dimensional Table Using Client-Side JavaScript
Scripting Remote Images in JavaScript
Using AJAX to Return HTML For Dynamic Forms
Using Images For CAPTCHA
Using Parameterized SQL with ASP and MS Access
Why JavaScript As The HREF Of A Link Is Bad