Solved

Database Connection

Posted on 2000-02-14
3
1,820 Views
Last Modified: 2008-03-06
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
Comment
Question by:believer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Expert Comment

by:jjmartin
ID: 2519631
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
 
LVL 1

Accepted Solution

by:
ca_heckler earned 50 total points
ID: 2519647
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
 
LVL 1

Author Comment

by:believer
ID: 2527766
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question