Random Button text

anaylor01

Well-Known Member
Licensed User
Longtime User
I have a database I pull a single record from. In the record is a question and 4 answers. 3 being incorrect and one being correct. I have 4 buttons. I need to populate the text of each button with random answer values. Right now I am places the values into 4 variables. I was thinking of doing something like this:
Dim question As String
Dim a(4) As Int

question = SQL1.ExecQuerySingleResult("SELECT question FROM trivia where ID = 116")
a(0) = SQL1.ExecQuerySingleResult("SELECT answer FROM trivia where ID = 116")
a(1) = SQL1.ExecQuerySingleResult("SELECT wrong1 FROM trivia where ID = 116")
a(2) = SQL1.ExecQuerySingleResult("SELECT wrong2 FROM trivia where ID = 116")
a(3) = SQL1.ExecQuerySingleResult("SELECT wrong3 FROM trivia where ID = 116")
lblQuestion.Text = question
Dim i, j, k, x As Int
Dim answers(4) As Int

For i = 0 To 3
answers(i) = i + 1
Next

For i = 0 To 3
k = Rnd(i, 4)

x = answers(i)
answers(i) = answers(k)
answers(k) = x
Next

' For j = 0 To 3
' Holdings(j).Clear
' k = j*13
' For i = 0 To 12
' Holdings(j).Add(answers(i + k))
' Next
' Holdings(j).Sort(True)

answer1.text = a(answers(k))
answer2.Text = "answer" + rand
answer3.Text = "answer" + rand
answer4.Text = "answer" + rand
I know that this will duplicate. I already have a process to prevent that but didn't want to put all the code here.
 

anaylor01

Well-Known Member
Licensed User
Longtime User
Error compiling program.
Error description: Object expected.
Occurred on line: 117
UsedNumbers.Add (NewNumber)
Word: add
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
We need more info, but check that you have declared UsedNumbers as a List in Sub Globals.

The line:
B4X:
UsedNumbers.Add (NewNumber)
is not in my code!

post up the code you have please.
 
Last edited:
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
I think that part is working now. But now I have this.
Error description: Too many parameters.
Occurred on line: 112
question = SQL1.ExecQuerySingleResult("SELECT question FROM trivia where ID = ?",Array As String(NewNumber))
Word: array
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
You really should post more information (like the code in the sub that is the problem) if you want help, as it is often not the line that throws the error that is the problem.

In this case it is because you are using "Array As String(NewNumber))" instead of a string to get the data.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
What is the proper syntax than? I tried all the examples from the tutorial and nothing works.

The correct syntax to do what? We do not know what your database comprises, what you are trying to do, or what results you are expecting.
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
What is the proper syntax for this:
question = SQL1.ExecQuerySingleResult("SELECT question FROM trivia where ID = ?",NewNumber)
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Error description: Too many parameters.
Occurred on line: 164
question = SQL1.ExecQuerySingleResult("SELECT question FROM trivia where ID = ?",array as Int(NewNumber))
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
What is the proper syntax for this:
question = SQL1.ExecQuerySingleResult("SELECT question FROM trivia where ID = ?",NewNumber)

:BangHead:

I think you need ExecQuerySingleResult2

B4X:
question = SQL1.ExecQuerySingleResult2("SELECT question FROM trivia where ID = ?",Array As String(NewNumber))
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Ok. Only one problem now. The questions show more than once. I'll explain what I am doing.
When a button is clicked it runs the following code to get a new question.

Dim i As Int
Dim NumberList As List
numberlist.Initialize

For i = 1 To 73
NumberList.Add (i) ' add the numbers to the list
Next
Dim i As Int
Dim NewNumber As Int

i = Rnd (0, NumberList.Size ) ' random pointer to somewhere in the list
NewNumber = NumberList.Get (i) ' get the number
NumberList.RemoveAt (i) ' remove the number from the list
' you can now use NewNumber for your question. Note this does not check for the list being empty
Dim answers(4) As Int
question = SQL1.ExecQuerySingleResult2("SELECT question FROM trivia where ID = ?",Array As String(NewNumber))

