Solved

How to Creat a blank access 2000 database in VB

Posted on 2003-11-05
6
178 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks so much!

   - Marc
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 8

Expert Comment

by:Rog
Comment Utility
Are you then going to accept this as an answer?


Thanks,

Rog
0
 
LVL 1

Author Comment

by:MarcGraff
Comment Utility
I will, just trying to get it to work right.

   - Marc
0
 
LVL 1

Author Comment

by:MarcGraff
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now