Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

vba ADODB connection no header

I have a successful connection and data pull, but I do not get the headers.
Can someone help with with the code to pull the headers into excel row 1?

Sub RunReport()

Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Worksheets
        ws.Visible = xlSheetVisible
    Next ws

Sheets("Shipment Data").Select
Range("A2:EZ1048576").Select
Selection.ClearContents


Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

Dim DSN As String

Dim Password As Variant

If Sheets("Queries").Range("G5").Value = True Then Password = InputBox("Please enter your password here:") Else Password = ""

DSN = Sheets("Selection").Range("C2")

cn.Open "DSN=" & DSN & ";password =" & Password & "; ConnectionTimeout=0;"

Dim Query As String

Query = Sheets("Queries").Range("E2").Value

Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command

cmd.ActiveConnection = cn
cmd.CommandTimeout = 0
cmd.CommandText = Query

Set rs = cmd.Execute()
Sheets("Shipment Data").Range("A2").CopyFromRecordset rs
Set rs = Nothing

cn.Close
Set con = Nothing


Dim ShipSheet As Worksheet
Dim ShipDataRange As Range
Dim NewRange1 As String
Dim LastShipRow As Long

Set ShipSheet = Sheets("Shipment Data")

LastShipRow = ShipSheet.Cells(Rows.Count, "A").End(xlUp).Row

Set ShipDataRange = ShipSheet.Range(ShipSheet.Cells(1, "A"), ShipSheet.Cells(LastShipRow, "CF"))
  
NewRange1 = ShipSheet.Name & "!" & _
ShipDataRange.Address(ReferenceStyle:=xlR1C1)



 ActiveSheet.Range("A5").CopyFromRecordset objMyRecordset
 Range("A4").Select
    
Dim pt As PivotTable

For Each ws In ActiveWorkbook.Worksheets
    If ws.Tab.Color = RGB(255, 193, 153) Then
    For Each pt In ws.PivotTables
    pt.ChangePivotCache _
    ThisWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=NewRange1)
    Next pt
    End If
Next ws

Sheets("Queries").Visible = False

If Sheets("Queries").Range("H18").Value = True Then
Dim strFilename As String
strFilename = Sheets("Queries").Range("H19").Value
ActiveWorkbook.SaveAs Filename:=strFilename, FileFormat:=xlOpenXMLWorkbookMacroEnabled
Else
End If


End Sub

Open in new window

0
Euro5
Asked:
Euro5
  • 3
  • 2
1 Solution
 
aikimarkCommented:
CopyFromRecordset only transfers the data.  You will need to iterate the fields and transfer each field's name to get the headers you desire.
0
 
Euro5Author Commented:
aikimark
Can you help me with that?
0
 
aikimarkCommented:
Set ShipDataRange = Sheets("Shipment Data").Range("A1")
For Each fld in rs.Fields
    ShipDataRange.Value = fld.Name
    Set ShipDataRange = ShipDataRange.Offset(0,1)
Next

Open in new window

0
 
Euro5Author Commented:
I have tried many variations and still none work.
When I put yours in it still doesn't work...
Sub RunReport()

Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Worksheets
        ws.Visible = xlSheetVisible
    Next ws

Sheets("Shipment Data").Select
Range("A2:EZ1048576").Select
Selection.ClearContents


Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

Dim DSN As String

Dim Password As Variant

If Sheets("Queries").Range("G5").Value = True Then Password = InputBox("Please enter your password here:") Else Password = ""

DSN = Sheets("Selection").Range("C2")

cn.Open "DSN=" & DSN & ";password =" & Password & "; ConnectionTimeout=0;"

Dim Query As String

Query = Sheets("Queries").Range("E2").Value

Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command

    
cmd.ActiveConnection = cn
cmd.CommandTimeout = 0
cmd.CommandText = Query

Set rs = cmd.Execute()
Sheets("Shipment Data").Range("A2").CopyFromRecordset rs
Set rs = Nothing

cn.Close
Set con = Nothing

Set ShipDataRange = Sheets("Shipment Data").Range("A1")
For Each fld In rs.Fields
    ShipDataRange.Value = fld.Name
    Set ShipDataRange = ShipDataRange.Offset(0, 1)
Next


Dim ShipSheet As Worksheet
Dim ShipDataRange As Range
Dim NewRange1 As String
Dim LastShipRow As Long

Set ShipSheet = Sheets("Shipment Data")

LastShipRow = ShipSheet.Cells(Rows.Count, "A").End(xlUp).Row

Set ShipDataRange = ShipSheet.Range(ShipSheet.Cells(1, "A"), ShipSheet.Cells(LastShipRow, "CF"))
  
NewRange1 = ShipSheet.Name & "!" & _
ShipDataRange.Address(ReferenceStyle:=xlR1C1)

    
Dim pt As PivotTable

For Each ws In ActiveWorkbook.Worksheets
    If ws.Tab.Color = RGB(255, 193, 153) Then
    For Each pt In ws.PivotTables
    pt.ChangePivotCache _
    ThisWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=NewRange1)
    Next pt
    End If
Next ws

Sheets("Queries").Visible = False

If Sheets("Queries").Range("H18").Value = True Then
Dim strFilename As String
strFilename = Sheets("Queries").Range("H19").Value
ActiveWorkbook.SaveAs Filename:=strFilename, FileFormat:=xlOpenXMLWorkbookMacroEnabled
Else
End If


End Sub

Open in new window

0
 
aikimarkCommented:
When I put yours in it still doesn't work...
If that is the case, why did you accept my comment as the solution?
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now