我的代码想要非常频繁地查询访问,我对每一行使用“for”并检查一个单元格的值是否存在于访问表中。但是我觉得性能很差。现在我使用 adodb.connection 来连接访问。抱歉,我无法输入代码,因为它不在我手中。任何人都可以帮助我了解如何从 excel vba 非常频繁地查询表并具有快速性能?
编辑:
For rowNum = 2 To 1000000
'check if title exists,if yes, get ppid, if not, insert one, get ppid, and make relation in r-table
ppID = isTitleExistReturnID(ppTitle)
If ppID = "0" Then
ppID = addPpReturnID(ppTitle, ppDate, ppJournal)
paperAddedCount = paperAddedCount + 1
isPpAdded = True
Else
isPpAdded = False
End If
Next rowNum
Function isTitleExistReturnID(title As String) As String
Dim r As New ADODB.Recordset
sqlstr = "select * from paper where title = '" & title & " '"
'MsgBox sqlstr
dbConnection.Open
r.Open sqlstr, dbConnection, adOpenKeyset, adLockOptimistic, adCmdText
If r.RecordCount < 1 Then
dbConnection.Close
isTitleExistReturnID = "0"
Else
aidi = r.Fields(0).Value
dbConnection.Close
isTitleExistReturnID = aidi
End If
End Function
Function addPpReturnID(title As String, pubDate As String, journaL As String) As String
Dim r As New ADODB.Recordset
sqlstr = "select * from paper where (1=0)"
'MsgBox sqlstr
dbConnection.Open
r.Open sqlstr, dbConnection, adOpenKeyset, adLockOptimistic, adCmdText
r.AddNew
r.Fields(1) = title
r.Fields(2) = pubDate
r.Fields(3) = journaL
r.Update
maxid = CStr(r.Fields(0).Value)
dbConnection.Close
addPpReturnID = maxid
End Function
以上是我的代码的一部分:
- 检查项目是否在访问表中
- 如果是,则返回其 ID
- 如果否,添加此项并返回 ID
做10万次以上,性能很低
任何建议将不胜感激,在此先感谢。