Question : How to speed up VBA Code ADODB Recordset updates

Please help I am new to VBA as you will see from my code. Probelm is i am trying to up date someone elses table that I can not alter. I Need to check if a row exists and if not add it. The code I have written works  but very slow. They do not have a autonumber so i look up the max number first. I have a list of variables in my table the other table should have. So I read through them and then check if they exist if not add them.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
Public Function AddVaribales(ProductRef As String)
Dim MaxRecord_New As Long
Dim rst As ADODB.Recordset
Dim strSQL As String
 
Dim VariableID As Long
Dim ContentLevel As Integer
Dim Status As String
Dim nUseParentSetting As Integer
Dim sValue As String
 
nUseParentSetting = 0
sValue = "0"
 
MaxRecord_New = DMax("[nID]", "[UserDefinedProperties]") + 1
 
Set rst = New ADODB.Recordset
'Read what variables should in actinic
strSQL = "SELECT VariablesProductShouldHave.[nVariableID], VariablesProductShouldHave.[sName], VariablesProductShouldHave.[nContentLevel], VariablesProductShouldHave.[sStatus] FROM VariablesProductShouldHave;"
rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdText
Do While Not rst.EOF
VariableID = rst("nVariableID")
ContentLevel = rst("nContentLevel")
Status = rst("sStatus")
 
AddRecords (ProductRef), (VariableID), (ContentLevel), (sStatus), (nUseParentSetting), (sValue)
rst.MoveNext
Loop
MsgBox "Update Complete...."
 
End Function
 
Public Function AddRecords(ProductRef As String, VariableID As Long, ContentLevel As Integer, Status As String, nUseParentSetting As Integer, sValue As String)
 
'Add records to table
Dim rst2 As ADODB.Recordset
Dim strSQL As String
'strSQL = "SELECT * From UserDefinedProperties WHERE nID=0"
' Check If It Exists first
strSQL = "SELECT UserDefinedProperties.*, UserDefinedProperties.[nVariableID], UserDefinedProperties.[sContentID] From UserDefinedProperties WHERE (((UserDefinedProperties.[nVariableID])=" & VariableID & ") AND ((UserDefinedProperties.[sContentID])='" & ProductRef & "'));"
Set rst2 = New ADODB.Recordset
rst2.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdText
If rst2.EOF = True Then
rst2.Close
Set rst2 = Nothing
strSQL = "SELECT * From UserDefinedProperties WHERE nID=0"
Set rst2 = New ADODB.Recordset
rst2.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdText
rst2.AddNew
rst2("nID") = MaxRecord_New
rst2("nUseParentSetting") = nUseParentSetting
rst2("sValue") = sValue
rst2("sContentID") = ProductRef
rst2("nContentLevel") = ContentLevel
rst2("sStatus") = Status
rst2("nVariableID") = VariableID
rst2.Update
MaxRecord_New = MaxRecord_New + 1
End If
 
rst2.Close
Set rst2 = Nothing
End Function

Answer : How to speed up VBA Code ADODB Recordset updates

I'd agree with Jim and Gustav that forming one procedure is most useful when there isn't repeated opening of a subsequent recordset.

As further performance hints...
You could do with limiting these subsequent recordsets as much as possible.
In your current scenario you open a targeted recordset each time based on the single ID passed to it.
You could perform a join such that your appending recordset is opened only containing matching rows for all the rows in VariablesProductShouldHave.  (The idea being making the recordset in which you're providing Find operations as small as possible to facilitate faster Find times).

If you're Finding upon the primary key value of the recordset source then that'll be as efficient as that gets most likely.
However you could still experiment with a Client side cursor instead (longer to load initially, but then is contained locally).
This would offer the other possible advantage (especially if operating on a non-key field) of being able to create an index on that field in the recordset itself to improve Find times.

If you want more on that then just shout (you might want to remain KeySet and server side).

Random Solutions  
 
programming4us programming4us