MS ACCESS OpenRecordset舍入小数

时间:2019-03-13 15:04:21

标签: excel vba ms-access export recordset

我有一个查询,该查询显示我的产品价格并与前几个月进行比较。 Access中的查询正确显示了小数点后4位的价格,但是当我使用下面的代码导出到Excel时,数字四舍五入到了小数点后2位。 我的代码有许多格式化行,我省略了。请注意,我没有在Excel中格式化价格。我想显示整个数字。 我认为Recordset函数以某种方式将数字四舍五入。 感谢您的帮助。谢谢

            Dim xlApp As Object
            Dim xlBook As Object
            Dim xlSheet As Object
            Dim i As Integer

            Dim SQL As String
            Dim rs1 As DAO.Recordset

 SQL = "SELECT tblTempCurrentPastRawPrice.ID, tblTempCurrentPastRawPrice.[Raw Material]," & _
        "tblTempCurrentPastRawPrice.Lastbought, tblTempCurrentPastRawPrice.PreviousPrice," & _
        "tblTempCurrentPastRawPrice.Newdate, tblTempCurrentPastRawPrice.LastPrice," & _
        "tblTempCurrentPastRawPrice.ReportDate FROM tblTempCurrentPastRawPrice ORDER BY tblTempCurrentPastRawPrice.[Raw Material]"


        Set rs1 = CurrentDb.OpenRecordset(SQL, dbOpenSnapshot)


 Set xlApp = CreateObject("Excel.Application")

            xlApp.Visible = False
            Set xlBook = xlApp.Workbooks.Add
            Set xlSheet = xlBook.Worksheets(1)

    With xlSheet

   i = 7    

            Do While Not rs1.EOF


                .Range("A" & i).Value = Nz(rs1![Raw Material], "")
                .Range("B" & i).Value = Nz(rs1!Lastbought, "")
                .Range("C" & i).Value = Nz(rs1!PreviousPrice, 0)
                .Range("D" & i).Value = Nz(rs1!Newdate, 0)
                .Range("E" & i).Value = Nz(rs1!LastPrice, 0)

                i = i + 1
                rs1.MoveNext

1 个答案:

答案 0 :(得分:1)

有趣的是,我可以重现一下。发生这种情况是因为列的类型为s///

显然,Excel像往常一样要比对您有好处,要更聪明,并假定Currency始终带有两个小数点并四舍五入。

要轻松复制,请使用以下代码:

Currency

请注意,Access中的With xlSheet i = 7 .Range("F" & i).Value = CDbl(1.2345) ' Result: 1.2345 .Range("G" & i).Value = CCur(1.2345) ' Result: 1.23 € End With 仍然是CCur(1.2345)

要解决此问题,请先转换为1.2345,然后再写入Excel,然后在Excel中以Currency格式 format (带有2或4个小数位,在两种情况下,数字实际上将全部为4小数):

Double