?
Solved

Looking for VB6 code to read SQL table export it to ascii

Posted on 2016-10-27
8
Medium Priority
?
77 Views
Last Modified: 2016-10-27
We need help in exporting a table to ascii.  The code is in an old protect in VB6.

We have:
cnn.Open "Provider=SQLOLEDB; " & _
          "Initial Catalog=" & lCompanyDB & "; " & _
          "Data Source=" & SqlServerName & "; " & _
          "integrated security=SSPI; persist security info=True;"
  
  Set rstVendor = New ADODB.Recordset
  ssql = "SELECT * from TABLE-NAME"
  rstVendor.Open ssql, cnn, adOpenKeyset, adLockOptimistic

Open in new window


We want to export the result of that query to a file.

Whats the best way to go about it?
0
Comment
Question by:rayluvs
[X]
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
  • 5
  • 3
8 Comments
 
LVL 28

Accepted Solution

by:
Ark earned 2000 total points
ID: 41863233
https://msdn.microsoft.com/en-us/library/ms676975(v=vs.85).aspx
Dim tableContent As String
Dim columnDelim As String, rowDelim As String, nullValue As String
columnDelim = ",": rowDelim = vbCrLf: nullValue = "NULL"
tableContent = recordset.GetString(adClipString, ,columnDelim, rowDelim, nullValue)

Open in new window

0
 

Author Comment

by:rayluvs
ID: 41863243
Ok have the contents in tableContent.  How do we export it to ascii?
0
 

Assisted Solution

by:rayluvs
rayluvs earned 0 total points
ID: 41863261
Found it:

Open "c:\p.txt" For Output As #1
Print #1, tableContent
Close #1

Open in new window


But what if the file thousand records? Is there a better way?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 28

Expert Comment

by:Ark
ID: 41863288
AFAIK this is the fastest way. ADO perform all reading and building string operations. Another way is using 2D array:
recordsArray = recordest.GetRows
For Excel VBA you can use  Range.CopyFromRecordset Method (Excel)
0
 

Author Comment

by:rayluvs
ID: 41863336
Can you provide an example of 'recordsArray ' based on our script below?

cnn.Open "Provider=SQLOLEDB; " & _
          "Initial Catalog=" & lCompanyDB & "; " & _
          "Data Source=" & SqlServerName & "; " & _
          "integrated security=SSPI; persist security info=True;"
  
  Set rstVendor = New ADODB.Recordset
  ssql = "SELECT * from TABLE-NAME"
  rstVendor.Open ssql, cnn, adOpenKeyset, adLockOptimistic

Open in new window

0
 
LVL 28

Expert Comment

by:Ark
ID: 41863341
dim values as Variant
values = rstVendor.GetRows
Dim txt As String
For rowNum = LBound(values, 2) To UBound(values, 2)
    For colNum = LBound(values, 1) To UBound(values, 1)
        txt = txt & values(colNum, rowNum) & ", "
    Next c
    txt = Left$(txt, Len(txt) - 1) & vbCrLf
Next r

Open in new window

Note that this code is much slower then GetString - VB6 string operations as slow. But in this case you can manipulate table's data and save data as Binary using
Put #1, values

Open in new window

Furthermore, you can get data only for cpecific column(s)
values = recordset.GetRows(, , columnName)
'or
values = recordset.GetRows(, , Array("columnName_1", "columnName_2", "columnName_5"))

Open in new window

0
 

Author Comment

by:rayluvs
ID: 41863346
Thanx!
0
 

Author Closing Comment

by:rayluvs
ID: 41863347
We chose our entry as assisted if a member, like us, forgot at the time.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…
Suggested Courses

800 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