ms-access - Access VBA : Scripting. 字典 - 转储到表?

标签 ms-access vba

多亏了前面提供的帮助,我有了一种函数式方法来计算记录集中字符串之间的词频。以下代码产生所需的结果。

最后一步是将字典结构转储到 currentDB 中的 Access 表中。我可以按下面注释的方式逐步执行每个键,并通过 SQL 附加到表中 - 但它非常慢 w/24K 术语。

已更新 >> 带解决方案 <<

Private Sub Command0_Click()

Dim counts As New Scripting.Dictionary
Dim word As Variant
Dim desc As String

Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset  '<< solution
Dim strSQL As String
Dim db As Database

Set db = CurrentDb

strSQL = "SELECT DISTINCT Items.Description FROM Items ;"

Set rs1 = db.OpenRecordset(strSQL, dbOpenDynaset)

Do While Not rs1.EOF
    For Each word In Split(rs1!Description, " ")
        If Not counts.Exists(word) Then
            counts.Add word, 1
        Else
            counts.Item(word) = counts.Item(word) + 1
        End If
    Next
    rs1.MoveNext
Loop

'>> .AddNew Solution inserted as suggested below <<

rs2 = db.OpenRecordset("Freq", dbOpenTable)  

For Each word In counts.Keys
    With rs2
       .AddNew             ' Add new record
       .Fields!Term = word
       .Fields!Freq = counts(word)
       .Update
       .Bookmark = .LastModified
    End With
Next

rs2.Close

'>> End Solution <<

Set rs1 = Nothing
Set rs2 = Nothing

MsgBox "Done"

End Sub

问题:

  1. 我可以将字典批量转储到表中,而不是逐个逐个搜索键吗?

理由:(对我而言)使用表结构更容易执行下游演示和操作。

谢谢!

最佳答案

您的代码没有显示您尝试插入行的方式 - 我假设每行都有单独的 INSERT INTO (...) VALUES (...) 语句?

对于循环中的多次插入,不要使用 SQL INSERT 语句。而是将 DAO.Recordset.AddNew 一起使用, 会快很多。

查看此答案:https://stackoverflow.com/a/33025620/3820271

举个例子:https://stackoverflow.com/a/36842264/3820271

关于ms-access - Access VBA : Scripting. 字典 - 转储到表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39491756/

相关文章:

sql - 将 TSQL 转换为 MS-Access SQL

SQL 多重连接语句

mysql - 尝试执行不包含指定表达式 'StaffDetails.StaffID' 作为聚合函数一部分的查询

mysql - VBA rs.filter Mysql日期之间

当语句为假时,VBA 程序不给出正确的文本

excel - VBA 调试器仅显示集合的 256 个元素

ms-access - 将记录集导出到电子表格

ms-access - 如何在ms-access中使用ISNULL函数

excel - 迭代 VBA 字典?

vba - 将变体作为参数传递给 VBA 中的函数