 |
jonwiper Level: Big Cheese
 Registered: 30-03-2005 Posts: 19
|
Using Access as a database for a VB project.
Hey guys,
I know everyone hates newbs, but i could do with a spot of help. Ive looked through the faq's and other discussions but cant find exactly what im looking for. Here goes;
I am creating a quiz using vb, i have all my questions, 20 in total, but i would like ten of them to be randomly generated each time a user selects to play.
It would be ideal to generate these questions into one form, one after the other, and store the score which they recieve in order to rate them at the end.
Is it just a case of storing these questions in 'access' somehow for example and calling them when needed, or having many items on top of eachother on the form and making them visible and invisible where appropriate (this suggests insanity) or maybe loading a text file!?
Help and suggestions would be much appreciated. Cheers
|
|
30-03-2005 at 02:06 PM |
|
|
jahsen Level: Professor
 Registered: 08-01-2005 Posts: 72
|
Re: Using Access as a database for a VB project.
Ok I am no expert but i hope this would help,
first if you need to store the scores into a database then you may need to create 2 tables on an access database name each player and question respectively, in the table player you may need to create necessary fields such as username password and scores
now how to access them in VB Goran thought me the easiest way so now i am sharing it to you, right click on the project exporer then choose add form, on the add form dialogue choose vb form wizard, choose the best that suites your need but i think for the account creation of each player it is best that you create a form with the textboxes for each datafield needed then on the create button you may put this codes
Dim cn As ADODB.Connection
Dim myRecordSet As ADODB.Recordset
Dim strsql As String
Set cn = New ADODB.Connection
cn.ConnectionString = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\folder\access.mdb;Persist Security Info=False
cn.Open
strsql = "INSERT INTO players(field1,field2,field3,field4,field5) VALUES('" & Text1 & "', '" & text2 & "', '" & Text3 & "','" & Text4 & "','" & Text5 & "');"
Set myRecordSet = New ADODB.Recordset
myRecordSet.Open strsql, cn
Set myRecordSet = Nothing
|
for the questions just create another form for the question table each textbox for each fields but you might want to make the textbox for the answer invisible
for example you have 20 questions then generate a number from 1 to 20 make sure you have a fields for the question table which is from 1 to 20 as well
Dim x As Integer
x = 0
x = Int((20 - 1 + 1) * Rnd) + 1
|
the above code will generate a number from 1 to 20 but several times may result to a same number
then in order to view the question then
use this
Dim cn As ADODB.Connection
Dim myRecordSet As ADODB.Recordset
Dim strsql As String
Set cn = New ADODB.Connection
cn.ConnectionString = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\folder\access.mdb;Persist Security Info=False
cn.Open
strsql = "select * from question where questionindex = '" & X & '""
Set myRecordSet = New ADODB.Recordset
myRecordSet.Open strsql, cn
Set myRecordSet = Nothing
text1 = myrecordset!question
text2 = myrecordset!answer
|
i hope somehow these gave you an idea, if not im sure Goran and Rodgers so as the others will help.
|
|
30-03-2005 at 07:48 PM |
|
|
jonwiper Level: Big Cheese
 Registered: 30-03-2005 Posts: 19
|
Re: Using Access as a database for a VB project.
Thanks for the post jahsen.
It has given me an idea now what to do.
I do have a couple of extra questions now tho;
cn.ConnectionString = Provider=Microsoft.Jet.OLEDB.4.0;Data
The above line is getting error messages about not having an end of line statement, i think its having trouble with the 4.0. any ideas?
Source=C:\folder\access.mdb;Persist Security Info=False
The line above is also causing an error - no line number?
Also im guessing the source is where access is held, or is it where my database is located?
Thanks Jon
|
|
31-03-2005 at 11:16 AM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: Using Access as a database for a VB project.
What kind of questions are you going to manage? Long-text questions? Single correct answer or multiple correct anwer? Close answer or Open answer? Some answer proposal to choose among, or no proposal?
I got some suggestions to give, but the most important is that all depends on how you organize the db. You should include for each record (a single question): a question text, a correct answer (or some words to compare with), some optional answer proposals. Then some optional utility fields, such as some keywords to make searches, or a question rating field.
I would execute a query to retrieve only a certain number of records, instead of keeping the whole db in memory. So you work with a restricted dataset, that's faster to manage.
In the query you retrieve all the fields you need: text, correct answer, optional proposals, but you show to user only the text and the proposals, and keep the correct answer to compare the user selection. In the same query you can retrieve also the questions rating.
You can filter the number of records by the sql TOP intruction, within the query itself. I created a small single-table db in Access97. In it, the job is done all by the sql instruction. It's done in Access, but it can be improved if you use VB, so I put no forms in it.
First, I included the Rnd() MSAccess' function into the sql statement, but it doesn't work properly as the returned values are not exactly random. In VB we can initialize the system randomizer by calling the Randomize instruction before using the Rnd() function, but Randomize is not avaliable in Access.
More, as in VB you can build an sql statement like a string, you can randomize and create an array of IDs, and then build a query to retrieve only the TOP 10 records with those IDs.
Anyway, you can have a look on my small db. It's designed to manage not-that-long questions (max 255 chars; if you need more, you shoud use a Memo type field, but in this case the field will not be indexed so you'd create another keyword field - or some other trick - for searches), with five answer proposal to choose among, and a single correct answer allowed.
Sample:
What is the largest continent on the Earth?
1) Africa
2) Europe
3) North America
4) Asia
5) South America
(user should choose the answer 4)
You should design the way the user makes his choice and how you can compare it with the correct answer you take by the query.
As I said at first, if you need to manage questions in other ways, you have to modify the db implementation.
Hope it helps.
PS. The correct syntax is as following:
| cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\folder\access.mdb;Persist Security Info=False" | It is one single line, if you need to trim it in two lines, you should use the underscore char ("_").
Data Source is the path and name of the .mdb file.
P.P.S. not everyone hates newbies, at least not here.
____________________________
Real Programmer can count up to 1024 on his fingers
____________________________ Attached:
db6.zip 10 KB (Downloads: 19)
|
|
31-03-2005 at 11:50 AM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: Using Access as a database for a VB project.
No large If statement: an array of radio buttons and a Select Case. And only if you need to evaluate every given answer, I mean "...he said "a": three points, he said "b": correct, six points, he said "c": one point...". If you only need to check if the given answer is correct or not, you just need an If block ("...If answer = "b" Then...")
This method is intended to show user one record (question) at a time: he read the question, make a choice, submit, then move to the next question. So you should create a recordset variable to store the questions, and link the textboxes to the fields of the recordset. You got to create also a global variable to keep the score, and increase it when moving to the next record.
Say you have four textboxes, named txtQuestion, txtOption1, txtOption2 and txtOption3: you can link them to the fields of the query. Say you have three radio buttons aside the txtOption1, txtOption2 and txtOption3 textboxes, and group the radio buttons in an array, named optGivenAnswer(); so you have optGivenAnswer(0), optGivenAnswer(1) and optGivenAnswer(2). Finally, say you put a command button named btnSubmit.
Then you can obtain the given answer by a Select Case block, as following: Option Explicit
Dim ChosenAnswer As Byte
Private Sub btnSubmit_Click()
Select Case ChosenAnswer
Case 0
' do the appropriate things
Case 1
' do the appropriate things
Case 2
' do the appropriate things
End Select
End Sub
Private Sub optGivenAnswer_Click(Index As Integer)
' (one single Click event occurs during loading phase too)
ChosenAnswer = Index
End Sub | If otherwise you need only to compare the given answer with the correct, retrieved by the query but hidden to the user, you can use a simpler approachDim correct As Byte
correct = ... ' (set the value from the query, here)
If optGivenAnswer(correct).Value = True Then
' do the appropriate things
|
If you prefer showing user all the questions once together, you might consider using a grid, and a grid column of cells/some textboxes to input his choices instead than radio buttons. But even in this case, the best solution is not a large If statement, but better looping by a For cycle, say "...For Each record In recordset, If CellInput.Value = recordset.Fields("CorrectAnswer").Value Then ....increase the score...")
Hope it helps.
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
01-04-2005 at 09:45 AM |
|
|
jonwiper Level: Big Cheese
 Registered: 30-03-2005 Posts: 19
|
Re: Using Access as a database for a VB project.
I wonder if you can take a look at my code to load some data from my database and see why it is not working - thanks:
Private sub form1levelselector.load ()
Dim cn As ADODB.Connection
Dim myRecordSet As ADODB.Recordset
Dim strsql As String
Set cn = New ADODB.Connection
cn.ConnectionString = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My Documents\Visual Work\db1.mdb;Persist Security Info=False
cn.Open
strsql = "select * from qRandomQuestions1 where Questions.QuestionRating = '" & 1 & '""
Set myRecordSet = New ADODB.Recordset
myRecordSet.Open strsql, cn
Set myRecordSet = Nothing
text1 = myrecordset!Questions.Text
End sub
Where qRandomQuestion1 is my query, Questions.QuestionRating is a field in the questions table and Questions.Text is the questionfield from the questions table which i wish to load.
I have a text box on the form where i thought the question from my database would appear. Any ideas please?
N.B. for yronium - I now have the Qyestions.QuestionRating as a way of making sure i load a question from the level of difficulty the user wants. Thanks
|
|
03-04-2005 at 02:32 PM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: Using Access as a database for a VB project.
quote: jonwiper wrote:
myRecordSet.Open strsql, cn
Set myRecordSet = Nothing
text1 = myrecordset!Questions.Text
You open the recordset, set it to nothing, then try to link a textbox to it, but it doesn't exist anymore.
Try this:
Private Sub form1levelselector_Load()
Dim dbname As String
Dim cn As ADODB.Connection
Dim myRecordSet As ADODB.Recordset
Dim strsql As String
Set cn = New ADODB.Connection
dbname = "C:\My Documents\Visual Work\db1.mdb"
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& dbname & ";Persist Security Info=False"
cn.Open
strsql = "SELECT * FROM Questions WHERE QuestionRating = " & Chr(34) & 1 & Chr(34)
Set myRecordSet = New ADODB.Recordset
myRecordSet.Open strsql, cn
text1.Text = myrecordset.Fields("Text").Value
Set myRecordSet = Nothing
End Sub |
quote:
Where qRandomQuestion1 is my query, Questions.QuestionRating is a field in the questions table and Questions.Text is the questionfield from the questions table which i wish to load.
Wait. What you call qRandomQuestion1 is nothing but an SQL instruction. You can view it in Access, by opening the query, and then click the menu View/SQL view. MSAccess gives users the capability to save these sql instructions with a name, and store them into the database file. So, to recall an existing query by VB, you just have to call its name, like myRecordSet.Open qRandomQuestion1, cn . If you prefer to replicate the sql instruction into the VB code (VB allows it), you have to refer to the fields of the table, not again to the query. Notice that in the sql instruction I wrote, I refer to the table fields, not the query fields: strsql = "SELECT * FROM Questions WHERE QuestionRating = " & blah blah . Hope it's clear the difference.
Now, you load the result from the sql instruction into a recordset variable. To link a textbox to a given field from this recordset, you have to refer to the Fields property of the recordset variable. The Fields property contains an array with the names of the columns of the recordset, so you can refer to a given field by its name (a String value), then to the value of this field, as following:
text1.Text = myrecordset ' <== this is the loaded recordset
text1.Text = myrecordset.Fields("Text") ' <== this is the field by the name "Text"
text1.Text = myrecordset.Fields("Text").Value ' <== this is the value contained in this field
(You can also bind the textbox to a recordset field, by setting the textbox' DataField property on field's name, and the DataSource property on the recordset - this case, myrecordset).
quote: N.B. for yronium - I now have the Qyestions.QuestionRating as a way of making sure i load a question from the level of difficulty the user wants. Thanks
Yeah, I thought to include a rating for questions to speed up the evaluation of the whole questionary. But you can also make queries on it too.
Finally, try to run the code I provided. I couldn't test it as I'm not home now, but it should work. Moreover I hope you've understood how it's working. If you miss something, let me know.
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
04-04-2005 at 02:21 PM |
|
|
jonwiper Level: Big Cheese
 Registered: 30-03-2005 Posts: 19
