We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Create MS Access database

SysCapstone
SysCapstone asked
on
Medium Priority
384 Views
Last Modified: 2010-04-23
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!
Comment
Watch Question

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
thank you so much!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.