Link to home
Start Free TrialLog in
Avatar of khagen
khagen

asked on

Insert Radio value to Boolean field in Table

I Have three radio groups, and 3 tables in an Access Database, each with a field called "WhatsNew"(Boolean).  I would like to insert the value "TRUE" into the appropriate record where my recordset field "Headline" matches the Database field "Headline".  
   Here is the base code for one radio group scenerio, I will do the same with the other two once these get working.

<form method = "post" action = "Default2.asp" name= "WhatsNew">
<h1>News</h1>
<%
                  Set Conn = Server.CreateObject("ADODB.Connection")
                  Conn.Open "DSN=yohalem"
                  sql="SELECT * FROM news"
                  Set RS = Conn.Execute(sql)
                  RS.MoveFirst%>
                  <% Do While Not RS.EOF %>
            <table border="0" align="center" width="100%">
              <tr>
                <td height="23" width="3%" valign="top"><INPUT type="radio" value=<%= RS("Headline") %> name=radio_news></td>
                <td valign="top" height="23" width="97%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b><a href="news2.asp?id=<%= RS("id") %>"><%= RS("Headline") %></a></b></font></td>
              </tr>
            </table>
            <% RS.MoveNext %>
            <% Loop %>
<INPUT type="submit" value="Change Whats New" name=cmdSubmit>
</Form>
 
Now I need an Insert Statment mostlikly in a procedure, that selects the record accouring to the Headline and sets the WhatsNew field to true.  I also need to call that procedure from the form upon submit(Which is easy but I forget and have no references right now)

Avatar of khagen
khagen

ASKER

Adjusted points to 100
Avatar of khagen

ASKER

Here's What I have,

<SCRIPT Language = VBSCRIPT>
Sub cmdWhatsNew_OnClick
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "DSN=yohalem"
                  
sqlNews = "Insert into News.Comment("Keith") where News.Headline =" Radio_news.value

Conn.Execute sqlNews

Alert("The Whats New Newsboard has been updated")

End Sub
</Script>


I don't think I am referencing my RadioGroup properly, and I'm working with a text field right now, and will change to boolean once I have it.
Avatar of Mark Franz
It is my experience that the radio button select feeds a boolean value by default, or is that a check box...

Whats your psuedo code for the above?

Mark
Avatar of khagen

ASKER

My psuedo code is;

I have no pseudo code, I am still looking for some direction to take.  I want to capture the value of the three radio groups, preferably in three variables of type boolean and then send them to the data base.  I work on some psuedo code then post it.
How are you defining the group name?  Your example only identifies one group.
Avatar of khagen

ASKER

Ok I have figured it out.  Now I have related question.  Right now, I chose a radio button, and it sets the data field to true in the database, so it workd.  However, when i do another test, if I chose another radio button, a second record in the table is set to true, making two records, where as I only want one.  How can I ensure the table has only one record with that field chosen at a time, and have the radio buttons reflect the current record chosen?

Here is how I am currently updating the table to set the boolean field to true for the appt record;

Set ConnNews = Server.CreateObject("ADODB.Connection")
ConnNews.Open "DSN=yohalem"
sqlNews = "UPDATE News SET WhatsNew ="&true&" Where ID ="&Request.Form("radio_news")
ConnNews.Execute(sqlNews)
ConnNews.Close
Set ConnNews = Nothing
Avatar of khagen

ASKER

I guess, limiting the field to only one true record is more of an Access issue, but how can i ensure that the form is reflective of whats in the database?
This is an Access issue, of which I can't help you with.  Sorry.
Avatar of khagen

ASKER

I have coded the VBA in a module, I just need to know how to call it.  Can I call a Access Procedure from ASP?
Not as far as I know, khagen. MS Access doesn't handle procedures the same way as MS SQL/Oracle/etc. You'll have to write sub routines in your ASP code to emulate stored procedures. It won't be as fast, but if you're using MS Access, you most likely won't notice the additional time it takes.

I did notice that you are trying to write sub routines in ASP that only work in VB .. if you want an onclick, you have to write the subroutine and then define the onclick in your form element.

Another thing I saw was that when you try to reference the values in your form, you are using the VB way, not the ASP way. You need to specify "Request.Form("element_name")" instead of "element_name.value".

I hope any or all of this helps!

-Aemergin
Avatar of khagen

ASKER

Thanks, all those VB VB script things were pretty early on.  I have learned alot today.  Thanks for letting me know that I have to write server side script.  Here is my VBA basics.  How would this work in ASP?

