How to read recordset as string in VBA

2019-06-28 04:38发布

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条回答
你好瞎i
2楼-- · 2019-06-28 05:22

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
查看更多
登录 后发表回答