Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Quiz Code

Recommended Posts

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 fromthe letter A or B or both?

Share this post


Link to post
Share on other sites

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...

Dim DB As DatabaseDim RS As Recordset
Then, proceed to open database and table
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...
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):

Dim DB As DatabaseDim RS As RecordsetDim SQL As StringPrivate Sub Form_Load()Set DB = OpenDatabase("database.mdb")End SubPrivate Sub Command1_Click()SQL = "SELECT * FROM TableName WHERE FieldName LIKE ""A*"""Set RS = DB.OpenRecordset(SQL)RS.MoveLastRS.MoveFirstIf RS.RecordCount > 0 Then  ' Write your code to handle the resultsElse  ' No matches found, what to do, write hereEnd IfEnd Sub

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

Share this post


Link to post
Share on other sites

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...

Dim DB As DatabaseDim RS As Recordset
Then, proceed to open database and table
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...
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):

Dim DB As DatabaseDim RS As RecordsetDim SQL As StringPrivate Sub Form_Load()Set DB = OpenDatabase("database.mdb")End SubPrivate Sub Command1_Click()SQL = "SELECT * FROM TableName WHERE FieldName LIKE ""A*"""Set RS = DB.OpenRecordset(SQL)RS.MoveLastRS.MoveFirstIf RS.RecordCount > 0 Then  ' Write your code to handle the resultsElse  ' No matches found, what to do, write hereEnd IfEnd Sub

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.