vba - ADO is truncating Excel data -
i have function gets adodb recordset contents of worksheet using ado, follows:
function worksheetrecordset(workbookpath string, sheetname string) adodb.recordset dim objconnection new adodb.connection dim objrecordset new adodb.recordset on error goto errhandler const adopenstatic = 3 const adlockoptimistic = 3 const adcmdtext = &h1 objconnection.commandtimeout = 99999999 objconnection.open "provider=microsoft.ace.oledb.12.0;" & _ "data source=" & workbookpath & ";" & _ "extended properties=""excel 12.0 xml;hdr=yes;imex=1"";" objrecordset.open "select * [" & sheetname & "$]", _ objconnection, adopenstatic, adlockoptimistic, adcmdtext if objrecordset.eof set worksheetrecordset = nothing exit function end if objrecordset.movelast objrecordset.movefirst set worksheetrecordset = objrecordset exit function errhandler: set worksheetrecordset = nothing end function i'm having problem importing number data numbers formatted 1 decimal place have 2 decimal places. happens if datatype mixed in column. example, these values:
0.03 0.05 0.08 0.13 when set them 1 decimal place in table:
+-------+-----------+ | value | | +-------+-----------+ | 0.0 | | | 0.1 | | | 0.1 | sda | | 0.1 | sdf | +-------+-----------+ then recordset gets correct 2 decimal place values. when put them in table:
+---------+-----------+ | value | | +---------+-----------+ | asdfasd | asdfas | | 0.0 | | | 0.1 | | | 0.1 | sda | | 0.1 | sdf | +---------+-----------+ then recordset gets 1 decimal place values, e.g. picks "0.0" instead of "0.03". think because string in first row causing ado treat values in columns strings displayed.
is there way can still pick text string, correct number of decimal places in number values?
edit: noticed odd. when run while workbook open, recordset gets correct decimal places. if run while workbook closed, gets displayed decimals.
try below objrecordset features , query (tested in ms query excel):
with objrecordset .cursorlocation = aduseclient .locktype = adlockoptimistic .cursortype = adopenstatic .activeconnection = objconnection .open "select format(`" & sheetname & "$`.value,'0.00') [value], [" & sheetname & "$]" end so, here jet sql format function forcing ado's sql parser output string formatted 0.00
also, have set cursortlocation property aduseclient won't need use movelast , movefirst
let know how on
philip
Comments
Post a Comment