Programming Games in Visual Basic: Data base

Download Report

Transcript Programming Games in Visual Basic: Data base

Programming Games in Visual
Basic: Data base
Catch up questions
Data base: generalities & specifics on
Visual Basic links
Lab: work on projects.
Questions?
• Hangman
– ‘word bank’ is an array of strings
– Dynamically loading alphabet (label control array)
– String operations
• Cannonball
– moving ball (setup in Fire button Click, movement &
checking for hitting target or ground in timer)
– press button & drag
Suggestions
• Reflect [back] on a project. What was difficult?
What was similar to another project?
• Similarities & differences between projects
– Use of images in memory versus mixed & hangman
– Use of Booleans (flags) for firstturn & follow-up in
chance, first and second turns in memory
– Clicking on labels
– Data in labels (memory) versus internal variables
– Counting: matches in memory; matches in hangman;
turns in hangman
– ?
Data base terminology
is a collection of files. A data base
management system (DBMS) is a software
application for creating & modifying
databases and for producing report.
Database
Tables
Records (‘rows’)
Fields (attributes, ‘columns’)
Example (simpler than in quiz)
• Table = the questions
• A question holds:
–
–
–
–
Text of question
Answer
Value
Category
• So, in the simple case: 4 fields = 4 attributes.
Datatype of 1st and 2nd and 4th field is text.
Datatype of 3rd field is number.
Data base terminology, cont.
A query is a request for information from a data base.
SQL (structured query language) is the standard for
queries to a data base.
Select * from questions where category = ‘trivia’
Get the whole record (all attributes) from all the records from the
questions table in which the category attribute equals ‘trivia’
Select question, answer from questions where used= ‘false’
Get the question and answer attributes from the questions table in which
the used attribute is false
Select distinct category from archive
Get all the distinct (no duplicate) values of the category attribute
Advanced Warning
• Visual Basic statements involving Select
statements have complex syntax:
– A string containing the Select statement is sent to the
database.
– The string may be made by concatenating strings and
the value of variables.
– There may be single quotation marks and double
quotation marks.
– Long statements may need to be continued on the next
line, requiring an underscore _
Quiz project
• VB to DB connection for
– Controls on quiz form and DB edit form
• Set properties in the data control
– Generating list of categories
• Generate a special recordset with just one field
– Generating list of non-used questions within a specific
category
• Generate a recordset for the controls. Use Rnd to advance
the cursor to a random choice within this set.
Setting up VB to database
connections
•
Connecting data control to the database and the table (there is only one) in the
database
dbQuestions.DatabaseName = App.Path & "/quiz.mdb"
dbQuestions.RecordSource = "archive"
• In the eventhandler for setting up the list of categories
Dim dbQuestions as DataBase
Dim rst as Recordset
Set dbQuestions = OpenDatabase(App.Path & "/quiz.mdb")
Set rst = _
dbQuestions.OpenRecordset("Select distinct category from
archive")
Example of query
Note: your code has already set up dbQuestions as the link to the
database.
dbQuestions.RecordSource = "Select * from " _
& "archive where category = '" & strCat _
& "' And " _
& "used = 0“
The words
“Select * from archive where category = ‘”
are taken literally. Then it is combined with the value of strCat and then the
literal
“’ And used = 0”
The single quotation marks will surround the actual value in the variable strCat
(namely the selected category).
Visual Basic / Data base
• Data control
– at design time or run time, specify database
AND table
• Database ‘aware’ controls
– link to the data control AND link to specific
field
Click &
then select
database
Choose
table in
database
Field in
table in
database
When the pulldown
was clicked, the menu
shows the only data
control.
Visual Basic / database
• Recordset is set (sequence) of records!
• Implicit notion of the current record. This
can be changed
– movefirst
– movenext
– moveprev
Recordsets
• Can be associated with a data control and
controls on the form
• OR
• can be independent of data control (and
controls on the form)
Quiz project
Database is one table, named questions
A question is a record in the table.
Question includes the following fields:
Question text
two model answers (more on this later)
Point value (may be set as currency)
category
used switch (yes/no)
Comparing strings to patterns
• Various ways of using special characters for
patterns.
• The patterns suggested in the text use just
the wild card character *
If strAns Like strModel then
…
If strAns holds “Christopher Columbus” and
strModel holds “*Columbus” then the Like operator will return true
• Consult HELP for more possibilities
Homework/Lab
• Continue with projects
• Read text
• Note: chapter 7 / Quiz show is one of the
'advanced' projects.
• Your own project can be enhancement of
one of the 6 projects, one of the advanced
projects or an existing game or something
totally of your own design.