Creating an Access DB & Tables

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone know of a good example for creating a access database and then
tables within that database.

All the examples I have found so far use a SQL database.

Thanks,

Ken
 
Ken

Here's a sample I cut and pasted from a prior program I worked on
Hope it helps. Note that when appending the items you can specify if the
column data is nullable, a size, a datatype etc etc.

Mike

Private Sub CreateDatabase(ByVal filename As String)

Dim CnnNew As ADODB.Connection

Dim CatNew As ADOX.Catalog

Try

' Open a Connection and create the Catalog (Database)

'

CnnNew = New ADODB.Connection

CatNew = New ADOX.Catalog

CatNew.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source=" & filename & ";" & _

"Jet OLEDB:Engine Type=5")


'Open the connection

CnnNew.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & filename)

'Open the Catalog

CatNew.ActiveConnection = CnnNew

'

'Now build all the Database tables as desired. Add or delete column

'attributes or properties as desired.

'

'Build and add the first Table

'

Dim objDesignTable As New ADOX.Table

With objDesignTable

..Name = "xxxxxxxx"

..ParentCatalog = CatNew

With .Columns

..Append("Units", ADOX.DataTypeEnum.adVarWChar)

..Append("Code", ADOX.DataTypeEnum.adVarWChar)

Append("Change X", adDouble)

..Append("Density", adDouble)

..Append("NA1", adDouble)

..Item("NA1").Attributes = ColumnAttributesEnum.adColNullable

..Append("Occasional Load Factor", adDouble)

..Append("Load Case W1", adBoolean)

..Append("Load Case T1", adBoolean)

..Append("Load X", adDouble)

..Item("Load X").Attributes = ColumnAttributesEnum.adColNullable

..Append("NA2", adDouble)

..Item("NA2").Attributes = ColumnAttributesEnum.adColNullable

..Append("FLAG1", adBoolean)

..Append("FLAG2", adBoolean)

..Append("FLAG3", adBoolean)

End With

'Create and Append a new auto incrementing column to be used

'as the primary key for the table

..Columns.Append("KeyID", adInteger)

..Columns("KeyID").Properties("AutoIncrement").Value = True

End With

'Create and Append a new key. Note that we are merely passing

'the "KeyID" column as the source of the primary key. This

'new Key will be Appended to the Keys Collection.

Dim objDesignKey As New ADOX.Key

objDesignKey.Name = "PrimaryKey"

objDesignKey.Type = KeyTypeEnum.adKeyPrimary

objDesignKey.Columns.Append("KeyID")

objDesignTable.Keys.Append(objDesignKey)

'Append the newly created table to the Tables Collection

CatNew.Tables.Append(objDesignTable)

' clean up objects

objDesignKey = Nothing

objDesignTable = Nothing

'. Add or delete column

'attributes or properties as desired.

catch

'error code here

end try

end sub
 
Ken
Add Project COM references
Microsoft ADO ext (#.#) for DDL and Security
Microsoft ActiveX Data Objects #.# Library
Mike
 
Appreicate you procedure. Just one more question: What Imports are required?
 
Michael,

In fact is only the Microsoft ADOx.x for DLL and Security needed when you do
the building from the tables with Adonet instead with ADODB.

When you want a sample of that, reply than I have sent a times that too this
newsgroup.

Cor
 
¤ Michael,
¤
¤ In fact is only the Microsoft ADOx.x for DLL and Security needed when you do
¤ the building from the tables with Adonet instead with ADODB.
¤

AFAIK you need both.

If I remember correctly, the ADOX interop assembly only accepts an ADO Connection object for the
Catalog ActiveConnection property.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
¤ Paul,
¤
¤ > AFAIK you need both.
¤ >
¤ > If I remember correctly, the ADOX interop assembly only accepts an ADO
¤ > Connection object for the
¤ > Catalog ActiveConnection property.
¤ >
¤
¤ You don't, see this sample I have showed often in the dotnet newsgroups.
¤ http://groups-beta.google.com/group/microsoft.public.dotnet.languages.vb/msg/3300033570443de4
¤
¤ I have probably a later one as well, this is a little bit overcomplete one.
¤
¤ However I hope that this makes it clear.
¤

I see, you used the Create method to specify the connection string. Nice shortcut.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Hi Cor,

When I use the adox.create I get a error - "Could not find installable
ISAM.". Is there something else that needs to be referenced.

I have ADODB, ADO Ext. 2.8 in my references.

Thanks,

Ken
 
Ken

I tried "Adox ext 2.8 for dll and security" with my sample and had no
problem at all.

Cor
 
¤ Hi Cor,
¤
¤ When I use the adox.create I get a error - "Could not find installable
¤ ISAM.". Is there something else that needs to be referenced.
¤
¤ I have ADODB, ADO Ext. 2.8 in my references.
¤

Possibly a bad connection string? You may want to post it so we can take a look.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Hi Guys,

It was the connection string DataSource is 2 words.

Thanks very much.

Ken
 
Hi Guys,

One more question - how do you release the connection?

Sub CreateDB (DB_PathName as string)

Dim NewDB As New ADOX.Catalog

NewDB.Create("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DB_PathName)

NewDB = Nothing

This code leaves the *.ldb file in the directory and it can not be deleted
until the program exits.


Ken
 
Ken,

This answer I leave for Paul, he is much more busy with access than me.

I have this problem not by the way when the program closes the ldb is gone.

I am glad you found the error by the way.

Cor
 
¤ Hi Guys,
¤
¤ One more question - how do you release the connection?
¤
¤ Sub CreateDB (DB_PathName as string)
¤
¤ Dim NewDB As New ADOX.Catalog
¤
¤ NewDB.Create("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DB_PathName)
¤
¤ NewDB = Nothing
¤
¤ This code leaves the *.ldb file in the directory and it can not be deleted
¤ until the program exits.

Try the following:

NewDB.ActiveConnection.Close()
NewDB.ActiveConnection = Nothing

ADOX can be a bit of a problem when it comes to closing connections via the COM interop layer. This
is why I typically establish the connection using the ADO Connection object instead of ADOX.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Hi Paul,

That did it! It closed the data base and removed the *.ldb file.

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top