excel - Can't set CopyFromRecordset results to Date or Number -
i'm trying import data set access 2010 excel using range.copyfromrecordset
. there fields in query of values have been cast dates or numbers, e.g.:
iif(isdate([install date]),cdate([install date]),[install date]) [install date (cast)], iif(isnumeric([feature number]),cdbl([feature number]),[feature number]) [feature number (cast)]
here code using generate worksheet data:
private sub createrssheet(db dao.database, rsname string, sheetname string) dim rs dao.recordset dim sht worksheet dim fldctr long dim columnrng range 'get recordset set rs = db.openrecordset(rsname) 'add worksheet set sht = bk.sheets.add sht.name = sheetname 'import recordset worksheet sht.range("a2").copyfromrecordset rs 'iterate through fields fldctr = 0 rs.fields.count - 1 sht.cells(1, fldctr + 1) = rs.fields(fldctr).name 'check if field date field if instr(ucase(rs.fields(fldctr).name), "date") > 0 'format column date set columnrng = sht.cells(1, fldctr + 1) columnrng.entirecolumn.numberformat = "mm/dd/yyyy" 'check if field number cast elseif instr(ucase(rs.fields(fldctr).name), "(cast)") > 0 'format column number set columnrng = sht.cells(1, fldctr + 1) columnrng.entirecolumn.numberformat = 0 end if next end sub
however, when sub runs, number columns still formatted text, , date columns still treated strings--when apply autofilter, filter lists each individual date instead of grouping them month , year.
how can these columns end desired format?
solved:
'check if field date field if instr(ucase(rs.fields(fldctr).name), "date") > 0 'format column date set columnrng = sht.cells(1, fldctr + 1) columnrng.entirecolumn.numberformat = "mm/dd/yyyy" 'refresh column values new formatting columnrng.entirecolumn.value=columnrng.entirecolumn.value 'check if field number cast elseif instr(ucase(rs.fields(fldctr).name), "(cast)") > 0 'format column number set columnrng = sht.cells(1, fldctr + 1) columnrng.entirecolumn.numberformat = 0 'refresh column values new formatting columnrng.entirecolumn.value=columnrng.entirecolumn.value end if
Comments
Post a Comment