show column names in excel from sql VBA

ghynes

Board Regular
Joined
Dec 21, 2004
Messages
96
hey folks,

i have populated a database using VBA connecting to an SQL server, but it is not showing the column names. anyone know how i can show the column names?

thanks.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
It would help to know what code you're using but if we assume ADO etc then you can iterate the Fields within the recordset and return the field name.
 
Upvote 0
Which version of Excel and how are you populating the data? If it's via a recordset and CopyFromRecordset, then you need to loop through the fields and output the Name yourself.
 
Upvote 0
using excel 2007. here is my code.. not too sure how to write the code to loop throught the field. any help appreciated!



Code:
Sub dataextract()

' Create a connection object.
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection

' Provide the connection string.
Dim strConn As String

'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"

'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=server.com;INITIAL CATALOG=table;"

'Use an integrated login.
strConn = strConn & " UID=****; PWD=****;"

'Now open the connection.
cnPubs.Open strConn

' Create a recordset object.
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset

With rsPubs
    ' Assign the Connection object.
    .ActiveConnection = cnPubs
    ' Extract the required records.
        
    
    .Open "SELECT * FROM *


    
    ' Copy the records into cell A1 on Sheet1.
    Sheet1.Range("A1").CopyFromRecordset rsPubs
    
    
    
    ' Tidy up
    .Close
    
End With

cnPubs.Close
Set rsPubs = Nothing
Set cnPubs = Nothing



End Sub
 
Upvote 0
Code:
With rsPubs
     For i = 1 To .Fields.Count
         sheet1.Cells(1, i) = .Fields(i - 1).Name
     Next i
end with
Sheet1.Range("A2").CopyFromRecordset rsPubs
for example.
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,927
Members
448,533
Latest member
thietbibeboiwasaco

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top