G
garygoodguy
Hi, I'm using the following code to pull in an Access cross-tab query t
Excel:
Option Explicit
Const TARGET_DB = "Forecast Mapping.accdb"
Sub Forecast()
'
' Forecast Macro
'
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim fld As ADODB.Field
Dim MyConn
Dim i As Long
Dim ShDest As Worksheet
Dim sSQL As String
Set ShDest = Sheets("GetData")
sSQL = "SELECT * FROM qryFTE WHERE SL ='"
Sheets("Start").Range("K41").Value & "'"
Set cnn = New ADODB.Connection
MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB
With cnn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open MyConn
End With
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open Source:=sSQL, ActiveConnection:=cnn, _
CursorType:=adOpenForwardOnly, LockType:=adLockOptimistic
_
Options:=adCmdText
'clear existing data on the sheet
ShDest.Activate
Range("A1").CurrentRegion.Clear
'create field headers
i = 0
With Range("A1")
For Each fld In rst2.Fields
.Offset(0, i).Value = fld.Name
i = i + 1
Next fld
End With
'transfer data to Excel
Range("A2").CopyFromRecordset rst
'Close the connection
rst.Close
cnn.Close
Set rst = Nothing
My problem is that the header that comes in changes the date format fro
d/mm/yyyy to m/dd/yyyy. The thing is, if I extract the qry manually fro
Access in excel it keeps the headers 'general', and thus the format i
correct.
So how can I keep the format 'gneral' in the code?
Thanks
Excel:
Option Explicit
Const TARGET_DB = "Forecast Mapping.accdb"
Sub Forecast()
'
' Forecast Macro
'
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim fld As ADODB.Field
Dim MyConn
Dim i As Long
Dim ShDest As Worksheet
Dim sSQL As String
Set ShDest = Sheets("GetData")
sSQL = "SELECT * FROM qryFTE WHERE SL ='"
Sheets("Start").Range("K41").Value & "'"
Set cnn = New ADODB.Connection
MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB
With cnn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open MyConn
End With
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open Source:=sSQL, ActiveConnection:=cnn, _
CursorType:=adOpenForwardOnly, LockType:=adLockOptimistic
_
Options:=adCmdText
'clear existing data on the sheet
ShDest.Activate
Range("A1").CurrentRegion.Clear
'create field headers
i = 0
With Range("A1")
For Each fld In rst2.Fields
.Offset(0, i).Value = fld.Name
i = i + 1
Next fld
End With
'transfer data to Excel
Range("A2").CopyFromRecordset rst
'Close the connection
rst.Close
cnn.Close
Set rst = Nothing
My problem is that the header that comes in changes the date format fro
d/mm/yyyy to m/dd/yyyy. The thing is, if I extract the qry manually fro
Access in excel it keeps the headers 'general', and thus the format i
correct.
So how can I keep the format 'gneral' in the code?
Thanks