How to read recordset as string in VBA

谁都会走 提交于 2019-12-10 15:59:27

问题


I have some code to read records from a database. when i read , the recordset variable modifies the table value to its own format. For Eg:

In Database

Time value is 12345 (Not date & time) but when record set reads it, it comes as For Eg: 23-06-2012 10:15:23

I just found that the recordset itself stores values in its own format after doing.

Set rs = CmdSqlData.Execute()

So is there any way to define a recordset as String?

Here is the code.

Dim rs As ADODB.RecordSet
Set rs = CmdSqlData.Execute()
Do While (rs.EOF = FALSE And rs.BOF = FALSE)
  p = rs.GetRows(1)
  cell(1,1) = p(0,0)
Loop

Can anyone please let me know how to read the data as String (as it is in database) so that no change in format will occur.

Note: I can not convert Excel cell format due to other requirements but I want to read everthing as String From Table


回答1:


If you write

CStr(p(0,0))

To a cell, Excel will convert to the appropriate type for the content, so if p(0,0) is a number, the cell will be numeric.

However, if you write

ActiveSheet.Cells(1, 1) = "'" & p(0, 0)

Cell A1 will contain '2 to view, but can be manipulated as a string. This is left over from the early days of Excel, where to enter a string you had to prefix it with a single quote.

A1
2   
=A1=2   FALSE
=A1="2" TRUE


来源:https://stackoverflow.com/questions/11153073/how-to-read-recordset-as-string-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!