19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

346 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

3. To view an existing record, use the navigator buttons (identical to those of the<br />

data control) or the Find button to move to a record of a specified value.<br />

4. To remove the current record from the table, click the Delete button. The record<br />

is immediately deleted <strong>and</strong> cannot be recovered.<br />

5. To edit an existing record, make changes in the data <strong>and</strong> click on the Update<br />

button.<br />

6. Press the Close button to return to the Database window.<br />

At any time, the main section of the Database window contains a list of the<br />

tables you have specified. Initially the main section is blank except for Properties.<br />

(This is a list of properties pertaining to the database.) If you click the right<br />

mouse button while highlighting a table, a menu will appear with the following<br />

options:<br />

Menu Item Use<br />

Open Open the table to allow records to be added.<br />

Design Specify the design (number <strong>and</strong> types of fields).<br />

Rename Rename the given table.<br />

Delete Remove the given table from the database.<br />

Copy Structure Copies the structure of the given database with or without the data currently<br />

contained in the database.<br />

Refresh List Redisplay the list in the Database window.<br />

New Table Open the Table Structure window.<br />

New Query Open the Query Builder window.<br />

■ CREATING A DATABASE IN CODE<br />

The following lines of code create a database named “DBNAME.MDB” with two tables,<br />

TABLE1 <strong>and</strong> TABLE2. TABLE1 has the fields FIELD1A (type Text with maximum length 50)<br />

<strong>and</strong> FIELD1B (numeric of type Single). TABLE2 has analogous fields called FIELD2A <strong>and</strong><br />

FIELD2B, <strong>and</strong> also FIELD2C of type Integer. FIELD1A is a primary key of TABLE1 <strong>and</strong><br />

FIELD2A is a foreign key in TABLE2 referring to FIELD1A of TABLE1. FIELD2C is a primary<br />

key of TABLE2. The record (“alpha”, 1997) is placed in TABLE1 <strong>and</strong> the record (“alpha”,<br />

2000, 1) is placed in TABLE2. This code, which is contained in the file CREATEDB.TXT accompanying<br />

this textbook, is intended as a template that you can modify to create a database<br />

programmatically. (Note: The code can be placed into an event procedure. Before you run the<br />

program, click on References in the Project menu <strong>and</strong> make sure that “DAO 3.51 Object<br />

Library” is selected.)<br />

Dim MyDB As Database, MyWs As Workspace<br />

Dim T1, T2 As TableDef<br />

Dim T1Flds(1 To 2), T2Flds(1 To 3) As Field<br />

Dim TempFld As Field<br />

Dim T1Idx, T2Idx As Index<br />

Dim Rel As Relation<br />

Dim MyRec As Recordset<br />

’Create Database<br />

Set MyWs = DBEngine.Workspaces(0)<br />

Set MyDB = MyWs.<br />

CreateDatabase(“C:\DBNAME.MDB”, dbLangGeneral)’Create first Table, TABLE1<br />

Set T1 = MyDB.CreateTableDef(“TABLE1”)<br />

’Specify fields for TABLE1<br />

’Note the use of the optional parameter 50 for field size<br />

’If 50 is omitted, the size will default to 20<br />

Set T1Flds(1) = T1.CreateField(“FIELD1A”, dbText, 50)<br />

Set T1Flds(2) = T1.CreateField(“FIELD1B”, dbSingle)<br />

’Add the New fields to the field list in the Table

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!