如何从Excel中的记录集中获取数据?
我有以下代码:
Dim cn As Object
Dim rs As Object
Dim strSql As String
Dim strConnection As String
Dim AppPath As String
Set cn = CreateObject("ADODB.Connection")
AppPath = Application.ActiveWorkbook.Path
Set rs = CreateObject("ADODB.RecordSet")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & AppPath & "\Masterlist_Current_copy.accdb;"
strSql = "SELECT [Neptune Number],[Description],[Manufacturer],[Manufacturer P/N] FROM [All Components];"
cn.Open strConnection
Set rs = cn.Execute(strSql)
'Need Code here to get Info out of recordset
我试图从查询结果转储到其中的记录集中获取信息。我试图弄清楚如何查询记录集并获取“Neptune Number”字段中具有特定值的行数。然后,我将在我正在修改的工作表中插入正确的行数。之后,我需要获取该值的数据并将其插入工作表中。
注意:我不在乎是否使用记录集、数据表或其他任何东西,我只需要能够执行上面描述的操作。请出示代码。
I have the following code:
Dim cn As Object
Dim rs As Object
Dim strSql As String
Dim strConnection As String
Dim AppPath As String
Set cn = CreateObject("ADODB.Connection")
AppPath = Application.ActiveWorkbook.Path
Set rs = CreateObject("ADODB.RecordSet")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & AppPath & "\Masterlist_Current_copy.accdb;"
strSql = "SELECT [Neptune Number],[Description],[Manufacturer],[Manufacturer P/N] FROM [All Components];"
cn.Open strConnection
Set rs = cn.Execute(strSql)
'Need Code here to get Info out of recordset
I am trying to get information out of the recordset that has the query result being dumped into it. I'm trying to figure out how to query the recordset and get the number of rows with a specific value in the "Neptune Number" field. I will then insert the correct number of rows into the worksheet I'm modifying. After that I need to get the data for that value and insert it into the worksheet.
Note: I don't care if recordset, datatable or anything else is used I simply need to be able to do what I described above. Please show code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
到达您想要的地方的最简单方法是修改您的 SQL 语句,更改
为
这将强制 sql 查询仅返回您需要的记录。
.find
方法可用于过滤记录集,但我避免在这种情况下使用它,因为仅向数据库询问您想要的信息会更干净。要处理记录集,您可以使用以下内容
,其中 row 是数据的起始点,colOffset 是数据的起始列。请注意,此代码不会按照您在问题中指定的确切顺序执行操作(我根据需要插入行,而不是预先计算记录数。)
我避免使用
.recordcount
因为我find 根据所使用的数据库,它不会返回正确的记录计数。The easiest way to get where you I think you are asking to go is to modify your SQL statement changing
to
This will force the sql query to only return the records you need. the
.find
method can be used for filtering the recordset, but i have avoided using it in this instance as it is cleaner to just ask the db for only the information that you want.to process the recordset you can use the following
Where row is the starting point of your data and colOffset is the starting column of your data. Note that this code does not do things in the exact order you specified in your question (I am inserting rows as needed instead of calcualting the number of records up front.)
I have avoided using
.recordcount
because I find depending on the database used it will not return a correct record count.