Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying multiple record from a DB 2

Status
Not open for further replies.

math

Programmer
Mar 21, 2001
56
0
0
BE
hi,

I'm making a YES/NO program, the questions are in a DB (as is the correct answer). Now when the program starts 10 questions should be randomly selected and displayed. But I can't seem to do that... The datasource and Table can only be set to 1 record. So all the textfields are displaying the same question... How can I set which record is selected? How can I go to the 5th record for example (in code)
Maybe I can randomly select a record, put it in an array, then the next, until I have 10...

Can Anyone help, Thanx so much...

Math
mathias.vandebroeck@skynet.be
 
Use grid for that purpose or don't use DB-aware edit controls.
 
Your best bet will be to not use db-aware controls. You can't pull only a random dataset together... so I would run through and pick your random records and manually display them to the screen.

Maybe I can randomly select a record, put it in an array, then the next, until I have 10...

I think that's the right track... You could load the messages you want to ask into a string list with the answer in the object part of each entry. TealWren
 
Oke, I'll try the array methode...
But what I don't know is the code to go to a record...

for example the fifth record...
how can I access the 5th record of a table in code?
Is it something like Table1[5] or completly different?

And In my example You'd have a question and an answer in each record... How do I access them via code?

Thanx for your help sofar !!!
Math
 
If your answer is always yes/no you can store the answer as a TObject(1) or TObject(0) in your stringlist. Since a LongInt and a TObject pointer are the same size you can typecast them and use this trick. Code to add to the stringlist would look something like

if table.fieldbyname('Answer').asboolean then
myStringList.AddObject(table.fieldbyname('question').asstring,TObject(1))
else
myStringList.AddObject(table.fieldbyname('question').asstring,TObject(0));


As far as randomly choosing the questions, I'm not sure if you can change your data structure or not but if so you could add a "question number" to your table, and then just get a random number between 1 and the highest number in the table, and use Locate to find the randomly selected question.

Good luck!
TealWren
 
A Soution is:

1) Determine how many records are in the table recordcount

2)Use the random(x) function, which will generate a number between 0 an x

3)Go to the first record of the table and use the move to method

notes: the first record starts at position 0

Step 1
procedure TForm1.FormCreate(Sender: TObject);
begin
label1.Caption := 'number of records = ' +intTostr(Table1.recordcount);
j:= table1.recordcount;
end;

Step 2 + 3

procedure TForm1.BitBtn1Click(Sender: TObject);
var i: integer ;
begin
i:= random(j);
label2.Caption := intTostr(i);
table1.First;
table1.MoveBy(i);
end;

One inconvenience, there is no sequence so you can get the same question consecutive.
I think the bigger the table, the less probability for this to happen.

In your case, the fift record will have i = 4

Regards S. van Els
SAvanEls@cq-link.sr
 
correction: it must be the moveby(x) method S. van Els
SAvanEls@cq-link.sr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top