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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

DonkeyOte

MrExcel MVP
Joined
Sep 6, 2002
Messages
9,123
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,563
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

ghynes

Board Regular
Joined
Dec 21, 2004
Messages
96
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
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,563
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

Forum statistics

Threads
1,144,611
Messages
5,725,311
Members
422,610
Latest member
sanantonio

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
Top