Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to speed up VBA Code ADODB Recordset updates

Posted on 2009-05-20
5
Medium Priority
?
763 Views
Last Modified: 2013-12-20
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.
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

Open in new window

0
Comment
Question by:taz8020
5 Comments
 
LVL 38

Expert Comment

by:Jim P.
ID: 24435689
You have two functions here: AddVariables & AddRecords

This should be able to be done in one. Can you give some sample data and how you call these functions?
0
 
LVL 3

Author Comment

by:taz8020
ID: 24439707
Table 1 ' where i list the variables the other table should have
________________
ID     VariableID   VariableName
1       86968         Un-Printed
2       09709         Printed

Table 2 , Where the variables should be
nID     nVariableID    nUseParentSetting   sValue    sContentID   nContentLevel   sStatus
356    86968            0                               £1.00      CA01            2                       N
357    09709            0                               £1.00      CA01            2                       N

Table 3 list of Products
ProductRef   Dec                      Price
CA01            Un-Printed            £1.05
CA01            Printed 1 Colour   £1.05

When I add a new product I Call AddVaribales(ProductRef)

Hope this makes sense
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 24494910
Open rst2 first with the full table.
Find the highest ID.

Run the loop.
For every record in rst, use Find to locate the ID.
If not found, add the record using next ID.

When the loop is finished, close both recordsets.

/gustav
0
 
LVL 44

Accepted Solution

by:
Leigh Purvis earned 1500 total points
ID: 24498740
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).
0
 
LVL 3

Author Closing Comment

by:taz8020
ID: 31583511
Hi Thanks I have done this through sql now, but thank you at least I know how to do it in future.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question