• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1829
  • Last Modified:

Database Connection

Hello Experts,

Last week was like Christmas around my office. I received a copy of Visual Studio, now I’m really dangerous.

Most of my time has been spent in MS Access and VBA. Now I need to get started connecting to databases, and using recordsets in VB6.

Could someone comment on the example code below that I cut out of an article.  I need to know the basics of connecting to a database, and manipulating a recordset.


Dim cn As Connection
Dim ConnStr As String, strSQL As String
Dim rst1 As Recordset

Set cn = New Connection
Set rst1 = New Recordset

cn.CursorLocation = adUseClient

ConnStr = "Provider=Microsoft.Jet.OLEDB.3.51;" & _
          " Data Source=C;\My Documents\TestTravel.mdb;" & _
          " Jet OLEDB: Database Password=MyPSW"
         
strSQL = "Select * FROM tblTravelCosts;"

cn.Open ConnStr

rst1.Open strSQL, cn, adOpenDynamic

rst1.Close: Set rst1 = Nothing
cn.Close: Set cn = Nothing


Thanks,
Believer

0
believer
Asked:
believer
1 Solution
 
jjmartinCommented:
Check out the following site for a decent explanation of ADO and DAO database access.

http://www.microsoft.com/data/ado/adotechinfo/dao2ado.htm

0
 
ca_hecklerCommented:
Bellow is some comments on the code.  This is pretty normal ADO code, I'll recommend that you check the ADODB on-line docs, and also some book, tutorial, etc.  On the VB project don't forget to set the reference to the ADO 2.1 library, otherwise it will not work.

Good luck !


'declare the ADO connection object:
Dim cn As ADODB.Connection

'Connection string and SQL statement:
Dim ConnStr As String, strSQL As String

'declare the ADO recordset
Dim rst1 As ADODB.Recordset

'Create the connection and recordset objects:
Set cn = New ADODB.Connection
Set rst1 = New ADODB.Recordset

'set to use client-side cursor
cn.CursorLocation = adUseClient

'Create a connection string, using OLEDB
ConnStr = "Provider=Microsoft.Jet.OLEDB.3.51;" & _
          " Data Source=C;\My Documents\TestTravel.mdb;" & _
          " Jet OLEDB: Database Password=MyPSW"

'SQL statement to retrieve all records/columns from table tblTravelCosts
strSQL = "Select * FROM tblTravelCosts;"

'opens a DB connection
cn.Open ConnStr

'Open the recordset (dynamic)
rst1.Open strSQL, cn, adOpenDynamic

'now do something with the open recordset
do while not rst1.EOF
   MsgBox rst1.Fields("field")
   rst1.MoveNext
loop


'Close recorset and connection, release objects:

rst1.Close
Set rst1 = Nothing

cn.Close
Set cn = Nothing


0
 
believerAuthor Commented:
jjmartin,

Thanks for the url, It has some really good example code.

ca_heckler,

Thanks for the comments. I Was pretty sure about what most of the code was for.  Your comments were helpful on the rest, especially setting the reference.

would you guys mind if I split the points ? If not, do you know how to split ?

Believer



0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now