简体   繁体   English

签入链接表中记录不存在的最快方法

[英]Fastest way to check in linked table that record doesn't already exist

Good afternoon, i require some help. 下午好,我需要一些帮助。 there are dozen of methods that would do the trick that im about to ask but which would be fastest (i hopefull am overlooking some) 有很多方法可以实现我要问的窍门,但是最快(我希望可以忽略一些方法)

i've got 2 methods now, both slow and superslow (2 is faster) 我现在有2种方法,慢速和超慢速(2种更快)

the thing it does is create new records if generated random number doesnt already exist in linked table. 如果链接表中不存在生成的随机数,它所做的就是创建新记录。

the bigger the table gets too populate, the slower the code will run. 表填充得越大,代码运行的速度就越慢。 in near future it might probably take days to add just a few codes. 在不久的将来,可能只需要几天的时间就可以添加一些代码。

the bit of code that add's records: 添加记录的部分代码:

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

also the bit of code for the random strings that return from a function 也是从函数返回的随机字符串的代码位

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

Any help is greatly appreciated. 任何帮助是极大的赞赏。

thanks in advance. 提前致谢。

To answer my own question... 要回答我自己的问题...

I've gained much in performance by keeping the recordset open and adding new entries with '.addNew' 通过保持记录集打开并使用'.addNew'添加新条目,我获得了很多性能
after each cycle i will '.update' the recordset to save changes. 在每个周期之后,我将“ .update”记录集以保存更改。
Because each new entry will fill an unique index field. 因为每个新条目将填充唯一的索引字段。 this might eventually raise an error 3022 when a double occures 当出现双重错误时,这最终可能会引发错误3022
i will trap this error with the errorhandler and resume to an Marker just before the update and try another value for the field before '.Update' 我将使用错误处理程序捕获此错误,并在更新之前恢复到标记,并在'.Update'之前为该字段尝试另一个值

This is what it looks like: 看起来是这样的:

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

If there is more performance to be gained, then please do reply. 如果要获得更多性能,请回复。 always love to learn more! 一直喜欢学习更多!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM