-
Yeah,
You need to reference Microsoft Active X Data Object 2.X library. Try this and then run the code.
Also just a side note, to get data from the database your recordsets should look something like this:
Code:
dim RS as New Recordset
RS.Open "SELECT * FROM table",DB,adOpenForwardOnly,adLockReadOnly
Hope this helps
-
Negative0,
I added the reference to ADO 2.X and reran the code and it gave me an error something about "Microsoft Run time error, could not find file". Does the code you provide automatically create the file if it's not there?
Thanks, Laura
-
It doesnt create a file there, you will have to make your own mdb in Access. I have no idea how to create a database programatically, but you need a database to start with if you are going to open it. once you got one, then you can add records into a table etc.
-
Sorry,
I read the post wrong, I thought you just wanted to access the DB, not create it. I am not sure how to create it from code with ADO.
-
Thanks everyone!
Does anyone know how to create a "mdb" database using ADO connection thru VB code?
-
This may help... ;)
Code:
Sub ADOCreateDBmitTbl()
Dim cat As New ADOX.Catalog, tbl As New ADOX.Table
' create new mdb
cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=[full_filename, i.e., c:\newkid].mdb;"
' if you want to encryt it...
' & "Jet OLEDB:Encrypt Database=True;Jet OLEDB:Engine Type=2;"
' Create a new Table object.
With tbl
.Name = "your_tbl_name"
' Create fields and append them to new Table object.
' You must do this before before appending object
' to Tables collection of Catalog.
.Columns.Append "Field1_name", adVarWChar
.Columns.Append "Field2_name", adVarWChar
.Columns.Append "Field3_name", adLongVarWChar
.Columns("Field3_name").Attributes = adColNullable
' yada, yada...
End With
' Add new table to database.
cat.Tables.Append tbl
Set cat = Nothing
End Sub
-
Mongo,
The code you posted is to create new Table. What I need
is to create a new "Database". Is it possible to create
a new empty database with ADO connection using VB code?
Thanks, Laura
-
This part creates the new mdb:
Code:
cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=[full_filename, i.e., c:\newkid].mdb;"
-
Mongo, thanks for the reply. I tried your code and it works nicely. I also wanted to add password do you know the syntax for that?
Laura
-
I believe you add:
;Jet OLEDB:Database Password=MyPassword
to the end of the connection string.....
-
How do I create an older Access Database?
What parameters do I use in the cat.create command?
Thanks, Laura
-
Laura
Try ';Jet OLEDB:Engine Type=4' in the create bit
i.e.
Code:
cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=[full_filename, i.e., c:\newkid].mdb;" & _
"Jet OLEDB:Engine Type=4;"
4 = Access 97
5 = Access 2000
Hope this is of use.