Dim con5 As New ADODB.Connection
Dim rs5 As New ADODB.Recordset
Private Sub Command1_Click()
con5.Provider = "Microsoft.JET.OLEDB.4.0;"
con5.Open App.Path + "\da.mdb"
rs5.Open "[DEMO]", con5, adOpenStatic, adLockOptimistic

'The above code can't work when the database is protected.
'Please Tell, How can I solve it

A password protected data base always need UserID and Password.

Your problem is in ConnectionString.
This is the connection string to use when you have an access database protected with a password using the Set Database Password function in Access.

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;
Jet OLEDB:Database Password=yourdatabasepassword;

hope it could help you.

I want to make a set up file so I can't write the path (Like C:\mydatabase.mdb").I can write the following code-
con5.Provider = "Microsoft.JET.OLEDB.4.0;"
con5.Open App.Path + "\da.mdb"

in the place of Datasource. So i can't change the code. Can you help me?

What do you mean by set up file?

This is the connection string to connect to the database. You cann't overlook or skip outany of the key words (like Provider, Data Source, Database Password). You must supply or assign appropriate values for each one.
Replace the path with "App.Path", your work would be done.
You can do it like

Dim conStr As String
Dim PsWd As string

conStr = "Provider=Microsoft.Jet.OLEDB.4.0;"
conStr = conStr & "Data Source=" & App.Path & "\da.mdb;"
conStr = conStr & "Jet OLEDB:Database Password=" & PsWd & ";"

con5.ConnectionString = conStr

con5.Open
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.