|
Re: Using Access as a database for a VB project.
Thanks for the new piece of coding yronium, i'll try it shortly.
With regards to the error i'm getting, it is the first line of code thats highlighted, I also am quite sure it isn't the project/reference im missing. I have Microsoft DAO 3.6 object library selected. That's right, yes?
Thanks once more. Jon
|
|
04-04-2005 at 03:06 PM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: Using Access as a database for a VB project.
quote: jonwiper wrote:
I have Microsoft DAO 3.6 object library selected. That's right, yes?
No, it isn't. DAO is one thing, ADO is another. You declared a variable "...As ADODB.Connection...", so you are intended to use an ADO object. Probably you miss this reference. Check it. ADO means "Microsoft ActiveX Data Objects 2.x Library".quote: cn.ConnectionString = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\folder\access.mdb;Persist Security Info=False
Please notice that in the code I provided I rounded the connection string by double quotes. Be sure doing the same to the string you use.quote: strsql = "INSERT INTO Players(First Name, Last Name) VALUES('" & Text1.text & "', '" & text2.text & "');"
Set myRecordSet = New ADODB.Recordset
myRecordSet.Open strsql, cn
[...]
With the code above; I am getting the response 'Syntax error in INSERT INTO STATEMENT' when i click debug it highlights the line of code: myRecordSet.Open strsql, cn
[...]
The code from the question part; I get the error message 'Data type mismatch in criteria expression' when I debug this error it points me again to the same line of code that it does above.
The highlighted line is the one where the error is raised, but it's not necessarily the line with the mistake.
If the sql instruction contains a mistake, the error will not be raised until the code will not try to execute it. So opening the recordset has raised an error, but you should not necessarily search the error in that line: in fact, you better check the sql instruction itself.
I'd wonder: is the INSERT INTO statement correctly written? (I'm not deep into sql, but I don't believe you can specify two fields at a time to insert values. I believe that the VALUES clause assign values to the same fields of multiple records. I mean: INSERT INTO LastName VALUES Smith, Johnson, Adams, Miller, Brown, McQueen, etc... Mmhh, check it, even in MSAccess online help).
Are the tetx1 and text2 values compatible with the fields (it gave me a "Data type mismatch")?
...Well, in plain words, check the sql instruction and its syntax. And keep in mind that MSAccess SQL is not completely the same of standard SQL: often, the best way is to create a query in Access, then copy/paste into VB the generated sql instruction.
And finally remember that, by ADO, you can always bind your controls to a recordset, so you have not to execute an sql instruction every time you edit/add a record, as the bound controls automatically pass the new values to the underlying recordset.
Try my code. Let me know
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
05-04-2005 at 12:47 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: Using Access as a database for a VB project.
Use [] arround fields that consist of two or more words. THe output of the SQL statement shoud look like this:
| INSERT INTO Players ([First Name], [Last Name]) VALUES ..... |
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
05-04-2005 at 12:59 PM |
|
|
jonwiper Level: Big Cheese
 Registered: 30-03-2005 Posts: 19
|
Re: Using Access as a database for a VB project.
I still cant get the blasted thing to work, even with gorans advice. The First Name and Surname are my field names in the table, so it should relate to them right?!
I've eventried turning it on its head and using the method below but even that doesn't work! ARGH!
cn.Provider = "Microsoft.Jet.OLEDB.4.0;"
cn. ConnectionString = “C:\My Documents\Visual Work\db1.mdb”
cn.Open
Set myRecordSet = New ADODB.Recordset
myRecordSet.Open “SELECT First_Name, Last_Name FROM Players” , db1, adOpenDynamic, adLockOptimistic
myRecordSet.AddNew
myRecordSet!First_Name = Text1.Text
myRecordSet!Last_Name = Text2.Text
myRecordSet.Update
myRecordSet.Close
Set myRecordSet = Nothing
Set cn = Nothing
‘Clear the text boxes after adding
Text1.Text = “”
Text2.Text = “” |
|
|
05-04-2005 at 02:20 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: Using Access as a database for a VB project.
jonwiper, you are mixing some things here.... I will give you two examples how things work with field names.
Situation 1:
Table: Players
1st field: First_Name
2nd field: Last_Name
SQL SELECT statement
| SELECT First_Name, Last_Name FROM Players |
Situation 2:
Table: Players
1st field: First Name
2nd field: Last Name
SQL SELECT statement
| SELECT [First Name], [Last Name] FROM Players |
So, as it can be seen, if you replaced blank space with _ character in field name, there is no need for [] brackets. [] brackets are only needed if field name has empty space in it.
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
05-04-2005 at 06:31 PM |
|
|
jonwiper Level: Big Cheese
 Registered: 30-03-2005 Posts: 19
|
Re: Using Access as a database for a VB project.
Back again guys,
Yronium gave me some ideas about how to check my answers in this quiz im making - its turned into a personal challenge now ive started! ;)
I wondered if you could go into some more detail about it for me. I know you've help me alot but I would still appraciate it immensely. The code he suggested is in an above post.
As he suggested I have my question text box and three answer boxes. I need to compare whatever answer the user choses via an array of check boxes, to the correct answer.
But what im not sure about is how to get the checkbox related to the text boxes to check the correct answer?!
Cheers for your time, Jon
|
|
13-04-2005 at 11:22 AM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: Using Access as a database for a VB project.
quote: jonwiper wrote:
But what im not sure about is how to get the checkbox related to the text boxes to check the correct answer?!
You don't have the need to relate the checkboxes to the textboxes. They don't need any link or binding or whatever: they just work disconnected.
You link the three textboxes to the db, in order to show the user the possible answers; then you provide the user a way to make its choice (a checkboxes array, an optionbuttons group, another textbox to write the number of the chosen answer in, etc...); finally, by a Submit button, you compare the value of the answer (the index of the checkboxes/optionbuttons array, or the text of the choice textbox, or whatever else) with the value of the correct answer's recordset field.
So, the answering mechanism you provide has no need to be bound to the avaliable choices, you see?
If I'm not clear, feel free to ask again. And of course, if anybody else has a better idea, I'm lurking here too.
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
13-04-2005 at 11:47 AM |
|
|
jonwiper Level: Big Cheese
 Registered: 30-03-2005 Posts: 19
