How to Create an Access 2007 Database in VB

Written by martin cole
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Create an Access 2007 Database in VB
(Jupiterimages/ Images)

Creating a database in Visual Basic can be useful if you need a database with a precise amount of records and fields for testing. The first method for doing this uses Visual Basic 4.0 and creates a Microsoft Office Access database with a code example that you can adapt to alter the contents of the records. The second technique uses both ADOX and Visual Basic to create an Access database using an example code.

Skill level:
Moderately Challenging

Other People Are Reading


    Create a Database With Visual Basic 4.0

  1. 1

    Open your "Visual Basic 4.0" application.

  2. 2

    Click the "File" menu, and then "New Project."

  3. 3

    Insert a "DBgrid Control," "Data Control" and a "Command Button" on Form 1.

  4. 4

    Change the DBgrid Control "Data Source Property" to the Data Control.

  5. 5

    Copy this example code to the Form 1 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 Table 1")
         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
  6. 6

    Press "F5" on your keyboard to run the program. Select the "Command" button and populate the DBgrid control. The file test.mdb is now created.

    Create a Database Using ADOX and Visual Basic .NET

  1. 1

    Start up "Visual Basic .NET."

  2. 2

    Click "Solution Explorer," right-click "References" and then choose "Add Reference."

  3. 3

    Click on the "COM" tab, then "Microsoft ADO Ext 2.7 for DDL and Security," followed by "Select." Click "OK."

  4. 4

    Delete the code from the Module1.vb code window. Then copy the following code into the same window:

    Imports ADOX

    Module Module 1

    Sub Main()
        Dim cat As Catalog = New Catalog()
        cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & _
                    "Data Source=D:\AccessDB\NewMDB.mdb;" & _
                    "Jet OLEDB:Engine Type=5")
        Console.WriteLine("Database Created Successfully")
        cat = Nothing
    End Sub

    End Module

  5. 5

    Change a path for the new .mdb file, making sure to use an existing path.

  6. 6

    Press the "F5" button on your keyboard to run the project. The .mdb file is in Access 2000 (Jet 4.0) format.

Tips and warnings

  • The technique from the section "Create a Database Using ADOX and Visual Basic .NET" can cause intermittent issues in multithreaded and service-based applications. It should only be used as a short-term method and after testing to ensure no compatibility issues exist.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.