Forum Moderators: open
[1]
Dim UserResponse, currentquestion, TotalQuestions, StatCounter
TotalQuestions = Request.Form("TotalQuestions")
currentquestion = 0
[/1][1]
Do While (currentquestion) < [b]TotalQuestions[/b]
currentquestion = currentquestion + 1
StatCounter = 0
UserResponse = Request.Form(Cstr(currentquestion))
StatsSQL = "SELECT * FROM Stats WHERE QuestionNumber = '" & (currentquestion) & "'"
Set StatsRS = MyConn.Execute(StatsSQL)
StatCounter = StatsRS(UserResponse) + 1
StatUpdate = "UPDATE Stats SET [b]'" & UserResponse & "'[/b] = '" & StatCounter & "' WHERE (Stats.QuestionNumber) = '" & currentquestion & "';"
MyConn.Execute StatUpdate
Loop
[/1] For those two spots, I'm trying to use variables...and it doesn't work (apparently I'm trying incorrectly) However, if I replace the variables with the actual values, like this:
[1]
Do While (currentquestion) < [b]5[/b]
StatUpdate = "UPDATE Stats SET [b]QuestionValue[/b] = '" & StatCounter & "' WHERE (Stats.QuestionNumber) = '" & currentquestion & "';"
[/1] Then it'll work. So, I'm wondering if there is a way to fix those two areas so I can use my variables and not a constant.
Also, if anyone has any suggestions on ways to "clean up" my code, I'd be more than willing to hear your suggestions. For example, I would like to know if there is a way to eliminate my first SQL statement by using my UPDATE SQL statement to just add 1 to the specific field. Thanks in advance for your help!
Dim UserResponse, currentquestion, TotalQuestions, StatCounter
TotalQuestions = Request.Form("TotalQuestions")
currentquestion = 0
Do While currentquestion < TotalQuestions
currentquestion = currentquestion + 1
StatCounter = 0
UserResponse = Request.Form(Cstr(currentquestion))
StatsSQL = "SELECT * FROM Stats WHERE QuestionNumber = '" & currentquestion & "'"
Set StatsRS = MyConn.Execute(StatsSQL)
StatCounter = StatsRS(UserResponse) + 1
StatUpdate = "UPDATE Stats SET QuestionValue = '" & StatCounter & "' WHERE (Stats.QuestionNumber) = '" & currentquestion & "';"
MyConn.Execute StatUpdate
Loop
[1]Do While currentquestion < TotalQuestions[/1] The page apparently gets stuck in a loop because I get an error that "The maximum amount of time for a script to execute was exceeded." I know that the TotalQuestions variable is 5, because if I do a Response.Write(TotalQuestions)...it will output a 5. And, it's not the parenthesis because the code will work if it put either:
[1]Do While (currentquestion) < 5 or Do While currentquestion < 5[/1] So, it's something with the the code not interpreting the TotalQuestions variable correctly. Second, the suggestion for setting the field name is not quite what I want to do. I want to use a variable for the field name because the field I update the data in will depend on what information I'm pulling from the form. That's why I want to use a variable instead of a constant. Does that make sense? I really do appreciate your help though...any other ideas?
You could make it much simpler , for questionaires (if that is was this is?) I usually use code like this:
For Each Question in Request.Form
if Question <> "Submit"
strAnswer =
replace(trim(Request((Question)),"'","''")
strSQL="INSERT INTO tblResults (Question, Answer) VALUES ('" & Question & "', '" & strAnswer & "')"
So, when they submit the form, I would like to update a statistics table that tells me how many people answered which of the question. My stats table is called "Stats" and the Fields are QuestionNumber, Response1, Response2, Response3, Response4, Response5, Response6.
So, I would like to make an SQL statement that will loop and go down the questions from the form and first get the question number (the name of the radio buttons) (i.e. 1) and also find out which response they picked (i.e. Response2). Then, it will pull the record that has 1 as the value of the QuestionNumber and then find the Response2 field and update that value by 1. Then, I want it to go back to the loop and tally up the rest of the responses.
Hope this helps some more. Once again, thanks so much for everyone's help...I really appreciate it as I'm learning all this!
Nothing wrong with your approach at all, I was just pointing out what I see as a simpler way.
There are many ways of designing questionires in asp. I get asked to do a lot of these and they are allways totally different :(
So I designed a simple db and some code that is very short, and then only modify the front end html for each new questionaire.
If you name the radio groups same as the actual questions, and the values same as the actual answers, you can report simply like this:
SELECT Answer, count(Answer) as Total
FROM tblResults
WHERE Question = <Your Question>
GROUP BY Answer
Good luck :)