How to Create a Microsoft Access Database using VB Prof 3.0 (108146)



The information in this article applies to:

  • Microsoft Visual Basic Professional Edition for Windows 3.0

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.

MORE INFORMATION

Step-by-Step Example

  1. Start a new project in Visual Basic. Form1 is created by default.
  2. 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
    						
  3. Start the program or press the F5 key. This creates a Microsoft Access database called COMPANY.MDB. End the program by closing the form.
  4. 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.

REFERENCES

The VISDATA.MAK file installed in the VB3\SAMPLES\VISDATA directory loads extensive examples of data access. The VISDATA sample program uses every data access function in Visual Basic. You can refer to the VISDATA source code for examples of how to use each data access function.

Modification Type:MinorLast Reviewed:1/8/2003
Keywords:KB108146