Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Create MS Access database

Posted on 2006-04-03
2
Medium Priority
?
349 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!
0
Comment
Question by:SysCapstone
2 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 1000 total points
ID: 16367324
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
 

Author Comment

by:SysCapstone
ID: 16367473
thank you so much!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question