【问题标题】:Fastest way to check in linked table that record doesn't already exist签入记录不存在的链接表的最快方法
【发布时间】:2015-12-10 07:14:44
【问题描述】:

下午好,我需要一些帮助。有十几种方法可以解决我要问的问题,但哪种方法最快(我希望忽略一些)

我现在有 2 种方法,慢速和超慢速(2 更快)

如果生成的随机数在链接表中不存在,它会创建新记录。

表格越大,代码运行越慢。在不久的将来,添加几个代码可能需要几天时间。

添加记录的代码:

Sub MakenNieuweNummers(AantalNieuweNummers As Long, strProduct As String, strBatch As String)
Dim strCode As String
Dim AantalNummersGemaakt As Long
Dim strSQL As String
'Vul hier het aantal nieuwe gewenste nummers in om de database mee uit te breiden

Do While AantalNummersGemaakt < AantalNieuweNummers
DoEvents
strCode = randomstring(6)
If DCount("code", "tblNummers", "code = '" & strCode & "'") = 0 Then

strSQL = "insert into tblNummers " & _
        "(code,actief,printdatum,product,batchnummer) " & _
        "VALUES ('" & strCode & "',TRUE,#" & Format(Date, "MM-DD-YYYY") & "#,'" & strProduct & "','" & strBatch & "')"
dbLocal().Execute strSQL
AantalNummersGemaakt = AantalNummersGemaakt + 1
End If

Loop

End Sub


Sub MakenNieuweNummers2(AantalNieuweNummers As Long, strProduct As String, strBatch As String)
Dim strCode As String
Dim AantalNummersGemaakt As Long
Dim strSQL As String
'Vul hier het aantal nieuwe gewenste nummers in om de database mee uit te breiden

Do While AantalNummersGemaakt < AantalNieuweNummers
DoEvents
strCode = randomstring(6)

If dbLocal().OpenRecordset("SELECT Count([ID]) AS [CountALL] FROM tblNummers WHERE code='" & strCode & "';")![CountALL] = 0 Then

strSQL = "insert into tblNummers " & _
        "(code,actief,printdatum,product,batchnummer) " & _
        "VALUES ('" & strCode & "',TRUE,#" & Format(Date, "MM-DD-YYYY") & "#,'" & strProduct & "','" & strBatch & "')"
dbLocal().Execute strSQL
AantalNummersGemaakt = AantalNummersGemaakt + 1
End If

Loop

End Sub

还有从函数返回的随机字符串的代码

Function randomstring(Optional iLengte As Integer) As String


If IsMissing(iLengte) Then
    iLengte = 6
End If
Randomize

Do While Len(randomstring) < iLengte
randomstring = randomstring & Mid(sReeks, Int((Len(sReeks)) * Rnd) + 1, 1)
Loop

End Function

非常感谢任何帮助。

提前致谢。

【问题讨论】:

    标签: ms-access-2010 database-performance linked-tables record-count


    【解决方案1】:

    回答我自己的问题...

    通过保持记录集打开并使用“.addNew”添加新条目,我获得了很多性能
    每次循环后,我都会“更新”记录集以保存更改。
    因为每个新条目都会填充一个唯一的索引字段。 当发生双倍时,这最终可能会引发错误 3022
    我将使用错误处理程序捕获此错误并在更新之前恢复到标记,并在“.Update”之前为该字段尝试另一个值

    这就是它的样子:

    Sub MakenNieuweNummers(AantalNieuweNummers As Long, strProduct As String, strBatch As String)
    On Error GoTo MakenNieuweNummers_err
    
    Dim AantalNummersGemaakt As Long
    Dim rst As DAO.Recordset
    
    Set rst = dbLocal().OpenRecordset("tblNummers", , dbFailOnError)
    With rst
      Do While AantalNummersGemaakt < AantalNieuweNummers
      DoEvents
      .AddNew
    
    MakenNieuweNummers_next:
      !code = randomstring(6)
      .Update 'Error 3022 in case of double, will let errorhandler fix this.
      AantalNummersGemaakt = AantalNummersGemaakt + 1
      Loop
    End With
    
    MakenNieuweNummers_Exit:
      rst.Close
      Set rst = Nothing
      Exit Sub
    
    MakenNieuweNummers_err:
    If Err.Number = 3022 Then
      Resume MakenNieuweNummers_next
    Else
      MsgBox Err.Number & vbNewLine & Err.Description, vbCritical
      Resume MakenNieuweNummers_Exit
    End If
    End Sub
    

    如果有更多的表现需要获得,那么请回复。总是喜欢了解更多!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-02
      • 2013-08-09
      • 1970-01-01
      • 2022-01-14
      • 2011-02-15
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多