Solved

Database Connection

Posted on 2000-02-14
3
1,824 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 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