Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2006
    Posts
    72

    Unanswered: Simple vote/database script

    Hello

    I am trying to put together a simple vote/survey which records the vote in a MS Access database (and ensures they do not vote twice by grabbing the IP address).

    However, I am getting the following error message from my server:

    Microsoft VBScript runtime error '800a01a8'

    Object required: ''

    /vote/vote_db.asp, line 34


    The code in my vote_db.asp file is:

    <%
    'Two varibles are passed into the db
    'u_input is the value if the user entered a response to
    'the vote/poll question....IP is the address of the user
    u_input=request.form("u_input")
    u_ip=request.servervariables("remote_addr")

    ' if the user did not enter anything in the poll on this visit
    ' then display the poll question and possible choices
    if u_input = "" then
    %>

    <form method="post" action="<%= request.servervariables("script_name") %>">
    <p>Question Goes Here<br>
    <input type="radio" value="1" name="u_input">A<br>
    <input type="radio" value="2" name="u_input" >B<br>
    <input type="radio" value="3" name="u_input" checked>C<br>
    <input type="radio" value="4" name="u_input">D<br>
    <input type="submit" value="Submit" ></p>
    </form>

    <%

    Dim conn
    else
    ' if the user did input a choice on the vote/ballot
    ' check to see if their ip address is already in the db
    accessdb="votes_db"

    'cn="driver={Microsoft Access Driver (*.mdb)};"
    'cn=cn & "dbq=" & server.mappath(accessdb)


    conn.Open "driver={Microsoft Access Driver (*.mdb)};DBQ=D:\business\votes_db.mdb;"

    set conn=Server.CreateObject("ADODB.Connection")

    'set rs = server.createobject("ADODB.Recordset")
    sql = "select ip from ballot where ip ='" & u_ip & "'"

    rs.Open sql, cn
    if rs.eof then
    ' if the user has not voted previously indicate it
    been_here_before="No"
    end if
    rs.close

    if been_here_before = "No" then
    ' Since the user has not voted previously their input
    ' their vote will be added to the db

    sql = "insert into ballot (ip, selection" & u_input &") "
    sql = sql & "values ('" & u_ip & "',1)"
    rs.Open sql, cn
    end if

    'This will summerize and count the records in the db
    sql= "select distinctrow sum(selection1) as sum_selection1, "
    sql= sql & "sum(selection2) AS sum_selection2, sum(selection3) AS sum_selection3, "
    sql= sql & "sum(selection4) AS sum_selection4, count(*) AS total_votes "
    sql= sql & "FROM ballot;"
    rs.Open sql, cn
    total1=rs ("sum_selection1")
    total2=rs ("sum_selection2")
    total3=rs ("sum_selection3")
    total4=rs ("sum_selection4")
    count=rs ("total_votes")
    %>
    <br>
    A<img src="images/red.jpg" height="10" width="<%= (total1/count)*100 %>">
    <%= formatnumber((total1/count)*100,1) %>%<br>
    B<img src="images/yellow.jpg" height="10" width="<%= (total2/count)*100 %>">
    <%= formatnumber((total2/count)*100,1) %>%<br>
    C<img src="images/green.jpg" height="10" width="<%= (total3/count)*100 %>">
    <%= formatnumber((total3/count)*100,1) %>%<br>
    D<img src="images/blue.jpg" height="10" width="<%= (total4/count)*100 %>">
    <%= formatnumber((total4/count)*100,1) %>%<br>
    Total Votes: <%= formatnumber(count,0,0) %><br>

    <% if been_here_before <> "No" then %>
    This user has voted before and this one was <u>not</u> counted.....
    <% else %>
    This user has <u>not</u> voted before and this one was counted.....
    <% end if %>
    <br>
    The I.P. Address is <%= u_ip %>
    <% end if %>

    I would be grateful for any help.

    Thanks.

    Steve

  2. #2
    Join Date
    Aug 2005
    Location
    D/FW, Texas, USA
    Posts
    78
    It looks like you are trying to open the connection before you create the connection object.

    Try switching these two lines so that the SET is first:

    Code:
    set conn=Server.CreateObject("ADODB.Connection")
     
    conn.Open "driver={Microsoft Access Driver (*.mdb)};DBQ=D:\business\votes_db.mdb;"
    You'll also have the same problem a few lines later because you're trying to open a recordset before you create the object.
    --wayne
    SELECT * FROM Users WHERE Clue>0
    0 rows returned

  3. #3
    Join Date
    Feb 2006
    Posts
    72
    Hello wayneph

    Many thanks for your message.

    I still get an error message, but I have tidied the script up a little and deleted the 'cn' variable (I can't see a use for it).

    The error I now get is:

    The connection cannot be used to perform this operation. It is either closed or invalid in this context.

    <%
    'Two varibles passed to MDB
    'u_input is the value of the user's vote
    u_input=request.form("u_input")
    u_ip=request.servervariables("remote_addr")

    ' display vote question
    if u_input = "" then
    %>

    <form method here>

    <%
    Dim conn
    else
    'check IP address in MDB to ensure no previous votes
    accessdb="votes_db"

    set conn=Server.CreateObject("ADODB.Connection")

    conn.Open "driver={Microsoft Access Driver (*.mdb)};DBQ=D:\business\votes_db.mdb;"

    set rs = server.createobject("ADODB.Recordset")
    sql = "select ip from ballot where ip ='" & u_ip & "'"

    rs.Open sql
    if rs.eof then
    'if no previous vote
    been_here_before="No"
    end if
    rs.close

    if been_here_before = "No" then
    ' Add vote to MDB

    sql = "insert into ballot (ip, selection" & u_input &") "
    sql = sql & "values ('" & u_ip & "',1)"
    rs.Open sql
    end if
    %>

    Do I just need to insert somewhere, something like this:

    conn.Close
    Set conn=Nothing

    Many thanks again.

    Steve

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    If you're inserting, you don't open a recordset, you execute the command on the connection. Also your INSERT statement is worrying me...
    Code:
    sql = "insert into ballot (ip, selection" & u_input &") "
    sql = sql & "values ('" & u_ip & "',1)"
    George
    Home | Blog

  5. #5
    Join Date
    Feb 2006
    Posts
    72
    Hello George

    Thank you for getting back to me.

    I thnk I have just got it working.

    I was missing 'conn' here:

    rs.Open sql,conn


    Regarding the:

    sql = "insert into ballot (ip, selection" & u_input &") "

    'ballot' is the table name, inside of which there is a text field for the IP address, and numeric fields for selections 1-4 (4 fields) which are supposed to store the u_input (that is, the vote).

    The table fields are called selection1, selection2, etc. There is a choice of 4 selections and the vote, that is u_input, in indicated by a radio button and then the vote is sent.

    The form itself is here:

    http://stevehigham59.7host.com/vote/vote_db.asp

    Thanks again for your post.

    Steve

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    I see... This was what was cofusing me.
    Code:
    if u_input = "" then
    I'm not convinced by your design; but thatshould be discussed in the appropriate topic.

    You're making good progress with your ASP mate; perhaps you should lok into learning some CSS to make it all shine
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •