can anyone plz help me out by giving the lines of code which will hwlp me establisg a connection with my access database usin ADO ......

9 Years
Discussion Span
Last Post by anud18

This is Salami Quadri
Ado programming is just a simple thing, the following steps are to be taking
1. click on project in the menu bar
2. select Component from the drop down menu
3.Microsoft ADO data control 4.0
Then click ok
4. Click on apply and ok
The control is added to the tool box, double click on it to place the control on the form
Change the name and other properties like
DatabaseName click on ... displayed at the right corner of this property to browse the name.


If you don't want to use a control (I never use one) then simply add Microsoft ActiveX Data Objects 2.1 Library in references. Then use the ADODB.Connection and ADODB.Recordset objects.

See here for a run down of ADO http://www.w3schools.com/ado/default.asp

And here for an Access (or any other) connection string


Hence you would do

Dim objConn as ADODB.Connection
Dim objRst as ADODB.Recordset
Dim lngRecordCount as Long

Set objConn = New ADODB.Connection
objConn.Open "[connectionstring]"

Set objRst = objConn.Execute("[SQL]", lngRecordCount)

Remember to set the connection string and SQL to what you actually want


I am astonished to find too many people asking the question of connectivity of database and vb.

First try to understand the properties of a connection object. You tell your ADO which database you want to contact, supply your user name and password (so the DBMS can grant you access to the database) and allow you to read,modify etc as per the previlages set for your user name. Actually the connection object is the gateway to any database.

If you open a code window of a new data project (EXE project you should have added a reference to the ADODB library) you can declare a connection object as follows:

Dim myConnection AS ADODB.Connection.

The moment you finish typing the dot(period) after the ADODB, you can see a vast list of objects with the ADO component, and you can select the one you want by the arrow keys. Declare the myConnection Object in the Code modules's declaration section. Then place the command button on the Form, name it myAccess Connection, and in the click event enter the following:

Sub Command1_click()
myConnection.ConnectionString =
"provider=Microsoft.Jet.OLEDB.4.0;" & _
"Persist Security Info=False;" & _
"Data Source = C:\Program Files\VB98\Nwind.mdb"
End Sub

The ampersand if you are using, kindly give a space before and after.
This will establish for you a connection to ACCESS NWind Database in the path shown by Data Source = c:\.....
If you want to know more please ask in the thread.


thanx fr ur help......but a problem is still there

Dim dbUserName As ADODB.Connection
Dim rsUserName As ADODB.Recordset

Set dbUserName = New ADODB.Connection
dbUserName.Open ("db1")
''dbUserName.Open "dsn=db1"
Set rsUserName = dbUserName.Execute("Select * from table ")
Do Until rsUserName.EOF
CboUserName.AddItem _
CboPassword.AddItem _
CboPassword.ItemData(CboPassword.NewIndex) = rsUserName("Serialno")
CboUserName.ItemData(CboUserName.NewIndex) = rsUserName("Serialno")

in the above code db1 is my database name for which i hv already created the user dsn....CboUserName and CboPassword are the two comboboxes that i hv used in my form
when i compile this code in the form load event ....it is giving an error in the "from clause" of the SQL statement...

can u plz help me out with this


Firstly have you checked that the connection is being established correctly, secondly what is the error


ya....connection has been established....its giving an error in the execute statement where i am executing the SQL query......its giving an error in the from clause.....


do tell me wat all refrences i need to add in my VB for opening up the database connection and executing the SQl query.....

i think i m nt being able to set up the connection.....wt mite be the problem???
plz tell me....


Please confirm by return post whether you are connecting to Access Db or MsSql Server version of the database. Then only your problem can be solved.
By the time I am writing the code for you for a cut and paste on the thread.


thanx....the problem is solved now.....it ws a connection problem only.....

i need 1 another help ....how can i encrypt my database.....
nd then how can i access that encryptd database from VB....
will i hv to decrypt it for accessing it.....nd if yes then how do i decrypt it also in VB

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.