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
 
 
 
 
 
 
TitleMake a database with a password and then open it with DAO
Keywordsdatabase, password
CategoriesDatabase
 
Use the ";pwd=" clause in CreateDatabase and OpenDatabase methods.

Note that this project has a reference to Microsoft DAO 3.51 Object Library.

 
Private Sub cmdCreate_Click()
Dim db_name As String
Dim password As String
Dim db As Database
Dim i As Integer
Dim txt As String

    ' Get the database name and password.
    db_name = txtDbName.Text
    password = txtPassword.Text

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

    ' Create the database.
    On Error GoTo CreateError
    Set db = DBEngine.CreateDatabase( _
        db_name, dbLangGeneral & _
            ";pwd=" & password)
    db.Close
    Set db = Nothing

    ' Verify that we can open the database.
    On Error GoTo OpenError
    Set db = DBEngine.Workspaces(0).OpenDatabase( _
        db_name, 0, 0, ";pwd=" & password)

    ' Read the table names.
    For i = 0 To db.TableDefs.Count - 1
        txt = txt & db.TableDefs(i).Name & vbCrLf
    Next i
    txtTables.Text = txt

    db.Close
    Set db = Nothing
    Exit Sub

CreateError:
    MsgBox "Error " & Format$(Err.Number) & _
        " creating database '" & db_name & "'" & _
        vbCrLf & Err.Description
    Exit Sub

OpenError:
    MsgBox "Error " & Format$(Err.Number) & _
        " opening database '" & db_name & "'" & _
        vbCrLf & Err.Description
    Exit Sub
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated