473,387 Members | 1,318 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,387 software developers and data experts.

Loop through a table and update first 10 records for each ID

I have a table in Access 2000 with these fields:

OrderID
ItemNo
DateUsed
UsageType

Customers are allowed 10 items per month for free, then the overages are billed separately, so there are multiple ItemNo for each OrderID. I need to update this table by setting the UsageType field to "pre-paid" for the first 10 records for each OrderID, then "invoice" for any overages.

How can I loop through the table and for each OrderID, mark the top 10 records one way and any that exceed the 10 count limit, mark them another way?

Thanks in advance for any help!
Aug 12 '07 #1
8 3133
ADezii
8,834 Expert 8TB
I have a table in Access 2000 with these fields:

OrderID
ItemNo
DateUsed
UsageType

Customers are allowed 10 items per month for free, then the overages are billed separately, so there are multiple ItemNo for each OrderID. I need to update this table by setting the UsageType field to "pre-paid" for the first 10 records for each OrderID, then "invoice" for any overages.

How can I loop through the table and for each OrderID, mark the top 10 records one way and any that exceed the 10 count limit, mark them another way?

Thanks in advance for any help!
  1. Before you do anything, you should read this Article:
    Database Normalization
  2. I've made certain assumptions about your Table which are listed below:
    1. Table Name: tblItems
    2. Field1 Name: ItemID [PRIMARY KEY - AUTONUMBER]
    3. Field2 Name: OrderID [LONG]
    4. Field3 Name: ItemNo [LONG]
    5. Field4 Name: DateUsed [DATE]
    6. Field5 Name: UsageType [TEXT]
  3. The following code will Update the UsageType Field to 'pre-paid' for all Items for which the count for a specific Order is 10 or below. If the number of Items are > 10, the UsageType will be updated to 'invoice' for those Items.
    Expand|Select|Wrap|Line Numbers
    1. Dim MyDB As DAO.Database, MyRS As DAO.Recordset, MyRS_2 As DAO.Recordset
    2. Dim strUniqueIDs As String, intCounter As Integer, MySQL As String
    3.  
    4. 'Generate Unique [OrderID]s
    5. strUniqueIDs = "SELECT DISTINCT OrderID FROM tblItems;"
    6.  
    7. Set MyDB = CurrentDb()
    8. Set MyRS = MyDB.OpenRecordset(strUniqueIDs, dbOpenDynaset)
    9.  
    10. MyRS.MoveFirst
    11. Do While Not MyRS.EOF
    12.   If DCount("[ItemNo]", "tblItems", "[OrderID]=" & MyRS![OrderID]) <= 10 Then
    13.     DoCmd.SetWarnings False
    14.       DoCmd.RunSQL "Update tblItems Set [UsageType]= 'pre-paid' Where [OrderID]=" & MyRS![OrderID]
    15.     DoCmd.SetWarnings True
    16.   Else     '> 10 Items for this OrderID
    17.     MySQL = "Select * From tblItems Where [OrderID] = " & MyRS![OrderID]
    18.     Set MyRS_2 = MyDB.OpenRecordset(MySQL, dbOpenDynaset)
    19.       MyRS_2.MoveLast: MyRS_2.MoveFirst
    20.       For intCounter = 1 To MyRS_2.RecordCount
    21.         If intCounter <= 10 Then    '1st 10 are pre-paid
    22.           MyRS_2.Edit
    23.             MyRS_2![UsageType] = "pre-paid"
    24.           MyRS_2.Update
    25.         Else   'overages
    26.           MyRS_2.Edit
    27.             MyRS_2![UsageType] = "invoice"
    28.           MyRS_2.Update
    29.         End If
    30.         MyRS_2.MoveNext
    31.       Next
    32.   End If
    33.   MyRS.MoveNext
    34. Loop
    35.  
    36. MyRS.Close
    37. MyRS_2.Close
Aug 12 '07 #2
Thank you so much! I'm going to try this right now. I'll read the information about normalization also, for future use, but this is a temporary app to handle an issue that will be put into our normal production stream (we have a development blackout right now) - I'm just trying to get something to work in the interim.
Aug 12 '07 #3
ADezii
8,834 Expert 8TB
Thank you so much! I'm going to try this right now. I'll read the information about normalization also, for future use, but this is a temporary app to handle an issue that will be put into our normal production stream (we have a development blackout right now) - I'm just trying to get something to work in the interim.
You are quite welcome - good luck on your project!
Aug 12 '07 #4
That worked perfectly! Thank you so much for your help.
Aug 12 '07 #5
I guess I spoke too soon. It ran beautifully during test with only 25 records in the table, but I just tried to run it on a full data set (90 records) and the UsageType field values were properly set, but I got a run time error: "Run-time error 91: Object variable or With block variable not set". Clicking "Debug" highlights this line of code: MyRS_2.Close

I tried using the help feature but I couldn't get it to work.
Aug 12 '07 #6
ADezii
8,834 Expert 8TB
I guess I spoke too soon. It ran beautifully during test with only 25 records in the table, but I just tried to run it on a full data set (90 records) and the UsageType field values were properly set, but I got a run time error: "Run-time error 91: Object variable or With block variable not set". Clicking "Debug" highlights this line of code: MyRS_2.Close

I tried using the help feature but I couldn't get it to work.
This would appear to be a Logic Bug, and would occur when no single OrderID has more than 10 Items associated with it. In this specific case, the Else Clause in Line #16 would never get executed, MyRS_2 would have never been created, but at the end of the Procedure an attempt is made to Close the Recordset (MyRS_2.Close). You can't close a Recordset that was never created in the first place. Rem Out or Delete the MyRS_2.Close line since it is not really needed, the Object Variable goes out of scope as soon as the Procedure ends anyway. Thanks for pointing out this flaw, and let me know how you make out. The number of actual Records should have no bearing on the code execution except for the time interval.
Aug 12 '07 #7
That's an interesting flaw - I never would have found it. I'm pretty much a novice - I know just enough to get myself into trouble. I did comment out the close rs line already, just to get it to execute. Now that I know that's okay I feel better about leaving it like that.

Again, thank you very much for your help. I'm embarrassed to say how many hours I spent working on this without being able to get it right. Now I can finish up this project and get back my nights & weekends. Thank you!
Aug 12 '07 #8
ADezii
8,834 Expert 8TB
That's an interesting flaw - I never would have found it. I'm pretty much a novice - I know just enough to get myself into trouble. I did comment out the close rs line already, just to get it to execute. Now that I know that's okay I feel better about leaving it like that.

Again, thank you very much for your help. I'm embarrassed to say how many hours I spent working on this without being able to get it right. Now I can finish up this project and get back my nights & weekends. Thank you!
I was just glad that I could help you in a small way, good luck!
Aug 13 '07 #9

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

Similar topics

2
by: M Wells | last post by:
Hi All, I need to change a column value in several thousand records in a table and output a list of the record ids of the records I've updated into another table. The table, however, is being...
4
by: Mark S. | last post by:
Hi, I have a weird UPDATE sequence I want to perform. The customer does not want to use INSERT and DELETE. My issue is that there are multiple Mat_Class, but I do not wish to hard code each one...
8
by: John | last post by:
I have created a purchase order database that has a PO form with PO Details in the subform. I need to assign each PO a job number and the PO details subform also has a job number field (for easy...
8
by: John Baker | last post by:
Hi: Access 2000 W98! I have a table with numerous records in it, and am attempting to delete certain records that have been selected from it. These are selected based on the ID number in a...
1
by: Kenneth | last post by:
Dear all, How can I update multiple records in using ASP.NET? Currently I have a table which require to update frequently but I can update records one by one using DataGrid. Kenneth
3
by: Slower Than You | last post by:
I am trying to write an SQL UPDATE statement for an MSAccess table and am having some problems getting my head around it. Can anyone help? TableName: CustTransactions TransactionKey AutoNumber ...
0
by: MIHAB | last post by:
Hi everyone. I have experienced a weird type of error – something obvious but I can’t get it. I am working with the ASP (VBSCRIPT) and FoxPro tables via ODBC. The goal is to update one table...
6
markrawlingson
by: markrawlingson | last post by:
Hopefully someone can help me out with this, it's driving me nuts... I have two tables - We'll call them table1 and table2. Table1 holds event information, and table2 holds user registration data...
1
by: hilal84 | last post by:
Hi, I have a problem on updating some records on sql server 2005. I can update the records in the table apart from some records. Records are added to table by a .net application. When I try to...
2
daniel aristidou
by: daniel aristidou | last post by:
Hi can anyone help me with the logic of updating records that are selected in a datagrid. Just wanna be sure before i code it all, since its first time im doing this and making sure the logic is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.