Creating a Database Example

This example creates a database with two tables, each containing three fields and two indexes.

Sub CreateNewDB ()
    Dim Db As Database, Dbname As String
    Dbname = "C:\VB\MYDB.MDB"
    Set Db = CreateDatabase(Dbname, DB_LANG_GENERAL)
    NewPeopleTable Db
    NewAddressTable Db
    MsgBox "Your database, " & UCase(Dbname) & ", is ready."
End Sub
'
'
Sub NewAddressTable (D As Database)
    Dim Td As New TableDef, Fld() As New Field
    Dim Idx() As New Index, I As Integer

    ReDim Fld(1 To 5), Idx(1 To 2)

    Td.Name = "Addresses"   ' Set the table name.
    ' Create Fields.
    Fld(1).Attributes = DB_AUTOINCRFIELD    ' Counter field.
    For I = 1 To 5  ' Set properties for fields.
        Fld(I).Name = Choose(I, "ANum", "Addr1", "Addr2", "CityState", "ZIP")
        Fld(I).Type = Choose(I, DB_LONG, DB_TEXT, DB_TEXT, DB_TEXT, DB_TEXT)
        Fld(I).Size = Choose(I, 4, 30, 30, 30, 10)
        Td.Fields.Append Fld(I)
    Next I

    ' Create Indexes.
    Idx(1).Name = "PrimaryKey"
    Idx(1).Fields = "ANum"
    Idx(1).Primary = True
    Idx(1).Unique = True
    Idx(2).Name = "City"
    Idx(2).Fields = "CityState"
    For I = 1 To 2
        Td.Indexes.Append Idx(I)
    Next I

    ' Create Table.
    D.TableDefs.Append Td
End Sub
'
'
Sub NewPeopleTable (D As Database)
    Dim Td As New TableDef, Fld() As New Field
    Dim Idx() As New Index, I As Integer

    ReDim Fld(1 To 5), Idx(1 To 2)

    Td.Name = "People"  ' Set the table name.
    ' Create Fields.
    For I = 1 To 4  ' Set properties for fields.
        Fld(I).Name = Choose(I, "PNum", "LName", "FName", "Phone")
        Fld(I).Type = Choose(I, DB_LONG, DB_TEXT, DB_TEXT, DB_TEXT)
        Fld(I).Size = Choose(I, 4, 30, 30, 15)
    Next I
    Fld(1).Attributes = DB_AUTOINCRFIELD    ' Counter field.
    For I = 1 To 4
        Td.Fields.Append Fld(I)
    Next I

    ' Create Indexes.
    Idx(1).Name = "PrimaryKey"
    Idx(1).Fields = "PNum"
    Idx(1).Primary = True
    Idx(1).Unique = True
    Idx(2).Name = "Full Name"
    Idx(2).Fields = "LName,FName"
    For I = 1 To 2
        Td.Indexes.Append Idx(I)
    Next I

    ' Create Table.
    D.TableDefs.Append Td
End Sub