DirectDrive
Programmer
I wrote a function that passes data from a SQL server to an Excel sheet. The function makes a copy of an Excel template file and populates the copy with the data set that was passed to it. The data makes it into the Excel sheet, but the currency data is showing up in Excel as text (it has a leading '). The Excel template already has the columns formatted to currency, but this doesn't seem to stay. I have even tried t use CCur(), but had no luck.
Any help would be appreciated.
Public Sub CreateXLS(NewFileName As String, rsS As ADODB.Recordset)
Dim i as Integer, iFieldCount as Integer, strSQL as String
Dim oconnX As ADODB.Connection 'Excel File connection
Dim rsX As ADODB.Recordset 'Excel file recordset
Set oconnX = New ADODB.Connection
Set rsX = New ADODB.Recordset
On Error GoTo err_Trap
rsS.MoveFirst 'if recordset is empty this wil generate an error
strSQL = "SELECT * FROM [Sheet1$]"
strPath = "\\webcluster1a\remote\downloads\"
Call FileCopy(strPath & "FileTemplates\StMasterCash.xls", strPath & NewFileName & ".xls"
oconnX.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPath & NewFileName & ".xls;" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
rsX.Open strSQL, oconnX, adOpenDynamic, adLockOptimistic
iFieldCount = rsS.Fields.Count
i = 1 'set to 1 so we skip the first field of the rsS recordset
Do While Not rsS.EOF
rsX.AddNew
rsX.Update
Do While i < iFieldCount
If IsNull(rsS(i)) Then
rsX(i - 1) = "NULL"
Else
If i > 6 Then
rsX(i - 1) = CCur(Trim(rsS(i)))
Else
rsX(i - 1) = Trim(rsS(i))
End If
rsX.Update
i = i + 1
Loop
rsX.Update
rsS.MoveNext
i = 1
Loop
rsX.Close
Set rsX = Nothing
oconnX.Close
Set oconnX = Nothing
exit_Sb:
Exit Sub
err_Trap:
If Err.Number = 70 Then
'code here for file already open
ElseIf Err.Number = 3021 Then
'code here for empty recordset
Else
End If
Resume exit_Sb
End Sub
Any help would be appreciated.
Public Sub CreateXLS(NewFileName As String, rsS As ADODB.Recordset)
Dim i as Integer, iFieldCount as Integer, strSQL as String
Dim oconnX As ADODB.Connection 'Excel File connection
Dim rsX As ADODB.Recordset 'Excel file recordset
Set oconnX = New ADODB.Connection
Set rsX = New ADODB.Recordset
On Error GoTo err_Trap
rsS.MoveFirst 'if recordset is empty this wil generate an error
strSQL = "SELECT * FROM [Sheet1$]"
strPath = "\\webcluster1a\remote\downloads\"
Call FileCopy(strPath & "FileTemplates\StMasterCash.xls", strPath & NewFileName & ".xls"
oconnX.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPath & NewFileName & ".xls;" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
rsX.Open strSQL, oconnX, adOpenDynamic, adLockOptimistic
iFieldCount = rsS.Fields.Count
i = 1 'set to 1 so we skip the first field of the rsS recordset
Do While Not rsS.EOF
rsX.AddNew
rsX.Update
Do While i < iFieldCount
If IsNull(rsS(i)) Then
rsX(i - 1) = "NULL"
Else
If i > 6 Then
rsX(i - 1) = CCur(Trim(rsS(i)))
Else
rsX(i - 1) = Trim(rsS(i))
End If
rsX.Update
i = i + 1
Loop
rsX.Update
rsS.MoveNext
i = 1
Loop
rsX.Close
Set rsX = Nothing
oconnX.Close
Set oconnX = Nothing
exit_Sb:
Exit Sub
err_Trap:
If Err.Number = 70 Then
'code here for file already open
ElseIf Err.Number = 3021 Then
'code here for empty recordset
Else
End If
Resume exit_Sb
End Sub