How To Create an Access Database Through Visual Basic 4.0

Article translations Article translations
Article ID: 150418 - View products that this article applies to.
This article was previously published under Q150418
Expand all | Collapse all

On This Page


This article describes how to create an Access database programmatically with a code example. The code is useful if you need a database with a specific number of fields and records for testing purposes.


An Access database called TEST.MDB is created when you click the command button. In this example, the database contains 55 records with each record containing 40 integer fields. By changing some of the code, you alter the number of records and fields and the contents of the records.

In the Form_Load event, the code checks to see if the database exists. If the database does not exist, a command button is shown on the form. Clicking the command button creates the database.

Code Example

  1. Start Visual Basic 4.0. If it is already running, from the File menu, click New Project.
  2. Add a command button, data control, and a DBGrid control on Form1. Set the DataSource property of the DBGrid control to the Data control.
  3. Copy the following code sample to the Form1 code window:
          Option Explicit
          Dim rs1 As Recordset
          Dim rs2 As Recordset
          Dim db As Database
          Dim td As TableDef
          Dim fl As Field
          Private Sub Command1_Click()
             Dim iFields As Integer, iRecords As Integer
             ' Create the database.
             Set db = CreateDatabase("C:\test.mdb", dbLangGeneral)
             Set td = db.CreateTableDef("Table1")
             'Now that the database is created, add fields to the database
             For iFields = 1 To 5 'The last number can be changed.
                Set fl = td.CreateField("Field " & CStr(iFields), dbInteger)
                td.Fields.Append fl
             Next iFields
             db.TableDefs.Append td
             ' Now that the database has fields, add records through a
             ' recordset.
             Set rs1 = db.OpenRecordset("Table1", dbOpenTable)
             For iRecords = 1 To 10  ' For each row
                rs1.AddNew           ' add a new record.
                For iFields = 1 To 5      ' For each field in the record
                   rs1("Field " & CStr(iFields)) = iFields  ' add a number.
                Next iFields
             Next iRecords
             ' Close both the recordset and database.
             ' Populate the DBGrid control with the contents of the Recordset.
             Set db = OpenDatabase("C:\test.mdb")
             Set rs1 = db.OpenRecordset("Select * from Table1")
             Set Data1.Recordset = rs1
             Command1.Visible = False
          End Sub
          Private Sub Form_Load()
             If Dir("C:\test.mdb") = "" Then
                Command1.Caption = "Create Database"
                Command1.Visible = True
             End If
          End Sub
  4. Press the F5 key, or click Start on the Run menu to run the program. Click the Command button, and populate the DBGrid control. Note that the file TEST.MDB is also created.


Article ID: 150418 - Last Review: June 28, 2004 - Revision: 3.0
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 16-bit Enterprise Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
kbcode KB150418

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from