Professional Documents
Culture Documents
Programming - 8
A Specialised Training Course
Contents
Form
Bound Control
Data Control
Table 1
DYNASET
Database
DatabaseName property
Determines the name and location of the source of data for a data control.
datactrl.DatabaseName = path
For Access databases, the DatabaseName property is the path and name of the database file
(.MDB ). For dBASE III, dBASE IV, Paradox, and FoxPro databases, the DatabaseName
property setting is the path to the directory of the database file (.DBF, .PDX).
RecordSource property
Once the DatabaseName property has been set this property then specifies the underlying
table, SQL statement, or QueryDef for the data control to create its Dynaset.
datactrl.RecordSource = {tablename | sqlstatement | queryname}
The Dynaset is then the source of the records accessible through bound controls on your
form.
Note: Both of the above properties are available at Design- and Run-time. If the values are
changed at Run-time the change must be registered using the Refresh Method.
Setting Description
True The database is open for single-user access. No one else can open the database until it is closed.
False (Default) The database is open for multi-user access. Others can open the database and have
access to the data while it is open.
ReadOnly property
Determines whether the control's Database is opened for read-only access.
datactrl.ReadOnly = {True | False}
Setting Description
True The control's Database is opened with read-only access to data. Changes to data are not allowed.
False (Default) The control's Database is opened with read/write access to data.
This property is used only the first time a database is opened by your application. If your
application subsequently opens other instances of the database, the property is ignored. For a
change in this property to take effect, you must close all instances of the database and then
use the Refresh method.
Connect property
Used to set the Data Control to access a database other than Microsoft Access.
datactrl.Connect [ = connectinfo ]
You can use the DatabaseName, Exclusive, ReadOnly, and Connect properties to open a
database with a data control. For example, a data control uses these property settings to open
a Paradox database:
Data1.DatabaseName = "C:\PDX\PUBS"
Data1.Exclusive = True
Data1.ReadOnly = False
Data1.Connect = "Paradox;"
Database Property
To provide access to data, a data control finds the specified database and then using the
Exclusive, ReadOnly, and Connect properties, opens it as a Database object. The Database
property is a direct reference to that object.
datactrl.Database.method
datactrl.Database.property = value
Database objects have properties and methods you can use to manage your data. You can use
any methods of a Database object with the Database property of a data control, such as Close
and Execute.
Recordset property
The Dynaset defined by a data control's Connect, DatabaseName, and RecordSource
properties.
datactrl.Recordset.property = value
datactrl.Recordset.method
The Recordset property is a reference to the underlying database object of a data control. It
has all the same properties and methods of the Dynaset object it represents.
The following example shows how you can combine the RecordCount property of a Dynaset
with the Recordset property to display the number of records in a data control's recordset:
Data1.Recordset.MoveLast
MsgBox "Records: " & Data1.Recordset.RecordCount
To set more than one value for this property, you can combine them by adding values
together. For example, to set both dbReadOnly and dbInconsistent you could use this code:
Data1.Options = dbReadOnly + dbInconsistent
If you change this property at run time, you must use the Refresh method for the change to
have any effect.
To determine if the property contains a specific value, you can use the And operator. For
example, to determine if the recordset is opened for read-only access, you could use this
code:
If Data1.Options And dbReadOnly Then...
Reposition Event
Occurs after a record becomes the current record. When a data control loads, it makes the
first record in the recordset the current record, causing the Reposition event. Whenever a
user clicks any button on the data control, moving from record to record, the Reposition event
occurs after each record becomes current. In contrast, the Validate event occurs before
moving to a different record.
You can use this event to perform calculations based on data in the current record, or to
change the form in response to data in the current record.
Validate Event
The Validate event occurs before a different record becomes the current record. The
following line illustrates the declaration line of the event procedure.
Sub datacontrol_Validate ( Action As Integer, Save As Integer)
The Action argument indicates the operation that is causing this event to occur. The possible
values are...
Constant Value Description
rdDataActionCancel 0 Cancel the operation when the Sub exits.
rdDataActionMoveFirst 1 MoveFirst method.
rdDataActionMovePrevious 2 MovePrevious method.
rdDataActionMoveNext 3 MoveNext method.
rdDataActionMoveLast 4 MoveLast method.
rdDataActionAddNew 5 AddNew method.
rdDataActionUpdate 6 Update operation (not UpdateRecord).
rdDataActionDelete 7 Delete method.
rdDataActionFind 8 Find method.
rdDataActionBookmark 9 The Bookmark property has been set.
rdDataActionClose 10 The Close method.
rdDataActionUnload 11 The form is being unloaded.
The Save argument initially indicates whether bound data has changed (True/False).
You can use this event to change values and update data as necessary. You can also choose to
save data or not, and choose to stop whatever action is causing the event to occur and
substitute a different action.
You ordinarily provide error-handling functionality for run-time errors in your code.
However, run-time errors can occur when none of your code is running, such as...
• A user clicks a data control button.
• The data control automatically opens a database and loads a recordset after the
Form_Load event.
• A custom control performs an operation such as the MoveNext method, the
AddNew method, or the Delete method.
Refresh Method
You can use the Refresh method on a data control to open or reopen the database (if the
DatabaseName, ReadOnly, Exclusive, or Connect properties have changed) and rebuild the
Dynaset in the control's Recordset property.
UpdateControls Method
Use this method to restore the contents of bound controls to their original values, such as
when a user makes changes to data and then decides to cancel the changes. This method
creates the same effect as making the current record current again, except that no events
occur.
UpdateRecord Method
Use this method to save the current contents of bound controls to the database during the
Validate event without triggering the Validate event again.
To bind a control it must be placed on the same form as a Data Control. Once that Data
Control has been linked (i.e. DatabaseName and RecordSet properties set) the bound control
can then be linked. This is done by setting the following properties...
DataSource Property
Determines the data control through which the current control is bound to a database and can
only be set at design time. To complete the connection with a database, you must also set the
DataField property...
DataField Property
control.DataField = fieldname
The DataField property specifies a valid Field object name in the data control's Recordset
property and can be set at run-time as well as at design time. If set at design time then a list of
valid field objects is available.
* - These external databases exist as directories on your disk. To create databases in these
formats, use Explorer to create a directory that becomes the database. Once the database
directory is created, use the Visual Data Manager to add tables and indexes, which become
files in this directory.
Once the Visual Data Manager is open you can either create an Access database (v2.0 or v7.0)
or open an existing database in any of the formats listed above. Once in you can create new
tables and indexes, add data to a table, change or examine the structure of a table or delete a
table. The two windows in the data manager are Database Window and SQL Statement; the
Database Window has the tree structure properties that can be doubled clicked with the
mouse to expand/collapse the default options. Also right clicking with the mouse over the
properties and choosing New Table brings up the Table Structure window where we can
add, modify or delete table and index details in our database.
The Visual Data Manager provides simple administration of our database, but be aware that
deleting tables, fields, indexes from the database schema are permanent and it is essential that
copies or backups of the schema are made first before attempting to modify the schema. If
you do not heed this warning, then significant data loss can occur with no way of retrieving
the existing data; this means that vital information could be lost and cause the business to
have substantial losses or even to collapse or cease trading.
Database
TableDefs Collection
TableDef Object
Fields Collection Indexes Collection
Database Object
A database object is a logical representation of a physical database. In the Standard Edition
you can only access such an object using the Database property of a data control. In the
Professional Edition you can also create object variables of its type using Dim and Set.
Properties
CollatingOrder Connect Exclusive
Name QueryTimeout TableDefs (coll)
Transactions Updatable (sic)
Methods
BeginTrans Close CommitTrans
CreateDynaset CreateQueryDef CreateSnapshot
DeleteQueryDef Execute ExecuteSQL
ListFields ListTables OpenQueryDef
OpenTable Rollback
A table is stored in a database and has a name that is recognised by the database. A TableDef
has a Name property that is the name of the table defined by the TableDef and is not the same
as the name of an object variable to which it is assigned.
In the Professional Edition, you can create an object variable of type TableDefs as well as
type TableDef. You can make changes in the structure of a table, and even create new tables
in a database.
Properties
Attributes Connect DateCreated
Fields (coll) Indexes (coll) LastUpdated
Name SourceTableName Updatable
Methods
Append* Delete*
* these methods relate to a collection of TableDefs.
Properties
BOF Bookmark Bookmarkable
DateCreated EOF Fields (coll)
Filter LastModified LastUpdated
LockEdits Name NoMatch
Sort Transactions Updatable
Methods
AddNew Clone CreateDynaset
CreateSnapshot Delete Edit
FindFirst FindLast FindNext
FindPrevious ListFields MoveFirst
MoveLast MoveNext MovePrevious
Update
Properties
Methods
AddNew Close CreateDynaset
CreateSnapshot Delete Edit
FindFirst FindLast FindNext
FindPrevious ListFields ListIndexes
MoveFirst MoveLast MoveNext
MovePrevious Seek Update
Properties
Attributes CollatingOrder Name
OrdinalPosition Size SourceField
SourceTable Type Value
Methods
Append AppendChunk FieldSize
GetChunk
Properties
BOF Bookmark Bookmarkable
EOF Filter NoMatch
Sort
Methods
Clone Close CreateSnapshot
FindFirst FindLast FindPrevious
ListFields MoveFirst MoveLast
MoveNext MovePrevious
Properties
Fields Name Primary
Unique
Methods
-
Exercise 1
With this exercise you will create a simple database using the Data Manager for use in later
exercises.
1 Open the Visual Data Manager, select File / New / Access 7.0 and create
EMPLOYEE.MDB in the C:\Temp directory.
2 Right mouse click on the Database Window and then click the New Table to create a
new table ( call it DEPT ). Then select the Add Field button in the Fields section to
add the fields shown below...
making sure that the ‘Unique Index Value’ and the ‘Primary Index’ check boxes are checked.
Once that’s done click Build Table.
4 Close that table design window and create a second table called EMPLOYEE. Create
the following fields and index...
Field Name Field Type Field Size
Emp Code Integer 2
Emp Name Text 25
Salary Currency 8
Dept Text 4
Date of Birth Date/Time 8
5 Return to the Database Window, double click on the Dept table. Obviously there is
no data yet so click on Add to create some. Click Update and then Add after each
record to create the next until the last record when you should click Close. Enter the
following records...
5 Now for some code. Firstly place two lines in the Load event procedure of the
Department form to show the other forms...
Sub Form_Load ()
frmEmp.Show
frmJoin.Show
End Sub
Sub Form_Load ()
' create SQL query string
Dim QSQL As String
QSQL = "SELECT [Emp Name], [Dept Name] FROM Employee, Dept "
QSQL = QSQL & "WHERE Employee.Dept = Dept.[Dept Code];"
6 Save all the forms and the project file in the Employees directory. Run your
application...
7 See how we have two forms viewing data from a table each, and a third form viewing
data from both. Try changing things about...
• Give someone a middle initial in their name, does this change reflect elsewhere?
• If you change someone’s department in the Employee form why isn’t the change
reflected in the join.
The next booklet continues with this subject, and the exercises there will extend this project.