Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleBuild an Access database and a table with records using DAO
Description
Keywordsbuild database, DB, DAO, make table, Access
CategoriesDatabase
 
Get the database's file name and delete the file if it already exists. Use the DBEngine object's CreateDatabase method to make the database.

Now use the database's Execute method to execute SQL statements to create the table and to fill the table with records. Note that this example creates a DATE field. The values for this field are surrounded by # symbols and must be in the format mm/dd/yyyy even if your operating system is set to a dd/mm/yyyy format.

 
Private Sub cmdMakeDb_Click()
Dim db_name As String
Dim db As Database

    ' Get the database name.
    db_name = App.Path
    If Right$(db_name, 1) <> "\" Then db_name = db_name & _
        "\"
    db_name = db_name & "test.mdb"

    ' Delete the database if it exists.
    On Error Resume Next
    Kill db_name
    On Error GoTo 0

    ' Create the database.
    Set db = DBEngine.CreateDatabase( _
        db_name, dbLangGeneral)

    ' Create the table.
    db.Execute "CREATE TABLE Dates (TheDate DATE, TheNumber " & _
        "INTEGER)"

    ' Insert records.
    db.Execute "INSERT INTO Dates VALUES (#8/20/2002#, 0)"
    db.Execute "INSERT INTO Dates VALUES (#8/21/2002#, 1)"
    db.Execute "INSERT INTO Dates VALUES (#8/22/2002#, 2)"
    db.Execute "INSERT INTO Dates VALUES (#8/23/2002#, 3)"
    db.Execute "INSERT INTO Dates VALUES (#8/24/2002#, 4)"

    db.Close
    Set db = Nothing

    MsgBox "Done"
End Sub
 
Note that this example requires a reference to the DAO object library. That's where the DBEngine object is.

Thanks to Kent for pointing this out. This HowTo originally said it needed ADO rather than DAO.

See my book Visual Basic .NET Database Programming for information on database programming in VB .NET.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated