首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问VBA: Scripting.Dictionary -转储到表?

访问VBA: Scripting.Dictionary -转储到表?
EN

Stack Overflow用户
提问于 2016-09-14 13:28:06
回答 1查看 1.5K关注 0票数 3

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

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

更新的>> w/解决方案<<

代码语言:javascript
复制
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. 我能把字典一堆堆地扔到桌子上吗?

基本原理:使用表结构执行下游表示和操作更容易(对我来说)。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-14 13:53:01

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

对于一个循环中的多个插入,不要使用SQL语句。相反,将DAO.Recordset.AddNew结合使用,速度会更快。

见以下答案:https://stackoverflow.com/a/33025620/3820271

下面是一个例子:https://stackoverflow.com/a/36842264/3820271

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39491756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档