如何从Excel中的记录集中获取数据?

发布于 2024-12-13 16:08:14 字数 789 浏览 2 评论 0原文

我有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅淡淡 2024-12-20 16:08:14

到达您想要的地方的最简单方法是修改您的 SQL 语句,更改

strSql = "SELECT [Neptune Number],[Description],[Manufacturer],[Manufacturer P/N] FROM [All Components];"

strSql = "SELECT [Neptune Number],[Description],[Manufacturer],[Manufacturer P/N] FROM [All Components] WHERE [Neptune Number] = 'Specific Value' ;"

这将强制 sql 查询仅返回您需要的记录。 .find 方法可用于过滤记录集,但我避免在这种情况下使用它,因为仅向数据库询问您想要的信息会更干净。

要处理记录集,您可以使用以下内容

with rs
    'will skip further processing if no records returned
    if not (.bof and .eof) then
        'assuming you do not need the headers
        'loop through the recordset
        do while not .eof
            for i = 0 to .fields.count -1
                'assuming the active sheet is where you want the data
                cells(row, i + colOffset) = .fields(i).value
            next
            Rows(Row & ":" & Row).Insert
            .movenext
        loop
    end if
end with

,其中 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

strSql = "SELECT [Neptune Number],[Description],[Manufacturer],[Manufacturer P/N] FROM [All Components];"

to

strSql = "SELECT [Neptune Number],[Description],[Manufacturer],[Manufacturer P/N] FROM [All Components] WHERE [Neptune Number] = 'Specific Value' ;"

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

with rs
    'will skip further processing if no records returned
    if not (.bof and .eof) then
        'assuming you do not need the headers
        'loop through the recordset
        do while not .eof
            for i = 0 to .fields.count -1
                'assuming the active sheet is where you want the data
                cells(row, i + colOffset) = .fields(i).value
            next
            Rows(Row & ":" & Row).Insert
            .movenext
        loop
    end if
end with

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文