473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How do you reliably use RecordsetClone function?

4
Using VBA/Access 2003 -
Having a weird time using the recordsetclone function. I am seeing issues where an Order Form is printed and the total is not correct, as the values used for printing is coming from the database. The problem is down to this code as far as I can see and have tested. I see that when multiple main Form records are open, this code doesn't properly update/save the curOrderTotal value in the database if there was a change of the dblsubTot. I see through testing the dblsubtot is correct, but it’s not putting into the database the value. Only does it when one Order is open perfectly. This code is run right before you print the Order to update any changes in the database that may have happened on the Order, ie changed a price of a line item.

Any ideas how to modify code this so that it always saves to the SQL database properly and reliably? Am I using recordsetclone function correctly? The main order form has two subforms on it.

Any help appreciated….thx
Alex

Expand|Select|Wrap|Line Numbers
  1. Sub cswUpdateOrder(strOrdNum As String)
  2.     On Error GoTo Error_cswUpdateOrder
  3.     ' cswUpdateOrder "OE10024-000017"
  4.  
  5.     Dim dblTaxTotal As Double
  6.     Dim dblTaxTotalRate As Double
  7.     Dim dblSubTot As Double
  8.     Dim dblSubTotRate As Double
  9.     Dim dblTaxCodeRate  As Double
  10.     Dim dblMHRTot As Double
  11.     Dim strCriteria As String
  12.  
  13.     With Forms!frmOEOrder
  14.         ' Set the Criteria for the Update.
  15.         .RecordsetClone.FindFirst "strOrderNumber = """ & strOrdNum & """"
  16.  
  17.         dblTaxTotal = 0
  18.         dblTaxTotalRate = 0
  19.         dblSubTot = 0
  20.         dblSubTotRate = 0
  21.         dblTaxCodeRate = 0
  22.         dblMHRTot = 0
  23.  
  24.         ' Update the !subDetail.Form.RecordsetClone Sales tax fields.
  25.         strCriteria = "strOrderNumber = """ & .RecordsetClone!strOrderNumber & """"
  26.         !subDetail.Form.RecordsetClone.FindFirst strCriteria
  27.  
  28.         Do Until !subDetail.Form.RecordsetClone.EOF
  29.             !subDetail.Form.RecordsetClone.Edit
  30.                 ' Get the Labor
  31.                 dblMHRTot = dblMHRTot + (!subDetail.Form.RecordsetClone!dblManHoursRequired * !subDetail.Form.RecordsetClone!dblQtyOrdered)
  32.                 dblTaxCodeRate = Nz(!subDetail.Form.RecordsetClone!dblTaxCodeRate)
  33.  
  34.                 ' Calculate the Tax
  35.                 If !strTransactionType = "RMA" Then
  36.                     !subDetail.Form.RecordsetClone!curSalesTax = ((!subDetail.Form.RecordsetClone!curSalesPrice * !subDetail.Form.RecordsetClone!dblQtyAllocated) - (!subDetail.Form.RecordsetClone!curSalesPrice * !subDetail.Form.RecordsetClone!dblQtyAllocated) * (!subDetail.Form.RecordsetClone!dblDiscount / 100)) * (dblTaxCodeRate / 100)
  37.                     !subDetail.Form.RecordsetClone!curSalesTaxRate = ((!subDetail.Form.RecordsetClone!curSalesPriceRate * !subDetail.Form.RecordsetClone!dblQtyAllocated) - (!subDetail.Form.RecordsetClone!curSalesPriceRate * !subDetail.Form.RecordsetClone!dblQtyAllocated) * (!subDetail.Form.RecordsetClone!dblDiscount / 100)) * (dblTaxCodeRate / 100)
  38.  
  39.                     dblTaxTotal = dblTaxTotal + CStr(((!subDetail.Form.RecordsetClone!curSalesPrice * !subDetail.Form.RecordsetClone!dblQtyAllocated) - (!subDetail.Form.RecordsetClone!curSalesPrice * !subDetail.Form.RecordsetClone!dblQtyAllocated) * (!subDetail.Form.RecordsetClone!dblDiscount / 100)) * (dblTaxCodeRate / 100))
  40.                     dblTaxTotalRate = dblTaxTotalRate + CStr(((!subDetail.Form.RecordsetClone!curSalesPriceRate * !subDetail.Form.RecordsetClone!dblQtyAllocated) - (!subDetail.Form.RecordsetClone!curSalesPriceRate * !subDetail.Form.RecordsetClone!dblQtyAllocated) * (!subDetail.Form.RecordsetClone!dblDiscount / 100)) * (dblTaxCodeRate / 100))
  41.                 Else
  42.                     !subDetail.Form.RecordsetClone!curSalesTax = ((!subDetail.Form.RecordsetClone!curSalesPrice * !subDetail.Form.RecordsetClone!dblQtyOrdered) - (!subDetail.Form.RecordsetClone!curSalesPrice * !subDetail.Form.RecordsetClone!dblQtyOrdered) * (!subDetail.Form.RecordsetClone!dblDiscount / 100)) * (dblTaxCodeRate / 100)
  43.                     !subDetail.Form.RecordsetClone!curSalesTaxRate = ((!subDetail.Form.RecordsetClone!curSalesPriceRate * !subDetail.Form.RecordsetClone!dblQtyOrdered) - (!subDetail.Form.RecordsetClone!curSalesPriceRate * !subDetail.Form.RecordsetClone!dblQtyOrdered) * (!subDetail.Form.RecordsetClone!dblDiscount / 100)) * (dblTaxCodeRate / 100)
  44.  
  45.                     dblTaxTotal = dblTaxTotal + CStr(((!subDetail.Form.RecordsetClone!curSalesPrice * !subDetail.Form.RecordsetClone!dblQtyOrdered) - (!subDetail.Form.RecordsetClone!curSalesPrice * !subDetail.Form.RecordsetClone!dblQtyOrdered) * (!subDetail.Form.RecordsetClone!dblDiscount / 100)) * (dblTaxCodeRate / 100))
  46.                     dblTaxTotalRate = dblTaxTotalRate + CStr(((!subDetail.Form.RecordsetClone!curSalesPriceRate * !subDetail.Form.RecordsetClone!dblQtyOrdered) - (!subDetail.Form.RecordsetClone!curSalesPriceRate * !subDetail.Form.RecordsetClone!dblQtyOrdered) * (!subDetail.Form.RecordsetClone!dblDiscount / 100)) * (dblTaxCodeRate / 100))
  47.                 End If
  48.             !subDetail.Form.RecordsetClone.Update
  49.  
  50.             If !strTransactionType = "RMA" Then
  51.                 dblSubTot = dblSubTot + cswRoundAmount2(CStr((!subDetail.Form.RecordsetClone!curSalesPrice * !subDetail.Form.RecordsetClone!dblQtyAllocated) - (!subDetail.Form.RecordsetClone!curSalesPrice * !subDetail.Form.RecordsetClone!dblQtyAllocated) * (!subDetail.Form.RecordsetClone!dblDiscount / 100)))
  52.                 dblSubTotRate = dblSubTotRate + cswRoundAmount2(CStr((!subDetail.Form.RecordsetClone!curSalesPriceRate * !subDetail.Form.RecordsetClone!dblQtyAllocated) - (!subDetail.Form.RecordsetClone!curSalesPriceRate * !subDetail.Form.RecordsetClone!dblQtyAllocated) * (!subDetail.Form.RecordsetClone!dblDiscount / 100)))
  53.             Else
  54.                 dblSubTot = dblSubTot + cswRoundAmount2(CStr((!subDetail.Form.RecordsetClone!curSalesPrice * !subDetail.Form.RecordsetClone!dblQtyOrdered) - (!subDetail.Form.RecordsetClone!curSalesPrice * !subDetail.Form.RecordsetClone!dblQtyOrdered) * (!subDetail.Form.RecordsetClone!dblDiscount / 100)))
  55.                 dblSubTotRate = dblSubTotRate + cswRoundAmount2(CStr((!subDetail.Form.RecordsetClone!curSalesPriceRate * !subDetail.Form.RecordsetClone!dblQtyOrdered) - (!subDetail.Form.RecordsetClone!curSalesPriceRate * !subDetail.Form.RecordsetClone!dblQtyOrdered) * (!subDetail.Form.RecordsetClone!dblDiscount / 100)))
  56.  
  57.             End If
  58.  
  59.         !subDetail.Form.RecordsetClone.MoveNext
  60.         Loop
  61.  
  62.         ' Calculate the totals for the Misc subform.
  63.         !subMisc.Form.RecordsetClone.FindFirst strCriteria
  64.         Do Until !subMisc.Form.RecordsetClone.EOF
  65.             !subMisc.Form.RecordsetClone.Edit
  66.                 ' Get the Labor
  67.                 dblMHRTot = dblMHRTot + (!subMisc.Form.RecordsetClone!dblManHoursRequired * !subMisc.Form.RecordsetClone!dblQtyShipped)
  68.                 dblTaxCodeRate = Nz(!subMisc.Form.RecordsetClone!dblTaxCodeRate)
  69.  
  70.                 ' Calculate the Tax
  71.                 !subMisc.Form.RecordsetClone!curSalesTax = ((!subMisc.Form.RecordsetClone!curSalesPrice * !subMisc.Form.RecordsetClone!dblQtyShipped) - (!subMisc.Form.RecordsetClone!curSalesPrice * !subMisc.Form.RecordsetClone!dblQtyShipped) * (!subMisc.Form.RecordsetClone!dblDiscount / 100)) * (dblTaxCodeRate / 100)
  72.                 !subMisc.Form.RecordsetClone!curSalesTaxRate = ((!subMisc.Form.RecordsetClone!curSalesPriceRate * !subMisc.Form.RecordsetClone!dblQtyShipped) - (!subMisc.Form.RecordsetClone!curSalesPriceRate * !subMisc.Form.RecordsetClone!dblQtyShipped) * (!subMisc.Form.RecordsetClone!dblDiscount / 100)) * (dblTaxCodeRate / 100)
  73.  
  74.                 dblTaxTotal = dblTaxTotal + (CStr((!subMisc.Form.RecordsetClone!curSalesPrice * !subMisc.Form.RecordsetClone!dblQtyShipped) - (!subMisc.Form.RecordsetClone!curSalesPrice * !subMisc.Form.RecordsetClone!dblQtyShipped) * (!subMisc.Form.RecordsetClone!dblDiscount / 100))) * (dblTaxCodeRate / 100)
  75.                 dblTaxTotalRate = dblTaxTotalRate + (CStr((!subMisc.Form.RecordsetClone!curSalesPriceRate * !subMisc.Form.RecordsetClone!dblQtyShipped) - (!subMisc.Form.RecordsetClone!curSalesPriceRate * !subMisc.Form.RecordsetClone!dblQtyShipped) * (!subMisc.Form.RecordsetClone!dblDiscount / 100))) * (dblTaxCodeRate / 100)
  76.  
  77.             !subMisc.Form.RecordsetClone.Update
  78.  
  79.             dblSubTot = dblSubTot + cswRoundAmount2(CStr((!subMisc.Form.RecordsetClone!curSalesPrice * !subMisc.Form.RecordsetClone!dblQtyShipped) - (!subMisc.Form.RecordsetClone!curSalesPrice * !subMisc.Form.RecordsetClone!dblQtyShipped) * (!subMisc.Form.RecordsetClone!dblDiscount / 100)))
  80.             dblSubTotRate = dblSubTotRate + cswRoundAmount2(CStr((!subMisc.Form.RecordsetClone!curSalesPriceRate * !subMisc.Form.RecordsetClone!dblQtyShipped) - (!subMisc.Form.RecordsetClone!curSalesPriceRate * !subMisc.Form.RecordsetClone!dblQtyShipped) * (!subMisc.Form.RecordsetClone!dblDiscount / 100)))
  81.  
  82.         !subMisc.Form.RecordsetClone.MoveNext
  83.         Loop
  84.  
  85.  
  86.         .RecordsetClone.Edit
  87.             ' Update Labor.
  88.             .RecordsetClone("curMHRTotal") = cswRoundAmount2(dblMHRTot) * .RecordsetClone!curHourly
  89.             .RecordsetClone!curMHRTax = .RecordsetClone("curMHRTotal")
  90.  
  91.             ' Update Freight Sales Tax.
  92.             If IsNull(.RecordsetClone!curFreight) Or .RecordsetClone!curFreight = "" Then
  93.                 .RecordsetClone("curFreight") = 0
  94.             End If
  95.  
  96.             ' Update Labor Sales Tax.
  97.             If IsNull(.RecordsetClone!curMHRTotal) Or .RecordsetClone!curMHRTotal = "" Then
  98.                 .RecordsetClone("curMHRTotal") = 0
  99.             End If
  100.  
  101.             ' Update the Total Sales Tax and Invoice Totals.
  102.             .RecordsetClone("curSalesTax") = cswRoundAmount2((Nz(dblTaxTotal) + (Nz(Me!curFreight) * (Nz(Me!dblTaxFreightPercent) / 100)) + (Nz(Me!curMHRTotal) * (Nz(Me!dblTaxMHRPercent) / 100))))
  103.             .RecordsetClone("curSalesTaxRate") = cswRoundAmount2((Nz(dblTaxTotalRate) + (Nz(Me!curFreight) * (Nz(Me!dblTaxFreightPercent) / 100)) + (Nz(Me!curMHRTotal) * (Nz(Me!dblTaxMHRPercent) / 100))))
  104.             .RecordsetClone("curOrderTotal") = cswRoundAmount2(dblSubTot + Nz(.RecordsetClone!curSalesTax) + .RecordsetClone!curFreight + (.RecordsetClone!curMHRTotal))
  105.             .RecordsetClone("curOrderTotalRate") = cswRoundAmount2(dblSubTotRate + (Nz(.RecordsetClone!curSalesTax) * .RecordsetClone!dblExchangeRate) + .RecordsetClone!curFreightRate + Nz(.RecordsetClone!curMHRTotal))
  106.         .RecordsetClone.Update
  107.  
  108.     End With
  109.  
  110. Exit_cswUpdateOrder:
  111.     ' Close Recordsets and destroy object variables.
  112.     Forms!frmOEOrder.RecordsetClone.Close
  113.     Forms!frmOEOrder.subDetail.Form.RecordsetClone.Close
  114.     Forms!frmOEOrder.subMisc.Form.RecordsetClone.Close
  115.     Exit Sub
  116. Error_cswUpdateOrder:
  117.     cswLogError Application.CurrentObjectName, "Error_cswUpdateOrder", Now, Err.Number, Err.Description
  118.     Resume Next
  119. End Sub
  120.  
May 11 '16 #1
5 1820
MikeTheBike
639 Expert 512MB
Hi

As far as I am aware, and according to the Access help, the RecordsetClone object is read only!?

Which may explain the apparent difference between the 'modified' Clone object and the actual data.

You could perhaps use the form's Recordset object, but the active record in the form will change as you move through the records.

However, not having a clue what you are trying to do, and looking at the code, I suspect there is a more logical way to do this.

MTB
May 11 '16 #2
jforbes
1,107 Expert 1GB
I agree with MikeTheBike, there is a more logical way to accomplish this. Most likely, putting all the calculations into a Query and using the Query as the basis of any Reporting or displaying of Totals. Someone will point out soon enough that it's not a good practice to store fields that can be calculated, and that it breaks normalization, but there are times that, even if it's a hard headed boss, that the calculated results can be saved along with the data.

So given that the calculations are to be stored, an update query still might be better as far as response and ease of development and maintenance. So you might want to consider it.

To increase the reliability of the code using RecordsetClones, I would first clone all the RecordSets into Variables and then only use the RecordSet Variables to perform the data manipulations. Mostly, because your dealing with a blackbox when it comes to when the Form and it's underlying RecordSet are updated. This is a mock-up of the code and a large portion of it has been removed, but it should give you the idea:
Expand|Select|Wrap|Line Numbers
  1. Sub cswUpdateOrder(strOrdNum As String)
  2. ...
  3.      Dim oOrder as DAO.Recordset
  4.      Dim oDetail as DAO.Recordset
  5.      Dim oMisc as DAO.Recordset
  6.  
  7.      Set oOrder = Forms!frmOEOrder.RecordsetClone
  8.      Set oDetail = Forms!frmOEOrder.subDetail.Form.RecordsetClone
  9.      Set oMisc = Forms!frmOEOrder.subMisc.Form.RecordsetClone
  10.      oDetail.MoveFirst
  11.      oMisc.MoveFirst
  12.  
  13.      With oOrder 
  14.          ' Set the Criteria for the Update.
  15.          .FindFirst "strOrderNumber = """ & strOrdNum & """"
  16.  
  17.          dblTaxTotal = 0
  18.          dblTaxTotalRate = 0
  19.          dblSubTot = 0
  20.          dblSubTotRate = 0
  21.          dblTaxCodeRate = 0
  22.          dblMHRTot = 0
  23.  
  24.          ' Update the !subDetail.Form.RecordsetClone Sales tax fields.
  25.          strCriteria = "strOrderNumber = """ & oOrder!strOrderNumber & """"
  26.          oDetail.FindFirst strCriteria
  27.  
  28.          Do Until oDetail.EOF
  29.              oDetail.Edit
  30.                  ' Get the Labor
  31.                  dblMHRTot = dblMHRTot + (oDetail!dblManHoursRequired * oDetail!dblQtyOrdered)
  32.                  dblTaxCodeRate = Nz(oDetail!dblTaxCodeRate)
  33. ... 
  34.             oDetail.Update
  35. ... 
  36.             oDetail.MoveNext
  37.          Loop
  38. ... 
  39.          .Edit
  40.              ' Update Labor.
  41.              !curMHRTotal = cswRoundAmount2(dblMHRTot) * !curHourly
  42. ...
  43.          .Update
  44.  
  45.      End With
  46. ...
May 11 '16 #3
alexg
4
I have read the same, that recordsetclone is read only. I didn't write this code, but what they are trying to do is write the changed values to the database. It seems like what they are doing updates on the screen but doesn't update in the database when multiple records are open. I will reconsider and rewrite this last part of the code to use the recordset instead. I think it cause a conflict as it's already being used with the query and form in use.

Thanks for your help!
May 12 '16 #4
alexg
4
I didn't write this, just trying to fix it. I agree about calculations not being saved, but they did to speed up reports and the printing of orders and invoices.
I will look at your suggestion and at least use it to do the update on the final values that need to be put back in the database. For the most part all the code currently works, except for the last few lines don't actually write to the database. I think they just update the screen view values/form that you are looking at the moment.

Thanks for your valuable input.
Alex
May 12 '16 #5
zmbd
5,501 Expert Mod 4TB
MikeTheBike hit the nail on the head.
From Microsoft Reference:
Form.RecordsetClone Property (Access2013)
You can use the RecordsetClone property to refer to a form's Recordset object specified by the form's RecordSource property. Read-only.
Jforbes has the right idea about setting the pointer for the RSClone, just as with the CurrentDB call, each time the call is directly made, it recreates the pointer and may create other issues in the long run. By setting the RSClone to the object-pointer you ensure that you are using the same object instance for each action AND when you release that instance of the object, that the memory and recordset are also released.

Finally, any changes made to the recordset that feeds the form not made via bound controls tends to not be properly reflected in the form (and subforms) until a requery is forced AND the recordsetclone most likely will not reflect any changes until it is also forced to requery.
May 12 '16 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Darren Dale | last post by:
Hi, I have a variable saved in a file like this #contents of myfile.py: testvar = and I am trying to write a function that does something like this: def myfunction(filename):
13
by: Seth Spearman | last post by:
Hey guys, I have the following code: '****************************************************** If Not Me.NewRecord Then Dim rs As DAO.Recordset Dim strBookmark As String Set rs =...
1
by: Gerry Abbott | last post by:
Hi all, I have a function I use to sort the recordset behind a form on designated fields of the form (see code below). I pass the field name, and trigger this function with the click of the...
3
by: Anthony Kroes | last post by:
I have a subform on a form and they are not linked. On the main form is a text box where the user types in a number. When that number changes, I have some code to make the corresponding text...
3
by: GGerard | last post by:
Hello I am trying to refer in code to the RecordsetClone of a subform datasheet but I'm getting syntax errors. This is what I am writing: Set MyDB = DBEngine.Workspaces(0).Databases(0)...
22
by: Br | last post by:
First issue: When using ADPs you no longer have the ability to issue a me.refresh to save the current record on a form (the me.refresh does a requery in an ADP). We usually do this before...
3
by: G Gerard | last post by:
Hello Can I write an SQL statement in code using a RecordSetClone of an open form? something like: MySQL = "UPDATE Me!Child0.Form.RecordSetClone SET Me!Child0.Form.RecordSetClone.MyField =...
10
by: WaterWalk | last post by:
*Hope this thread doesn't violate this group's rule* I found the following code in the GNOME's glib-2.12 source: /* In gclosure.c */ GClosure* g_cclosure_new (GCallback callback_func,...
10
by: d.francis | last post by:
I have converted an Access 97 database to Access 2003 The following code now fails and returns Run-time error '3420' Dim rst as DAO.recordset Set rst = Forms!frm1!frm2.Form.RecordsetClone I...
2
imrosie
by: imrosie | last post by:
Hello, I have a search form that uses a row query to locate a customer by customerID and first & last names, so it's an unbound control. I also have two events associated with this control,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.