Solved

SQL Select with VB6

Posted on 2011-03-06
2
780 Views
Last Modified: 2012-05-11
I know this has been answered many times but can't find the exact answer I am looking forward.  I am connecting to a database from which I want to lean how to use SQL statements in VB6.  I want to be able to select records and then output them in a text file (or screen if desired).  Once I execute the SQL SELECT * FROM database name WHERE ... how do I get just those records to the text file or screen and not the whole database.  Can someone give me some sample code?
0
Comment
Question by:grandrew
2 Comments
 

Author Comment

by:grandrew
ID: 35047607
While that is a nice site, it does not show me how to integrate with VB6.
0
 
LVL 14

Accepted Solution

by:
Brook Braswell earned 500 total points
ID: 35056101
Q1.  How to get just those records ... and not the whole database..
   A1.  Seems like your example should be SQL = "Select Fields from Table where condition"
   Example:
  DIM SQL AS STRING
  Dim RS as new adodb.recordset
  Dim CN as new adodb.connection

  cn.open "connection to your database"
  SQL = "SELECT ADDRESS, NAME FROM CUSTOMERS WHERE STATE = 'Texas' "
  rs.open SQL,CN, adOpenStatic, adLockBatchOptimistic
  DO WHILE NOT RS.EOF
  ' OUTPUT AS DESIRED....
   RS.MOVENEXT
  LOOP

Q2.  output them in a text file (or screen if desired)
   A2.  So you want an example of text file out put?
    Example:
    Dim sFile as string
    Dim SQL AS STRING
    Dim RS as new adodb.recordset
    Dim CN as new adodb.connection

    sFile = app.path & "\OutPut.txt"
    open sFile for output as #1

  cn.open "connection to your database"
  SQL = "SELECT ADDRESS, NAME FROM CUSTOMERS WHERE STATE = 'Texas' "
  rs.open SQL,CN, adOpenStatic, adLockBatchOptimistic
  DO WHILE NOT RS.EOF
       Print #1, RS!NAME & ", " & RS!ADDRESS
      RS.MOVENEXT
  LOOP
 Close #1
   
 If you wish this to out put to the screen then you need a grid control or other ActiveX object..
   then bind your RS to that object...
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…

706 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

16 Experts available now in Live!

Get 1:1 Help Now