views:

360

answers:

1

I create a database like this:

Sub Main()

        Dim wrkDefault As Workspace
        Dim dbE As DBEngine
        Dim dbs As Database

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

        'Set the database filename
        Dim DBFilename As String
        DBFilename = "c:\mydb.mdb"

        'Make sure there isn't already a file with the same name of
        'the new database file.
        If Dir(DBFilename) <> "" Then
            MsgBox("File already exists!")
            Exit Sub
        End If

        'Create a new encrypted database with the specified
        'collating order.
        'lock database with the password 'hello'
        dbs = wrkDefault.CreateDatabase(DBFilename, _
              LanguageConstants.dbLangGeneral & ";pwd=hello;", DatabaseTypeEnum.dbEncrypt)

        dbs.Close()

    End Sub

How do I open this database again in VB.NET using DAO?

+1  A: 
Dim cn As New ADODB.Connection
cn.Provider = "Microsoft Jet 4.0 OLE DB Provider"
cn.ConnectionString = "Data Source=c:\db1.mdb"
cn.Properties("Jet OLEDB:Database Password") = "mypwd"
cn.Open

For DAO, connection strings are most likely going to look either like this:

ODBC;DSN=DSNname;DATABASE=DBname;UID=SQLUser;PWD=SQLpassword;

or like this:

Driver={Microsoft Access Driver (*.mdb)};" & _
         "DBQ=C:\...\NWind.mdb;" & _
         "UID=admin;PWD=password;"

For the first string, you will have to create a Data Source Name (DSN) file, using Control Panel/Administrative Tools/ODBC Sources. The second string does not require a DSN connection. Now you know why they invented ADO.NET.

Robert Harvey
1. What references and namespaces do you have to include for ADODB?2. Is there a way to use OpenDatabase() method of DBEngine.Workspaces(0)?
1. Add a reference to the Microsoft ActiveX Data Objects 2.8 Library, using the COM tab of the Add References dialog in Visual Studio.
Robert Harvey
2. No, that is a DAO object. You can try adding a reference to the Microsoft DAO 3.6 Object Library, and using DAO. There is some example code here: http://www.dotnet247.com/247reference/msgs/18/90914.aspx
Robert Harvey
Anyway, the OpenDatabase() method of the DBEngine interface takes as its fourth parameter a connection string, which is probably similar in structure to one of these that includes a password: http://www.carlprothman.net/Default.aspx?tabid=90#ODBCDriverForAccess
Robert Harvey
I tried OpenDatabase(DBFilename, False, False, "MS Access;pwd=hello;") but it is not enough.
See my edit for some more connection strings. More info here: http://www.carlprothman.net/Default.aspx?tabid=90#ODBCDriverForAccess and here: http://msdn.microsoft.com/en-us/library/system.data.odbc.odbcconnection.connectionstring.aspx and here: http://support.microsoft.com/kb/193332
Robert Harvey
Please note that the password is for encrypting the database and not for controling user access. I don't believe there is a concept of a userid here. There should be a one line answer to my question.
Yes, you're right. And you're now outside of my realm of expertise, sorry. I believe there is an encryption password property on the Workspaces object, but you still need the connection string(s) I gave you. The UID and PASSWORD parts of the connection strings are optional, if you're not using them.
Robert Harvey