473,387 Members | 1,423 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.

How can I update an MS Access table with a sequential number based on a record date?

I have a table in which I record referral records. I have a second table in which I record service records. One referral can have many service records attached. The tables are linked by referral ID - the service table incudes the referal ID field. A representation of the service table follows:

Col1 Col2 Col3 Col4
1122 160 01/01/2001 1
1256 160 01/10/2001 2
1899 160 01/06/2004 3
1999 160 02/07/2005 4
2150 160 05/08/2006 5
1200 100 1/08/1999 1
1466 100 1/07/2003 2

Where:

Col1 = ServiceRecordID (AutoNum)
Col2 = ReferralD
Col3 = ServiceStartDate
Col4 = AuditServiceRecordCount

Each time a new service record is created it is assigned a sequencial 'record count' (1,2,3,4,5 . . . Etc). This allows me to identify the order each service relating to a specific referral occurred.

The database allows for service records to be deleted (using a button connected to a delete query). What I need the database to do is automatically update the record counter when a deltion takes place. . . So if record 3 or 5 is deleted, the database automatically updates record 4 to 3 and record 5 to 4.

I have looked at some other answers on in this forum and have come up with these two queries. . .

Query # 1
Expand|Select|Wrap|Line Numbers
  1. SELECT ServiceTable.ServiceRecordID, ServiceTable_1.ServiceRecordID, ServiceTable_1.ReferralID, ServiceTable_1.ServiceStartDate, ServiceTable_1.ServiceEndDate, ServiceTable_1.AuditServiceRecordCount
  2. FROM (ReferralTable INNER JOIN ServiceTable ON ReferralTable.ReferralID = ServiceTable.ReferralID) INNER JOIN ServiceTable AS ServiceTable_1 ON ReferralTable.ReferralID = ServiceTable_1.ReferralID
  3. WHERE (((ServiceTable.ServiceRecordID)=[Forms]![DeleteRecordServiceForm]![ServiceRecordID]))
  4. ORDER BY ServiceTable_1.ServiceStartDate DESC;
  5.  
Query # 2

Expand|Select|Wrap|Line Numbers
  1. UPDATE DeleteServiceRecordCountUpdateQuery1 SET DeleteServiceRecordCountUpdateQuery1.AuditServiceRecordCount = Dcount(1,"DeleteServiceRecordCountUpdateQuery1","ServiceTable_1.ServiceRecordID<>[Forms]![DeleteRecordServiceForm]![ServiceRecordID] And ServiceStartDate <= [ServiceStartDate]")
  2. WHERE (((DeleteServiceRecordCountUpdateQuery1.ServiceTable_1.ServiceRecordID)<>[Forms]![DeleteRecordServiceForm]![ServiceRecordID]));
  3.  
Query 1 pulls out all service records that are attached to the same referral as the service being deleted

Query 2 updates all service records from query 1, except for the one being deleted . . . And should assign a sequential number to each of the other. However it doesn’t work. It just assigns a dcount value (i.e if two records will be left, both will be given a count of 2 rather than 1 and 2 based on date value).

Can anyone help? I ideally want to use update queries to do this . . . I think the problem may be with the dcount element in query 2.

Many thanks!!
Nov 18 '10 #1
2 5170
NeoPa
32,556 Expert Mod 16PB
Assuming you have a form ([DeleteRecordServiceForm]) which has controls reflecting both the [ReferralID] and the [AuditServiceRecordCount] of the record that has been deleted (for now we will assume they have the same name as the fields they represent, except with "txt" prepended), then after the record has been deleted, and in the same Event Procedure, code similar to the following could be run, which will execute the simple SQL to do the job :

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2.  
  3. 'Code to delete the record
  4.  
  5. strSQL = "UPDATE [ServiceTable] " & _
  6.          "SET    [AuditServiceRecordCount] = [AuditServiceRecordCount] - 1 " & _
  7.          "WHERE  (([ReferralID] = " & Me.txtReferralID & ")" & _
  8.          "  AND   ([AuditServiceRecordCount] > " & Me.txtAuditServiceRecordCount & "))"
  9. Call DoCmd.SetWarnings(False)
  10. Call DoCmd.RunSQL(strSQL)
  11. Call DoCmd.SetWarnings(False)
Nov 18 '10 #2
NeoPa
32,556 Expert Mod 16PB
I assume Simon Penny is a pseudonym of James Jones as this exact same question was posted earlier and answered. As you are now posting from a registered account I deleted the anonymous one and moved my answer across to here.

Please ensure you don't double-post any questions in future.
Nov 18 '10 #3

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

Similar topics

3
by: Ken Bush | last post by:
How can I write an update query that removes part of a field? Like if I have a field with values such as 8/3/68 (a birthday obviously) and I need to put values in a new column but I need...
2
by: SenseForAll | last post by:
First please note I am a novice at VBA and not even that experienced with DAO/ADO and MS-SQL. Any assistance is appreciated. That said... I have an application written in Access w/ VBA. I need to...
7
by: GAVO. | last post by:
Hello every one I have a database with a form called "frmOrders" on that for I need to create a sequential number for each city apart from the original autonumber. So the table "tblorders" would...
0
by: EKL | last post by:
Hi, I'm making a sort of Customer and Orders database in MS Access 2003. My problem is that I wish to update the table "tblTransaction" based on changes made in the table "tblOrderDetails"....
1
by: Pierre Maricq | last post by:
Hi, I am using Win2000 and Access2000. I need to build build a macro or write a VBA in Access that would screen all files contained in a directory on my C drive (files are structrured DAT...
1
by: john_liu | last post by:
How can I update an Access table based on a sheet in Excel by VBA in excel. Thanks
1
by: ET | last post by:
Please help with the query: There are two tables, A and B. A table has information about cell phones, like cell number, sim number, model, manufacturer etc... B table has user related...
9
by: Nooby | last post by:
New to Access here. I inherited a db that has the first column as an automatically generated sequential number. I want to bump it up, but for the life of me I can't figure out how to do it. Is...
1
by: Esmi | last post by:
I am developing an application in Ms Excel 2003 that has a feature to update records in Ms Access. The problem is the table name I am trying to update contains a space and whenever I run the below...
8
by: MOCaseA | last post by:
Greetings (again)... This should be a simple matter but I cannot seem to find any information on how to do/build this. Inside a single DB I have two tables... One is titled Employee Submissions...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.