Public Sub WhatsNew()
Dim Field As Boolean
Dim rs As Recordset
Dim varValid As Integer
Dim varID As Integer
'This to ensure that only one record has a true WhatsNew
Set rs = CurrentDb.OpenRecordset("Select WhatsNew From News")
rs.MoveFirst
varValid = 0
Do Until rs.EOF

Field = rs("WhatsNew")
If Field = True Then
    varValid = varValid + 1
    If varValid > 1 Then
        rs.Edit
        rs!WhatsNew = False
        rs.Update
    End If
End If
rs.MoveNext
Loop
End Sub
Try this;

Sub WhatsNew()
Dim Field, rs, ojbDC, varValid, varID, sql

'This to ensure that only one record has a true WhatsNew
' For DSNless connection use this;
Set objDC = Server.CreateObject("ADODB.Connection")
objDC.ConnectionTimeout = 30
objDC.CommandTimeout = 60
objDC.Open "DBQ=" & Server.MapPath("your.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};"
Set rs = Server.CreateObject("ADODB.Recordset")
sql = ("Select WhatsNew From News")
rs.MoveFirst
varValid = 0
Do Until rs.EOF

Field = rs("WhatsNew")
If Field = True Then
    varValid = varValid + 1
    If varValid > 1 Then
        'rs.Edit
        rs.WhatsNew = False
        rs.Update
    End If
End If
rs.MoveNext
Loop
End Sub

Untested...
Oops, forgot to open the rs;

Set rs = Server.CreateObject("ADODB.Recordset")
sql = "Select WhatsNew From News;"
rs.Open sql, objDC, 3, 3
....
Avatar of khagen

ASKER

Thanks,
The code was pretty good, I just had to change some recordset stuff.  While I'm on here,(and you can lock me If you like), do you know of a way of seting a radio button to true.  As you know, I'm retrieving values from a database, and when I first open the page with the radio buttons, I would like the radio buttons to accuratly represent which record in the tablw is currently set to true.  

The psuedo code is;

IF WhatsNew is True Then
     Set the current radio button to True
End If

I would intergrate it into this code;

<%Do While Not RSNews.EOF %>
            <table border="0" align="center" width="100%">
              <tr>
                <td height="23" width="3%" valign="top">
                              <INPUT type="radio" value=<%= RSNews("id") %> name=radio_news></td>
                <td valign="top" height="23" width="97%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>
                              <a href="news/news.asp?id=<%= RSNews("Headline") %>"><%= RSNews("Headline") %></a></b></font></td>
              </tr>
            </table>
            <% RSNews.MoveNext %>
            <% Loop %>

Since the value of the radio button will be either true of false in the dB anyway, all you will have to do is check the value;

<%
sql = "select * from table"

if <%RSNews("WhatsNew")%> = "True" Then
%>
<table border="0" align="center" width="100%">
              <tr>
                <td height="23" width="3%" valign="top">
<INPUT type="radio" value=<%= RSNews("id") %> name=radio_news></td>
                <td valign="top" height="23" width="97%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>
<a href="news/news.asp?id=<%= RSNews("Headline") %>"><%= RSNews("Headline") %></a></b></font></td>
              </tr>
            </table>
            <% RSNews.MoveNext %>
            <% Loop %>
<ElseIf <%RSNews("WhatsNew")%> = "False" Then
%>
Do something else...

Essentially all you want to do is check the value of the field, if true do something, if false, do something else.  No need to Set the button value, unless you need it as an object.  You can put this loop in a sub for simplicity.

Mark
Avatar of khagen

ASKER

How is that different than this?

<%
                  Set Conn = Server.CreateObject("ADODB.Connection")
                  Conn.Open "DSN=yohalem"
                  sql="SELECT * FROM news"
                  Set RSNews = Conn.Execute(sql)
                  %>
                  <%Do While Not RSNews.EOF %>
            <table border="0" align="center" width="100%">
              <tr>
                <td height="23" width="3%" valign="top">
                              <INPUT type="radio" value=<%= RSNews("id") %> name=radio_news></td>
                <td valign="top" height="23" width="97%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>
                               <%Response.Write(RSNews("Headline"))%></b></font></td>
              </tr>
            </table>
            <% RSNews.MoveNext %>
            <% Loop %>
       
         
It seems to me that your saying the option button should already be selected because the value is being set to true due to that value being passed as true from the database.  Is that right?
ASKER CERTIFIED SOLUTION
Avatar of Mark Franz
Mark Franz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial