pro_vb thread: CreateDatabase Function.
Change the "Provider" parameter in the connection string from:
Provider=Microsoft.Jet.OLEDB.4.0; 'Access 2000
to
Provider=Microsoft.Jet.OLEDB.3.51; 'Access 97
-----Original Message-----
From: Desmond [mailto:desmond_otoole@h...]
Sent: Wednesday, March 14, 2001 6:35 AM
To: professional vb
Subject: [pro_vb] Re: CreateDatabase Function.
I tryed this code. It did create the database, but when i double click on
the Icon Access 97 States Unrecognized Database Format.
What is ADOX anyway and are there any books on it.
Desmond.
===
> Hi Desmond,
>
> Here is some VB code that will create a new Access database with a
table.
> It's a bit more code than is strictly necessary to create a database,
but
> it will demonstrate some other ADOX functionality as well.
>
> Please let me know if this worked for you and was what you were after.
>
> Imar
>
>
> Steps:
>
> 1. Create a new Visual Basic Project
> 2. Add a reference to "Microsoft ADO Ext. 2.5 for DDL and Security"
> 3. Add a Command button to your form
> 4. Add the following code:
>
> <CODE>
> Option Explicit
>
> Private Sub Command1_Click()
> Dim sDatabaseName As String
> sDatabaseName = "c:\testDatabase.mdb"
> CreateAccessDatabase sDatabaseName
> CreateAccessTable sDatabaseName
> End Sub
>
> Sub CreateAccessDatabase(strDBPath As String)
> Dim catNewDB As ADOX.Catalog
> Set catNewDB = New ADOX.Catalog
> catNewDB.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> "Data Source=" & strDBPath & _
> ";Jet OLEDB:Engine Type=5;"
> ' Engine Type=5 = Access 2000 Database
> ' Engine Type=4 = Access 97 Database
> Set catNewDB = Nothing
> End Sub
>
> Sub CreateAccessTable(strDBPath As String)
> Dim catDB As ADOX.Catalog
> Dim tblNew As ADOX.Table
> Set catDB = New ADOX.Catalog
> ' Open the catalog.
> catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> "Data Source=" & strDBPath
> Set tblNew = New ADOX.Table
>
> ' Add an extra column, with all settings at once.
> ' Note: You can't add attributes at the same time here.
> ' An error will occurr if you try anyway.
> Dim col As ADOX.Column
> Set col = New ADOX.Column
> Dim col2 As ADOX.Column
> Set col2 = New ADOX.Column
> With col
> .ParentCatalog = catDB
> .Type = adInteger
> .Name = "ID"
> .Properties("Autoincrement") = True
> .Properties("Description") = "Description of Column ID"
> End With
>
> With col2
> .ParentCatalog = catDB
> .Type = adVarWChar
> .Name = "Test"
> .Properties("Jet OLEDB:Allow Zero Length") = True
> .Properties("Description") = "Description of Column Test"
> End With
>
> ' Create a new Table object.
> With tblNew
> .Name = "Contacts"
> ' Create fields and append them to the
> ' Columns collection of the new Table object.
> With .Columns
> .Append col
> .Append col2
> .Append "FirstName", adVarWChar
> .Append "LastName", adVarWChar
> .Append "Phone", adVarWChar
> .Append "Notes", adLongVarWChar
>
> End With
> With .Columns("FirstName")
> .Attributes = adColNullable ' can also be adColFixed
> End With
> End With
>
> ' Add the new Table to the Tables collection of the database.
> catDB.Tables.Append tblNew
> Set catDB = Nothing
> End Sub
> </CODE>
>
>
>
>
>
>
> At 10:41 AM 3/13/2001 +0000, you wrote:
> >I am trying to create a simple Access database in VB no conection
strings
> >or passwords. The MSDN help seems to conflict with the arguments
required
> >when i am writing code. all i have at the moment is this.
> >
> >Function CreateDatabase(db As String) As Integer
> >Dim ws As Workspace
> >Dim dbs As Database
> > sPath = App.Path
> > If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"
> > Set ws = DBEngine.Workspaces(0)
> > Set dbs = ws.CreateDatabase(sPath & db)
> >End Function
> >
> >I don't even need a workspace. Is there anyconflict in the different
> >reference(s) i am using DAO 3.6 Object Library And ActiveX Objects 2.5
> >Library.
> >
> >Desmond.
>