Microsoft KB Archive/108146

= How to Create a Microsoft Access Database using VB Prof 3.0 =

Article ID: 108146

Article Last Modified on 1/8/2003

-

APPLIES TO


 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q108146



SUMMARY
Below is an example showing how to use the CreateDatabase function and data definition language (DDL) statements to create an empty Microsoft Access database. This example defines the structure of the Microsoft Access database, and doesn't add any data.



Step-by-Step Example
 Start a new project in Visual Basic. Form1 is created by default.  Double-click the form to open its code window. Add the following code to the Form Load event: Sub Form_Load

Const DB_LANG_GENERAL = ";LANGID=0x0809;CP=1252;COUNTRY=0" Dim db As Database Dim tdEmployee As New TableDef Dim tdStore As New TableDef

Dim fEmp_ID As New Field Dim fEmp_FirstName As New Field Dim fEmp_LastName As New Field Dim fEmp_Address As New Field

Dim fStore_ID As New Field Dim fStore_Location As New Field

Dim indxEmployee As New Index Dim indxStore As New Index

Set db = CreateDatabase("COMPANY.MDB", DB_LANG_GENERAL)

tdEmployee.Name = "Employee" tdStore.Name = "Store"

'Define all Employee fields fEmp_ID.Name = "Emp_ID" fEmp_ID.Type = 4                 'Long integer

fEmp_FirstName.Name = "Emp_FirstName" fEmp_FirstName.Type = 10         'Text (32) fEmp_FirstName.Size = 32

fEmp_LastName.Name = "Emp_LastName" fEmp_LastName.Type = 10          'Text (32) fEmp_LastName.Size = 32

fEmp_Address.Name = "Address" fEmp_Address.Type = 10           'Text (256) fEmp_Address.Size = 255

'Define all Store fields fStore_ID.Name = "Store_ID" fStore_ID.Type = 4               'Long integer

fStore_Location.Name = "Store_Location" fStore_Location.Type = 10  'Text (256) fStore_Location.Size = 255

'Add employee fields to Fields collection tdEmployee.Fields.Append fEmp_ID tdEmployee.Fields.Append fEmp_FirstName tdEmployee.Fields.Append fEmp_LastName tdEmployee.Fields.Append fEmp_Address

'Add store fields to Fields collection tdStore.Fields.Append fStore_ID tdStore.Fields.Append fStore_Location

'Define employee table index indxEmployee.Name = "INDEX_EMPLOYEE" indxEmployee.Fields = "Emp_ID" indxEmployee.Unique = True indxEmployee.Primary = True

'Define store table index indxStore.Name = "INDEX_STORE" indxStore.Fields = "Store_ID" indxStore.Unique = True indxStore.Primary = True

'Append the employee and store indexes 'to the respective Indexes collection tdEmployee.Indexes.Append indxEmployee tdStore.Indexes.Append indxStore

'Append employee and store TableDefs 'to TableDefs collection db.TableDefs.Append tdEmployee db.TableDefs.Append tdStore

End Sub  Start the program or press the F5 key. This creates a Microsoft Access database called COMPANY.MDB. End the program by closing the form. You can check that COMPANY.MDB was created correctly by opening it with Microsoft Access or with the Data Manager provided with Visual Basic. You can run the Data Manager program from the Window menu in Visual Basic, or from the Windows File Manager run DATAMGR.EXE in the Visual Basic directory.

