clchapman
asked on
Maintain text data type when transferring data from Access to Excel via ADODB
I am transferring data from Access to Excel via an ADODB recordset. It is speedy and works perfectly with one exception. There are several Text fields in which the data housed is numeric. When I transfer the data to Excel, all but one are formatted correctly. The lone, rebel field ("Claim ID") is displayed as: 1.21317E+17. In Excel all fields are formatted as "General". There are only two differences I see between the correctly displayed fields and "Claim ID": (1) the number of characters in "Claim ID" is 18, the other are 11 characters or less (probably not the problem); (2) the values in the "Claim ID" field all end with three 0's (i.e., 130315089476146000, 130665133631147000, etc.).
How can I revise my code (see below) so that the "Claim ID" field formats correctly once it is in Excel?
Sub Data2ExcelTest()
'Transfer daily inventory transctions to Excel
Dim rsHNresponse As ADODB.Recordset
Dim objExcel As Excel.Application
Dim objBook As Excel.Workbook
Dim objSheet As Excel.Worksheet
Dim fld As ADODB.Field
Dim intCol As Integer
Dim intRow As Integer
Dim strSQL As String
Dim dteDateSent As Date
Dim strFileName As String
Dim strResponse As String
dteDateSent = Format(Date, "mm-dd-yyyy")
strFileName = "HN Disagree Response.xlsx"
strResponse = "Disagree Response"
'Define data to transfer
strSQL = "SELECT [Pharmacy Name], [Pharmacy ID], [Member ID]," & _
"[Member Last Name], [Fill Date], [Rx Number], [Amount Paid], NDC," & _
" [Label Name], Quantity, [Days Supply], [Compound Code], [Claim ID]," & _
" [Correct Qty] AS [Correct Quantity], [Actual Days Supply] AS [Actual DS]," & _
" [Corrected Compound Price], [3H Findings], 'N' AS Pending," & _
" [4C Sort Comment] AS [CMK Disagree], [RD Response] AS [HN Response]," & _
" (#" & dteDateSent & "#) as [RD Date Sent]," & _
" ('" & strFileName & "') as [RD FileExportConverter Name]" & _
" FROM [40 Billing and Recovery Table]" & _
" WHERE [RD Tab] = '" & strResponse & "'"
Set rsHNresponse = New ADODB.Recordset
'assign desired data into recordset
rsHNresponse.Open strSQL, CurrentProject.Connection
'Open Excel
Set objExcel = New Excel.Application
'Add Workbook
objExcel.Workbooks.Add
'Open new worksheet
Set objSheet = objExcel.ActiveSheet
'Transfer data
'start with field names
For intCol = 0 To rsHNresponse.Fields.Count - 1
Set fld = rsHNresponse.Fields(intCol )
objSheet.Cells(1, intCol + 1) = fld.Name
Next intCol
'Actual data
intRow = 2
Do Until rsHNresponse.EOF
For intCol = 0 To rsHNresponse.Fields.Count - 1
objSheet.Cells(intRow, intCol + 1) = rsHNresponse.Fields(intCol ).Value
Next intCol
rsHNresponse.MoveNext
intRow = intRow + 1
Loop
objExcel.Visible = True
Set rsHNresponse = Nothing
Set objExcel = Nothing
Set objBook = Nothing
Set objSheet = Nothing
End Sub
Thank you!
How can I revise my code (see below) so that the "Claim ID" field formats correctly once it is in Excel?
Sub Data2ExcelTest()
'Transfer daily inventory transctions to Excel
Dim rsHNresponse As ADODB.Recordset
Dim objExcel As Excel.Application
Dim objBook As Excel.Workbook
Dim objSheet As Excel.Worksheet
Dim fld As ADODB.Field
Dim intCol As Integer
Dim intRow As Integer
Dim strSQL As String
Dim dteDateSent As Date
Dim strFileName As String
Dim strResponse As String
dteDateSent = Format(Date, "mm-dd-yyyy")
strFileName = "HN Disagree Response.xlsx"
strResponse = "Disagree Response"
'Define data to transfer
strSQL = "SELECT [Pharmacy Name], [Pharmacy ID], [Member ID]," & _
"[Member Last Name], [Fill Date], [Rx Number], [Amount Paid], NDC," & _
" [Label Name], Quantity, [Days Supply], [Compound Code], [Claim ID]," & _
" [Correct Qty] AS [Correct Quantity], [Actual Days Supply] AS [Actual DS]," & _
" [Corrected Compound Price], [3H Findings], 'N' AS Pending," & _
" [4C Sort Comment] AS [CMK Disagree], [RD Response] AS [HN Response]," & _
" (#" & dteDateSent & "#) as [RD Date Sent]," & _
" ('" & strFileName & "') as [RD FileExportConverter Name]" & _
" FROM [40 Billing and Recovery Table]" & _
" WHERE [RD Tab] = '" & strResponse & "'"
Set rsHNresponse = New ADODB.Recordset
'assign desired data into recordset
rsHNresponse.Open strSQL, CurrentProject.Connection
'Open Excel
Set objExcel = New Excel.Application
'Add Workbook
objExcel.Workbooks.Add
'Open new worksheet
Set objSheet = objExcel.ActiveSheet
'Transfer data
'start with field names
For intCol = 0 To rsHNresponse.Fields.Count - 1
Set fld = rsHNresponse.Fields(intCol
objSheet.Cells(1, intCol + 1) = fld.Name
Next intCol
'Actual data
intRow = 2
Do Until rsHNresponse.EOF
For intCol = 0 To rsHNresponse.Fields.Count - 1
objSheet.Cells(intRow, intCol + 1) = rsHNresponse.Fields(intCol
Next intCol
rsHNresponse.MoveNext
intRow = intRow + 1
Loop
objExcel.Visible = True
Set rsHNresponse = Nothing
Set objExcel = Nothing
Set objBook = Nothing
Set objSheet = Nothing
End Sub
Thank you!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You may need to use an Export Spec to do this reliably. Rey has a step-by-step here:
https://www.experts-exchange.com/questions/28532814/Importing-CSV-Without-Losing-Decimals.html
You only need to create the Import Spec once, then you can refer to it for your automated import after that.
https://www.experts-exchange.com/questions/28532814/Importing-CSV-Without-Losing-Decimals.html
You only need to create the Import Spec once, then you can refer to it for your automated import after that.
Excel only recognises numbers to 15 significant digits hence why 18 digits are getting muddled.
Are the "3 zero" endings after the import or before? If it is after the import, you could be potentially losing data as Excel is taking the first 15 significant digits as mentioned above, with the remaining value getting rounded.
Thanks
Rob H
Are the "3 zero" endings after the import or before? If it is after the import, you could be potentially losing data as Excel is taking the first 15 significant digits as mentioned above, with the remaining value getting rounded.
Thanks
Rob H
ASKER
SimonAdept,
The “’” & [Claim ID] as ClaimID was giving me a syntax error. Since I have difficulty with syntax (in general), I replaced the “’” with Chr(146), which worked, but the apostrophe appeared in Excel. Thank you for the input.
chaau,
Works perfectly! Thank you.
Scott,
Since chaau’s solution worked perfectly (and was fewer steps) I went that direction, but thank you for your input as well.
Rob,
Many thanks! If you had not pointed this out I likely would have missed it and lost data without realizing it—or been completely stumped as to why. Thank you.
P.S. I am having issues with the accepting the solution and rewarding points step. I will get it resolved soon. Thanks!
The “’” & [Claim ID] as ClaimID was giving me a syntax error. Since I have difficulty with syntax (in general), I replaced the “’” with Chr(146), which worked, but the apostrophe appeared in Excel. Thank you for the input.
chaau,
Works perfectly! Thank you.
Scott,
Since chaau’s solution worked perfectly (and was fewer steps) I went that direction, but thank you for your input as well.
Rob,
Many thanks! If you had not pointed this out I likely would have missed it and lost data without realizing it—or been completely stumped as to why. Thank you.
P.S. I am having issues with the accepting the solution and rewarding points step. I will get it resolved soon. Thanks!
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Prefix the Claim ID field with a single quote to force Excel to see it as text.
strSQL = "SELECT [Pharmacy Name], [Pharmacy ID], [Member ID]," & _
"[Member Last Name], [Fill Date], [Rx Number], [Amount Paid], NDC," & _
" [Label Name], Quantity, [Days Supply], [Compound Code], "'" & [Claim ID] as ClaimID," & _