Wednesday, July 22, 2009

Connect to secure access database

This sample source code how to connect to secure access database using ADODB. Can open connection to your access database although database which you use to use password. if succeeding connection, hence will go out message using msgbox that success connection.

First of all make database, last give password at database. Afterwards, make a new form and give name "Form1". Enhancing "Commandbutton" at form, and give name "Command1". After you make form and enhance button command, double click at button "Command1", last insert code following:

Dim brec As Object
On Error GoTo Error

Set brec = CreateObject("ADODB.Connection")

With brec
.CursorLocation = 3
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\test.mdb" & ";Persist Security Info=False;Jet OLEDB:Database Password=test"
.Open
End With

MsgBox "Connection success!"

Set brec = Nothing

Error:
If Err.Number = 0 Then Exit Sub
MsgBox Err.Description, vbCritical + vbOKOnly, "Error " & Err.Number
Err.Clear


Change & App.Path & "\test.mdb & at ConnectionString as according to name of database you which have make previously. For example you of save the database at C:\test.mdb hence you can to change & App.Path & "\test.mdb & with & " C:\test.mdb &.

Accomodating also your database password, you can to change it at part of Password=test, if your password is "1234" hence you can to change Password=test become Password=1234.


1 comment:

  1. can you please tell me what are the code for save ? please!! i do not know it because vb6 is very unfamiliar programing language for me.

    ReplyDelete