如何在VBA中读取记录集作为字符串 [英] How to read recordset as string in VBA

查看:633
本文介绍了如何在VBA中读取记录集作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码来从数据库读取记录。当我读取时,记录集变量将表值修改为其自己的格式。
例如:



在数据库中


时间值为12345 (不是日期和时间),但是当记录集读取它时,它
来自于例如:23-06-2012
10:15:23


我刚刚发现,记录集本身之后会以自己的格式存储值。



设置rs = CmdSqlData.Execute()



有没有办法将记录集定义为 String



这是代码。

  Dim rs As ADODB.RecordSet 
设置rs = CmdSqlData.Execute()
Do While(rs.EOF = FALSE And rs.BOF = FALSE)
p = rs.GetRows(1)
单元格(1,1)= p(0,0)
循环

我知道如何读取数据为String(如在数据库中),以便不会改变格式。



注意:我无法将Excel单元格格式转换为到其他要求,但我想读取一切作为字符串从表

解决方案

如果你写

  CStr(p(0,0))

对于单元格,Excel将转换为适当的内容类型,所以如果p(0,0)是一个数字,单元格将是数字。



但是,如果你写

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

单元格A1将包含'2查看,但可以被操作为串。这是从Excel的早期遗留的,在哪里输入一个字符串,你必须用单引号前缀。

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


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

解决方案

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

这篇关于如何在VBA中读取记录集作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