Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get a record set column names

Status
Not open for further replies.

komyg

Programmer
Dec 13, 2005
68
BR
Hi, I am working with VB6 to make a program that makes a SQL SELECT to a generic table in a database and then outputs the information brought by the SELECT to an excel csv file.

So far I was able to output all the rows returned by the SELECT using a record set, but I cannot get column names.

The code is below:

OBS: The record set is the tabRefRecord variable and at this point in the code I have already made query on the database.

Code:
Private Sub csvCreator()

    Dim csvStr As String
    Dim str As String
    Dim i As Integer
    
    Open "C:\Documents and Settings\Administrator\Desktop\teste.txt" For Output As #1
      
    If Not tabRefRecord.RecordCount = 0 Then
        tabRefRecord.MoveFirst
    End If
    
    While Not tabRefRecord.EOF
        
        str = ""
        For i = 0 To tabRefRecord.Fields.Count - 1
            str = str & tabRefRecord.Fields(i) & ";"
        Next i
        
        Print #1, str
        tabRefRecord.MoveNext
    Wend
    
    Close #1

End Sub

As you can see I am outputing all the rows in the table except the column names.

Could you tell me how can I get the column names from the record set?

Thanks,
Komyg

PS: I'm also outputing the data to a data grid, is it possible to get the column names from the data grid?
 
Similar to getting the values, get the names.

Code:
        For i = 0 To tabRefRecord.Fields.Count - 1
            str = str & tabRefRecord.Fields(i)[!].Name[/!] & ";"
        Next i

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
You are getting Records from a TEXT File ... Not a DataBase !!!

whole new Ball game !!!
 

>You are getting Records from a TEXT File ... Not a DataBase !!!

What are you talking about?
 
Cange this
Code:
If Not tabRefRecord.RecordCount = 0 Then
to
Code:
If (Not tabRefRecord.EOF And tabRefRecord.BOF) Then

And this
Code:
For i = 0 To tabRefRecord.Fields.Count - 1
to
Code:
Dim TheFieldsCount As Long
TheFieldsCount =tabRefRecord.Fields.Count - 1
For i = 0 To TheFieldsCount
Use a ForwardOnly - ReadOnly recordset for speeding up process
 
Jerry's point is that object references are expensive in terms of processing overhead, so it's best not to put them in a loop. I also like this:
Code:
With tabRefRecord
   if Not (.EOF And .BOF) Then
       Do until .EOF
           [etc.]
       Loop
   End If
End With
The use of the With block adds an additional efficiency.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top