Link to home
Create AccountLog in
Avatar of sensbury

asked on

access updating recordsets is sooo slow

I am trying to link data from three different sources into one single database and all was going well but all of a sudden I have hit a brick wall. The current updates I am running to merge the latest set of data just seem to be running so slow.  I could really do with some advice on how to optimise this section of code as we have 11,700 records to update and it is painful.  Like I said I am not an expert - just starting out with access so am sure I could be doing things better.

Sub applydescriptionstoproducts()

Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection

cnn.ConnectionString = strcnn
cnn.Open CurrentProject.Connection

Dim strprodcode As String
Dim strDescription As String
Dim strSummary As String
Dim strName As String
Dim prodrs As ADODB.Recordset
Dim deeprodrs As ADODB.Recordset
Dim proddesrs As ADODB.Recordset
Dim productid As Integer

Set prodrs = New ADODB.Recordset
prodrs.ActiveConnection = CurrentProject.Connection
prodrs.Source = "vgm_products"
prodrs.CursorType = adOpenDynamic
prodrs.LockType = adLockOptimistic

Set deeprodrs = New ADODB.Recordset
deeprodrs.ActiveConnection = CurrentProject.Connection
deeprodrs.Source = "products"
deeprodrs.CursorType = adOpenDynamic
deeprodrs.LockType = adLockOptimistic

Set proddesrs = New ADODB.Recordset
proddesrs.ActiveConnection = CurrentProject.Connection
proddesrs.Source = "vgm_product_desc"
proddesrs.CursorType = adOpenDynamic
proddesrs.LockType = adLockOptimistic

prodrs.Open "SELECT vgm_products.[code],vgm_products.[product_id] FROM vgm_products "
Do Until prodrs.EOF
    strDescription = " "
    strSummary = " "
    strName = " "
    strprodcode = prodrs!code
    productid = prodrs!product_id
    Debug.Print productid

    deeprodrs.Open "SELECT products.[short_desc],products.[name],products.[description] FROM products WHERE (((products.[ProductStyle])= " & Chr(34) & strprodcode & Chr(34) & "));"
    Do Until deeprodrs.EOF
            Rem there are blanks in this column so we need to ignore this error
            Rem that is caused by assigning null to a string
            On Error GoTo emptystringerror:
            strDescription = deeprodrs!Description
            strSummary = deeprodrs![short_desc]
            strName = deeprodrs![Name]
   proddesrs.Open "SELECT * FROM vgm_product_desc WHERE (((vgm_product_desc.[product_id])= " & productid & "));"
   Rem if we found it save the id
   Do Until proddesrs.EOF
       proddesrs![Description] = strDescription
       proddesrs![summary] = strSummary
       proddesrs![Name] = strName



Rem all done close recordsets

Set prodrs = Nothing
Set deeprodrs = Nothing
Set proddesrs = Nothing

Rem close the database connection
Set cnn = Nothing
Exit Sub

    Resume Next
Exit Sub

End Sub

Open in new window

Avatar of YohanF
Flag of United Kingdom of Great Britain and Northern Ireland image


It is slightly confusing, could you please right down the steps you want to do.

1 - open main products
2 - match in to the new file using xxx parameters
3 - Update part...

I have no clue what you are updating and why... so bit more detail on code would help you and me both realise whats going wrong..

Avatar of sensbury


hi thanks for the swift response

Basically I have three tables vgm_products, products and vgm_product_descriptions.

products is a table that has been imported from excel it has in it information about the products such as its name long description and short description.  I want to extract information about producs from this table and place it in vgm_product_descriptions.  

So I am going through vgm_products and for each product in that table I am extracting that products id (a numerical value) and its product code which is a string.

then I am going through products and searching for the record containing that product code. In the products table the product code stored in the productstyle field.  Once I have found the record, I want to extract the contents of the description, name and short_description field.

Once I have this data I  want to go to vgm_product_description and find the record that has the id that i got from vgm_products and update that with the newly found description, name and summary.

When I have completed this task I will be getting rid of the table products it is purely being used at the moment to populate vgm_product_description. But  records in vgm_product description are identified by a product id and in products they are identified by product style hence I am using  vgm_products to identify the records in the other two tables.

I hope that makes sense.
Avatar of Jeffrey Coachman
Can you define "sooo slow", in chronological terms please?

Depending on what is happening, perhaps this will never really be "Fast" in your terms...

Avatar of YohanF
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
The answer should be more like this....

There are a couple of ways to do this

1 - simple update query

do a query with 3 joins, a) vgm_products, b) vgm_product description and c) products and join them using their common fields.. and then add update statements to update appropriate items (use ms access query builder and you will be done with this within no time)

2 - the vba method

a) open up a record set, combining both vgm_products and products  (rather than opening 2 record sets)
prodrs.Open "SELECT vgm_products.[code],vgm_products.[product_id], products.[short_desc],products.[name],products.[description] FROM vgm_products INNER JOIN products ON products.[ProductStyle] = vgm_products.[code]"

Open in new window

please check the query syntax, there may be small errors in syntax

b) then for each iteration, open the vgm_description table/appropriate record and update it

and if you want to update you have to use following code (see the edit and update lines of code).. This way you will only open up 1 record and no need to iterate - which is waste of a line of code (unless it appears more than once)
  proddesrs.Open "select * from vgm_description where xxx =" & xxxx

if proddeers.eof <> true then

       proddesrs![Description] = strDescription
       proddesrs![summary] = strSummary
       proddesrs![Name] = strName


Open in new window

Hope this help... buzzz if you need more info...
Thanks Yohan this really helped to speed up my task. I had some errors in my code anyway but once I fixed those and added your additional optimisation the sub updates 11,700 records in a matter of minutes.instead of never finishing - I am very happy :)
Glad to hear... :)