Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1245
  • Last Modified:

Difficulty Creating a sequential counter in Access 2007

Hey guys...
I've created a query that I need to put a sequential counter into.  I'm using the DCount function against an autonumber primary key field (ID).  For some reason or another in this particular query my counter is returning the same number as in my ID field. Any suggestions would be greatly appreciated!!

SELECT DCount("ID","[Room Bed Table]","ID<="& ID) AS [Counter], [Room Bed Table].ID, [Room Bed Table].[Facility ID], [Room Bed Table].[Facility Description], [Room Bed Table].[Unit ID], [Room Bed Table].[Room ID], [Room Bed Table].[Room Description], [Room Bed Table].[Location Comment], [Room Bed Table].Pic1, [Room Bed Table].Pic2, [Room Bed Table].Pic3
FROM [Room Bed Table]
WHERE ((([Room Bed Table].[Facility Description])="Voorhees") AND (([Room Bed Table].[Unit ID])="PICU")) OR ((([Room Bed Table].[Facility Description])="Voorhees") AND (([Room Bed Table].[Unit ID])="PCU")) OR ((([Room Bed Table].[Facility Description])="Voorhees") AND (([Room Bed Table].[Unit ID])="ICU"))
ORDER BY [Room Bed Table].ID;
0
romieb69
Asked:
romieb69
  • 8
  • 6
  • 2
1 Solution
 
hnasrCommented:
Check Where criteria.
Remove all the where expression. What do you get?
0
 
romieb69Author Commented:
Good suggestion... same thing.  I also removed all spaces from table and field names... same thing.
0
 
GRayLCommented:
I know this will work:

SELECT (SELECT Count(b.ID) FROM [Room Bed Table] b WHERE b.ID<= a.ID) AS [Counter],
a.ID, a.[Facility ID], a].[Facility Description], a.[Unit ID], a.[Room ID], a.[Room Description], a.[Location Comment], a.Pic1, a.Pic2, a.Pic3
FROM [Room Bed Table] a
WHERE (((a.[Facility Description])="Voorhees") AND ((a.[Unit ID])="PICU")) OR (((a.[Facility Description]) = "Voorhees") AND ((a.[Unit ID]) = "PCU")) OR (((a.[Facility Description]) = "Voorhees") AND ((a.[Unit ID]) = "ICU"))
ORDER BY a.ID;
0
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
romieb69Author Commented:
Yep that sure as heck looks exactly like it would work... but I'm getting the EXACT same results... did a copy and paste of your code exactly as it appears.  I'm going to export the query results (only 30 recs) and attach.


RoomBedVoorhees.xlsx
0
 
hnasrCommented:
If the ID starts with 1, one expects the count to be as the ID.
ID1 --- count 1
ID2 --- count 2

0
 
GRayLCommented:
I was assuming that there were 'holes' in the ID as the result of deletions and subsequent additions in which the deleted ID are never used again.  If that is not the case and the ID sequence is intact, then Count will equal ID.
0
 
GRayLCommented:
I do not have Office 2007 - just good old Office 2003.
0
 
romieb69Author Commented:
Thanks to everyone for the input.  The actual auto number field (ID) does not contain any holes in the main table. The returned results from my query though do leave holes in the sequence of numbers. My goal here was to basically 're-create' a new id field that would 'start over' at 1 based on the number of records returned from a query.  I have some additional VB code that needs to begin at '1' thru whatever (with no holes in sequence) for each data set it looks at to generate reports.  So if I get 30 records from my query and the id number is 220 - 260 (obviously a hole in sequence in this example) I want my counter field to literally count 1-30 ....  

My approach may be off here and I may very well need to rethink how to tackle this problem.
0
 
GRayLCommented:
But the query I gave you will created the 1-30 count, so long as the ID increased record by record, regardless of 'holes'.
0
 
GRayLCommented:
That is to say each record must have an ID, but there may be skips,  ie, 1,4,5,6,11,12,13, etc.  
0
 
romieb69Author Commented:
Gray ... yeah this is really weird.  I've taken queries I've written ... as well as your query.... copied the structure of the table I'm working with... added some nonsense records ... deleted a couple of records and then ran both my query and your query and it works exactly like I'd expect.  I get back into my main query that's pointed at my table and then I get unexpected results.  Make sure you examine the attachment I provided so you get an illustration of this.  I'm giving you the points because I know good and well your query works.
0
 
GRayLCommented:
Do a Compact and Repair.  If that does not work, open a new mdb and copy all the objects from the old db to the new one.  Let me know if that resolves it.  I do not like B's.
0
 
romieb69Author Commented:
Already did the compact and repair. I'll isolate this table, the report, my code and my query into a new database and see what happens. I'll have to get on this in the morning though as I have to get on a production issue.  Appreciate all the suggestions and insight.
0
 
GRayLCommented:
Anything yet?
0
 
romieb69Author Commented:
Same thing Gray ... pretty weird man... I set up a single database with one table, one report and this one query and got the same results. I then tried to isolate the records by hospital (facility) and created 4 databases .... same thing. What I don't understand is if I just add 10 or so records to the same table structure with nonsense data ... delete a few ... add a few more.. the counter works perfect. Your code works fine.. and my original code works fine. I'm actually headed out to LA to start a new contract using MSMQ. I'm not going to mess with this any longer. This was all a work around to get around a report export bug with Access 2007 concerning the new 'Attachment' fields. Problem is related to a memory issue. Some 'picture attachments' show up in the export and some don't. Microsoft will have a patch for the issue in about a month. At this point it's easier to use the 'temp fix code' that Microsoft provided for the export problems (exports one file per page) and then grep those reports for specific things the client needs vs using a query. Not pretty but it works for now until MS fixes the original bug.
0
 
GRayLCommented:
Then it looks like you gave me a B because of a MS bug ;-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 8
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now