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!
8 3133
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!
- Before you do anything, you should read this Article:
Database Normalization
- I've made certain assumptions about your Table which are listed below:
- Table Name: tblItems
- Field1 Name: ItemID [PRIMARY KEY - AUTONUMBER]
- Field2 Name: OrderID [LONG]
- Field3 Name: ItemNo [LONG]
- Field4 Name: DateUsed [DATE]
- Field5 Name: UsageType [TEXT]
- 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.
- Dim MyDB As DAO.Database, MyRS As DAO.Recordset, MyRS_2 As DAO.Recordset
-
Dim strUniqueIDs As String, intCounter As Integer, MySQL As String
-
-
'Generate Unique [OrderID]s
-
strUniqueIDs = "SELECT DISTINCT OrderID FROM tblItems;"
-
-
Set MyDB = CurrentDb()
-
Set MyRS = MyDB.OpenRecordset(strUniqueIDs, dbOpenDynaset)
-
-
MyRS.MoveFirst
-
Do While Not MyRS.EOF
-
If DCount("[ItemNo]", "tblItems", "[OrderID]=" & MyRS![OrderID]) <= 10 Then
-
DoCmd.SetWarnings False
-
DoCmd.RunSQL "Update tblItems Set [UsageType]= 'pre-paid' Where [OrderID]=" & MyRS![OrderID]
-
DoCmd.SetWarnings True
-
Else '> 10 Items for this OrderID
-
MySQL = "Select * From tblItems Where [OrderID] = " & MyRS![OrderID]
-
Set MyRS_2 = MyDB.OpenRecordset(MySQL, dbOpenDynaset)
-
MyRS_2.MoveLast: MyRS_2.MoveFirst
-
For intCounter = 1 To MyRS_2.RecordCount
-
If intCounter <= 10 Then '1st 10 are pre-paid
-
MyRS_2.Edit
-
MyRS_2![UsageType] = "pre-paid"
-
MyRS_2.Update
-
Else 'overages
-
MyRS_2.Edit
-
MyRS_2![UsageType] = "invoice"
-
MyRS_2.Update
-
End If
-
MyRS_2.MoveNext
-
Next
-
End If
-
MyRS.MoveNext
-
Loop
-
-
MyRS.Close
-
MyRS_2.Close
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.
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!
That worked perfectly! Thank you so much for your help.
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.
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.
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!
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!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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
|
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 ...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |