Nov 8, 2009

Quiz Code

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > VB Programming

Quiz Code

kvarnerexpress
I saw a nice quiz code posted by medialint where the questions and answers are written in the code. What I
need help with is adapting it so that it uses an access database or an excel csv file as the source of the questions and the choices. In the code itself there is a comment to the fact that "ideally they (meaning the Q's & A's) will be read from a database."
So how is that done? How do you get a quiz code to use a database?Also the table is fairly long and is divided and arranged in alphabetical order. Could the questions be limited to the info in the group of one letter of the alphabet at a time? Fopr example get the questions from
the letter A or B or both?

Comment/Reply (w/o sign-up)

Galahad
Yes, you can get your quiz to use Access database, or any other for that matter...

To use Access DB, you can use either ADO, DAO or Data control... I prefer to use DAO 3.6, so I'll explaint it quickly...

Before everything, add a reference to Microsoft DAO 3.6 object. Next, you need to create Database object, that will hold the database, and create Recordset object, that holds actual table with questions and answers...
CODE
Dim DB As Database
Dim RS As Recordset

Then, proceed to open database and table
CODE
Set DB = OpenDatabase("databasename.mdb")
Set RS = DB.OpenRecordset("tablename")

Now, I prefer to use SQL for search operations inside database, so here's how you get a group of questions, based on a first letter for example. I assume you have some basic knowledge of SQL (Structured Query Language). Like with filenames, * is a wildcard, and SQL syntax is fairly easily understandable...
CODE
SQL="SELECT * FROM tablename WHERE question LIKE ""A*"""
Set RS = DB.OpenRecordset(SQL)

Now, you may have noticed I used OpenRecordset, to execute SQL... This is a great way to create a recordset object, containing search results in your SQL query... Now, it is not neccesary to open the entire table, if you will be searching for specific questions... If you have some basic knowledge of databases, you will handle this easily... Here's the portion of the code you could use (it's not tested, but it should work, just replace database, table and field names with your own):

CODE
Dim DB As Database
Dim RS As Recordset
Dim SQL As String
Private Sub Form_Load()
Set DB = OpenDatabase("database.mdb")
End Sub

Private Sub Command1_Click()
SQL = "SELECT * FROM TableName WHERE FieldName LIKE ""A*"""
Set RS = DB.OpenRecordset(SQL)
RS.MoveLast
RS.MoveFirst
If RS.RecordCount > 0 Then
 ' Write your code to handle the results
Else
 ' No matches found, what to do, write here
End If
End Sub


If you need further help, drop me an e-mail...

 

 

 


Comment/Reply (w/o sign-up)

Galahad
Yes, you can get your quiz to use Access database, or any other for that matter...

To use Access DB, you can use either ADO, DAO or Data control... I prefer to use DAO 3.6, so I'll explaint it quickly...

Before everything, add a reference to Microsoft DAO 3.6 object. Next, you need to create Database object, that will hold the database, and create Recordset object, that holds actual table with questions and answers...
CODE
Dim DB As Database
Dim RS As Recordset

Then, proceed to open database and table
CODE
Set DB = OpenDatabase("databasename.mdb")
Set RS = DB.OpenRecordset("tablename")

Now, I prefer to use SQL for search operations inside database, so here's how you get a group of questions, based on a first letter for example. I assume you have some basic knowledge of SQL (Structured Query Language). Like with filenames, * is a wildcard, and SQL syntax is fairly easily understandable...
CODE
SQL="SELECT * FROM tablename WHERE question LIKE ""A*"""
Set RS = DB.OpenRecordset(SQL)

Now, you may have noticed I used OpenRecordset, to execute SQL... This is a great way to create a recordset object, containing search results in your SQL query... Now, it is not neccesary to open the entire table, if you will be searching for specific questions... If you have some basic knowledge of databases, you will handle this easily... Here's the portion of the code you could use (it's not tested, but it should work, just replace database, table and field names with your own):

CODE
Dim DB As Database
Dim RS As Recordset
Dim SQL As String
Private Sub Form_Load()
Set DB = OpenDatabase("database.mdb")
End Sub

Private Sub Command1_Click()
SQL = "SELECT * FROM TableName WHERE FieldName LIKE ""A*"""
Set RS = DB.OpenRecordset(SQL)
RS.MoveLast
RS.MoveFirst
If RS.RecordCount > 0 Then
 ' Write your code to handle the results
Else
 ' No matches found, what to do, write here
End If
End Sub


If you need further help, drop me an e-mail...

Comment/Reply (w/o sign-up)

Galahad
I apologize for double post, I got "Cannot find server" page, and then I just hit the back button, and submited again... Please moderators, delete this, and the post before this... Sorry

Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


Searching Video's for quiz, code
See Also,
advertisement


Quiz Code

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com