|
Re: Using Access as a database for a VB project.
This method surely means that for each question, I would need to know which answer and therefore which associated check box is actually beside the right answer. No?
I.e. if optgivenanswer(1) is beside the right answer then if its value is true (selected) then it will be correct. But if the questions are being loaded randomly the correct answer isnt always loaded into the same answer textbox.
So how will the array help then?!
Sorry to keep bothering you but I really dont get this bit.
Thanks mate, Jon
|
|
14-04-2005 at 01:02 PM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: Using Access as a database for a VB project.
The correct answer is a field into the recordset. You have the fields OptionalAnswer1, OptionalAnswer2 and OptionalAnswer3, and then the field CorrectAnswer, containing a value from 1 to 3 (the field that contains the correct answer). Each OptionalAnswer field doesn't know if it contains the correct one or not, but it doesn't matter as the correct one is written into an appropriate field. So, it's a record feature, and it changes for each record: the next question has its own correct answer.
It's the records that are returned randomly, so each record - in its random order - has its three OptionalAnswer fields and its own CorrectAnswer field.
Look the sample image I attached.
The CorrectAnswer field is not shown on the form, but it's included in the query, so you know its value. You have just to compare the hidden textbox value with the index (+1, as arrays are in Zero base) of the optionbuttons array, during the btnSubmit_Click event.
Of course this is just a sample, but you can choose another validation method.
Hope I have been clear.
[Edited by yronium on 15-04-2005 at 08:31 AM GMT]
____________________________
Real Programmer can count up to 1024 on his fingers
____________________________ Attached:
|
|
15-04-2005 at 07:19 AM |
|
|
ushakiran Level: Trainee
 Registered: 14-05-2005 Posts: 1
|
Create a quiz Using Access as a database
Hii,
Can u please tell me how to cretae database for inserting quiz options. The forntend may be anything either asp or .net.
Regards,
Usha.
quote: jahsen wrote:
Ok I am no expert but i hope this would help,
first if you need to store the scores into a database then you may need to create 2 tables on an access database name each player and question respectively, in the table player you may need to create necessary fields such as username password and scores
now how to access them in VB Goran thought me the easiest way so now i am sharing it to you, right click on the project exporer then choose add form, on the add form dialogue choose vb form wizard, choose the best that suites your need but i think for the account creation of each player it is best that you create a form with the textboxes for each datafield needed then on the create button you may put this codes
Dim cn As ADODB.Connection
Dim myRecordSet As ADODB.Recordset
Dim strsql As String
Set cn = New ADODB.Connection
cn.ConnectionString = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\folder\access.mdb;Persist Security Info=False
cn.Open
strsql = "INSERT INTO players(field1,field2,field3,field4,field5) VALUES('" & Text1 & "', '" & text2 & "', '" & Text3 & "','" & Text4 & "','" & Text5 & "');"
Set myRecordSet = New ADODB.Recordset
myRecordSet.Open strsql, cn
Set myRecordSet = Nothing
|
for the questions just create another form for the question table each textbox for each fields but you might want to make the textbox for the answer invisible
for example you have 20 questions then generate a number from 1 to 20 make sure you have a fields for the question table which is from 1 to 20 as well
Dim x As Integer
x = 0
x = Int((20 - 1 + 1) * Rnd) + 1
|
the above code will generate a number from 1 to 20 but several times may result to a same number
then in order to view the question then
use this
Dim cn As ADODB.Connection
Dim myRecordSet As ADODB.Recordset
Dim strsql As String
Set cn = New ADODB.Connection
cn.ConnectionString = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\folder\access.mdb;Persist Security Info=False
cn.Open
strsql = "select * from question where questionindex = '" & X & '""
Set myRecordSet = New ADODB.Recordset
myRecordSet.Open strsql, cn
Set myRecordSet = Nothing
text1 = myrecordset!question
text2 = myrecordset!answer
|
i hope somehow these gave you an idea, if not im sure Goran and Rodgers so as the others will help.
|
|
14-05-2005 at 06:04 PM |
|
|
|
|
 |
 |