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
Open your "Visual Basic 4.0" application.
Click the "File" menu, and then "New Project."
Insert a "DBgrid Control," "Data Control" and a "Command Button" on Form 1.
Change the DBgrid Control "Data Source Property" to the Data Control.
Copy this example code to the Form 1 code window:
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 rs1.Update Next iRecords ' Close both the recordset and database. rs1.Close db.Close ' 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
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 With Visual Basic 4.0
Start up "Visual Basic .NET."
Click "Solution Explorer," right-click "References" and then choose "Add Reference."
Click on the "COM" tab, then "Microsoft ADO Ext 2.7 for DDL and Security," followed by "Select." Click "OK."
Delete the code from the Module1.vb code window. Then copy the following code into the same window:
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
Change a path for the new .mdb file, making sure to use an existing path.
Press the "F5" button on your keyboard to run the project. The .mdb file is in Access 2000 (Jet 4.0) format.
Create a Database Using ADOX and Visual Basic .NET
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.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for