Link to home
Start Free TrialLog in
Avatar of SysCapstone
SysCapstone

asked on

Create MS Access database

I am using the following code to create an MS Access database.  It creates the database but not the table.  It turns up a first chance exception of type 'System.Runtime.InteropServices.COMException'

Public Function CreateDatabase(ByVal DBFullPath As String, ByVal InitialTable As String) As Boolean
        'PURPOSE: Creates an access database, with one table.  The
        'table will have one numeric field, ID
        'PARAMETERS: DBFULLPATH: FileName/Path of database to create
        'InitialTable: Name of table to create
        'RETURNS: TRUE IF SUCCESSFUL, FALSE OTHERWISE

        Dim db As Database
        Dim td As New TableDef
        Dim ID As New Field
        Dim Juris As New Field
        Dim Month As New Field
        Dim Type As New Field
        Dim Count As New Field

        On Error GoTo ErrorHandler
        Dim engine As New DBEngine
        db = engine.CreateDatabase(DBFullPath, DAO.LanguageConstants.dbLangGeneral)
        td = db.CreateTableDef(InitialTable)

        'Optional for add a primary key field to Table
        Dim IDX As Index
        td.Fields.Append(td.CreateField("ID", ADOX.DataTypeEnum.adInteger))
        td.Fields("ID").Attributes = DAO.FieldAttributeEnum.dbAutoIncrField
        IDX = td.CreateIndex("IDXID")
        IDX.Fields.Append(td.CreateField("ID"))
        IDX.Primary = True
        td.Indexes.Append(IDX)

        ID = td.CreateField("ID", ADOX.DataTypeEnum.adInteger)
        Juris = td.CreateField("ID", ADOX.DataTypeEnum.adVarChar)
        Month = td.CreateField("ID", ADOX.DataTypeEnum.adDate)
        Type = td.CreateField("ID", ADOX.DataTypeEnum.adVarChar)
        Count = td.CreateField("ID", ADOX.DataTypeEnum.adInteger)

        td.Fields.Append(ID)
        td.Fields.Append(Juris)
        td.Fields.Append(Month)
        td.Fields.Append(Type)
        td.Fields.Append(Count)

        db.TableDefs.Append(td)

        CreateDatabase = True
ErrorHandler:
        If Not db Is Nothing Then db.Close()

    End Function

I call the function like this in the main form:

CreateDatabase("C:\Documents and Settings\Melissa Thomason\Desktop\GRASP\VA 1 .mdb", "Crime Data")

any help appreciated.  thanks!
ASKER CERTIFIED SOLUTION
Avatar of clockwatcher
clockwatcher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SysCapstone
SysCapstone

ASKER

thank you so much!