Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Select with VB6

Posted on 2011-03-06
2
Medium Priority
?
794 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…

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