Solved

SQL Select with VB6

Posted on 2011-03-06
2
788 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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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 In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

840 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