Solved

Database Connection

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now