PDA

View Full Version : Who has ever met this?



jack nt
01-05-2014, 07:23 PM
when i get the data from access, and put it to excel with code
Cells(RowInput, C_DATA_INPUT) = RS.Fields(StrField)
excel get value different from access (datatype = single)

check in immediate window, the result as follows:
?RS.Fields(StrField)
1.99
?Cells(RowInput, C_DATA_INPUT)
1.99000000953674
? 1.99 = 1.99000000953674
False
?Cells(RowInput, C_DATA_INPUT) = RS.Fields(StrField)
True

who can expain? and how to solve the problem? (excel get exact value is 1.99)

thanks in advance
jack nt

Jan Karel Pieterse
01-06-2014, 12:50 AM
What happens if you use the CopyFromRecordset method?

Aflatoon
01-06-2014, 01:03 AM
Excel will use Double data type so it has to convert your Single to a Double which can cause small changes in value.

westconn1
01-06-2014, 02:07 AM
and how to solve the problem?round the cell to the length of the data value

Cells(RowInput, C_DATA_INPUT) = round(RS.Fields(StrField),2)if the number of decimal places in data is unknown, use some function to return the length, several methods would work

jack nt
01-06-2014, 08:03 AM
thank you all, i will try as yours.

jack nt
01-06-2014, 08:03 PM
Excel will use Double data type so it has to convert your Single to a Double which can cause small changes in value.

@Aflatoon: as your advice, i change the data type to double and it works ok. thanks again.