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
Solved

Database Connection

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

792 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