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!
SysCapstoneAsked:
Who is Participating?
 
clockwatcherCommented:
Try this:

  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

        ID = td.CreateField("ID", DataTypeEnum.dbLong)
        ID.Attributes = DAO.FieldAttributeEnum.dbAutoIncrField
        td.Fields.Append(ID)

        Juris = td.CreateField("Juris", DataTypeEnum.dbText, 50)
        td.Fields.Append(Juris)

        Month = td.CreateField("Month", DataTypeEnum.dbDate)
        td.Fields.Append(Month)

        Type = td.CreateField("Type", DataTypeEnum.dbText, 50)
        td.Fields.Append(Type)

        Count = td.CreateField("Count", DataTypeEnum.dbInteger)
        td.Fields.Append(Count)

        IDX = td.CreateIndex("IDXID")
        IDX.Fields.Append(IDX.CreateField("ID"))
        IDX.Primary = True
        td.Indexes.Append(IDX)

        db.TableDefs.Append(td)

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

    End Function
0
 
SysCapstoneAuthor Commented:
thank you so much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.