a(0) = SQL1.ExecQuerySingleResult2("SELECT answer FROM trivia where ID = ?",Array As String(NewNumber))
a(1) = SQL1.ExecQuerySingleResult2("SELECT wrong1 FROM trivia where ID = ?",Array As String(NewNumber))
a(2) = SQL1.ExecQuerySingleResult2("SELECT wrong2 FROM trivia where ID = ?",Array As String(NewNumber))
a(3) = SQL1.ExecQuerySingleResult2("SELECT wrong3 FROM trivia where ID = ?",Array As String(NewNumber))
lblQuestion.Text = question

For i = 0 To 3
answers(i) = i
Next

For i = 0 To 3
k = Rnd(i, 4)

x = answers(i)
answers(i) = answers(k)
answers(k) = x
Next

answer1.Text = a(answers(0)) 'assuming answer1 etc are button names
answer2.Text = a(answers(1))
answer3.Text = a(answers(2))
answer4.Text = a(answers(3))
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
The problem is that this code block:
B4X:
NumberList.Clear ' added as per post #18
For i = 1 To 73
NumberList.Add (i) ' add the numbers to the list
Next
should only be run when the program starts, or when you reset it.
If you run it where it is it resets the list of questions that have not been asked (have another read of post #18.
Also
B4X:
Dim NumberList As List
NumberList.Initialize
should be in Sub Globals so that it is available to your other Subs.


Some of that code looks strangely familiar :cool:
 
Last edited:
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
When it gets to the end of the list it error out. How do I check to see if there are any numbers left?
Here is the line it errors out on.
i = Rnd (0, NumberList.Size )
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
When it gets to the end of the list it error out. How do I check to see if there are any numbers left?
Here is the line it errors out on.
i = Rnd (0, NumberList.Size )

I did say in post #18 that there was no check for an empty list.

You can easily add one
B4X:
Dim i As Int
Dim NewNumber As Int

If NumberList.Size = 0 Then
MsgBox ("No questions left","Stop getting questions")
Return
End If

i = Rnd (0, NumberList.Size ) ' random pointer to somewhere in the list
NewNumber = NumberList.Get (i) ' get the number
NumberList.RemoveAt (i) ' remove the number from the list
' you can now use NewNumber for your question. Note this does not check for the list being empty
Dim answers(4) As Int
question = SQL1.ExecQuerySingleResult2("SELECT question FROM trivia where ID = ?",Array As String(NewNumber))

a(0) = SQL1.ExecQuerySingleResult2("SELECT answer FROM trivia where ID = ?",Array As String(NewNumber))
a(1) = SQL1.ExecQuerySingleResult2("SELECT wrong1 FROM trivia where ID = ?",Array As String(NewNumber))
a(2) = SQL1.ExecQuerySingleResult2("SELECT wrong2 FROM trivia where ID = ?",Array As String(NewNumber))
a(3) = SQL1.ExecQuerySingleResult2("SELECT wrong3 FROM trivia where ID = ?",Array As String(NewNumber))
lblQuestion.Text = question

For i = 0 To 3
answers(i) = i
Next

For i = 0 To 3
k = Rnd(i, 4)

x = answers(i)
answers(i) = answers(k)
answers(k) = x
Next

answer1.Text = a(answers(0)) 'assuming answer1 etc are button names
answer2.Text = a(answers(1))
answer3.Text = a(answers(2))
answer4.Text = a(answers(3))
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Ok. I need to have 2 criteria in my query. I have tried the 2 queries below but neither worked.
question = SQL1.ExecQuerySingleResult2("SELECT question FROM trivia where ID = ?" Array As String(newnumber) " and category = ?",Array As String(category))

question = SQL1.ExecQuerySingleResult2("SELECT question FROM trivia where ID = ? and category = ?",Array As String(NewNumber,category))
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Try
B4X:
question = SQL1.ExecQuerySingleResult2("SELECT question FROM trivia where ID = ? and category = ?",Array As Object(NewNumber,category))
or let us have more info.
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
That errored out. When I was trying to figure this out I found something but I don't know how to use it.
cmd.CommandText = "update geo set ed = '"&txtfilter.Text&"'where hpid = '"& txtsearch.Text& "';"
cmd.ExecuteNonQuery
 
Upvote 0
Top