Solved

How to Creat a blank access 2000 database in VB

Posted on 2003-11-05
6
181 Views
Last Modified: 2010-05-03
How would one generate a blank access 2000 database called "Backup.mdb" in vb code?

   - Marc
0
Comment
Question by:MarcGraff
  • 3
  • 3
6 Comments
 
LVL 8

Assisted Solution

by:Rog
Rog earned 250 total points
ID: 9689176
Make sure your project has references to the Microsoft DAO 3.51 Object Library.


Sub CreateDatabaseX()

 Dim wrkDefault As Workspace
 Dim dbsNew As DATABASE
 Dim prpLoop As Property

 ' Get default Workspace.
 Set wrkDefault = DBEngine.Workspaces(0)

 ' Make sure there isn't already a file with the name of
 ' the new database.
 If Dir("NewDB.mdb") <> "" Then Kill "NewDB.mdb"

 ' Create a new encrypted database with the specified
 ' collating order.
 Set dbsNew = wrkDefault.CreateDatabase("NewDB.mdb", _
  dbLangGeneral, dbEncrypt)

With dbsNew
  Debug.Print "Properties of " & .Name
  ' Enumerate the Properties collection of the new
  ' Database object.
  For Each prpLoop In .Properties
   If prpLoop <> "" Then Debug.Print "    " & _
    prpLoop.Name & " = " & prpLoop
  Next prpLoop
 End With

 dbsNew.Close

End Sub


0
 
LVL 8

Accepted Solution

by:
Rog earned 250 total points
ID: 9689211
Actually you might want to use Dao 3.6 for an  access 2000 database.  This one uses ADOX object instead of the DAO.  One of these should work.

Also here is another example for you that shows better how to create tables too....

Actually, to create a new database, we need to use the ADOX object. We can
use the ADOX object to defining and retrieving a database's shema such as
database, tables. Here are some sample codes for your reference:

Private Sub Form_Load()

'if the database file already exists, kill it
If Dir("c:\newmdb.mdb") <> "" Then
    Kill "c:\newmdb.mdb"
End If

Set cat = New ADOX.Catalog
Set tbl = New ADOX.Table
Set PrimaryKey = New ADOX.Key

cat.Create "Provider= Microsoft.Jet.OLEDB.4.0" & _
              ";Data Source=c:\newmdb.mdb" & _
              ";Jet OLEDB:Engine Type=4"

  tbl.Name = "MyTable"
  tbl.Columns.Append "Column1", adInteger
  tbl.Columns.Append "Column2", adVarWChar, 50
  cat.Tables.Append tbl
 
  PrimaryKey.Name = "Column1"
  PrimaryKey.Type = adKeyPrimary
  PrimaryKey.Columns.Append "Column1"
  cat.Tables("MyTable").Keys.Append PrimaryKey

Set cat = Nothing
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider= Microsoft.Jet.OLEDB.4.0" & _
              ";Data Source=c:\newmdb.mdb"
cn.Open
 
Set rs = New ADODB.Recordset
rs.Open "SELECT column1,column2 FROM MyTable", cn, adOpenDynamic,
adLockOptimistic
rs.AddNew
rs!column1 = 1
rs!column2 = "First Value"
rs.Update

Do While rs.EOF = False
    Debug.Print "Key : " & rs!column1 & "  Value :  " & rs!column2
    rs.MoveNext
Loop
 
End Sub

0
 
LVL 1

Author Comment

by:MarcGraff
ID: 9689337
Thanks so much!

   - Marc
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 8

Expert Comment

by:Rog
ID: 9689347
Are you then going to accept this as an answer?


Thanks,

Rog
0
 
LVL 1

Author Comment

by:MarcGraff
ID: 9689373
I will, just trying to get it to work right.

   - Marc
0
 
LVL 1

Author Comment

by:MarcGraff
ID: 9689380
It is saying I need to convert the database to access 2000 when I open it and I am working on what I did wrong.

   - Marc
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

786 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