Solved

Difficulty Creating a sequential counter in Access 2007

Posted on 2009-05-07
16
1,239 Views
Last Modified: 2012-05-06
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
Comment
Question by:romieb69
  • 8
  • 6
  • 2
16 Comments
 
LVL 30

Expert Comment

by:hnasr
ID: 24329055
Check Where criteria.
Remove all the where expression. What do you get?
0
 

Author Comment

by:romieb69
ID: 24329130
Good suggestion... same thing.  I also removed all spaces from table and field names... same thing.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 24329360
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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:romieb69
ID: 24329530
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
 
LVL 30

Expert Comment

by:hnasr
ID: 24329838
If the ID starts with 1, one expects the count to be as the ID.
ID1 --- count 1
ID2 --- count 2

0
 
LVL 44

Expert Comment

by:GRayL
ID: 24330193
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
 
LVL 44

Expert Comment

by:GRayL
ID: 24330286
I do not have Office 2007 - just good old Office 2003.
0
 

Author Comment

by:romieb69
ID: 24330412
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
 
LVL 44

Expert Comment

by:GRayL
ID: 24330792
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
 
LVL 44

Accepted Solution

by:
GRayL earned 125 total points
ID: 24330813
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
 

Author Comment

by:romieb69
ID: 24330876
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
 
LVL 44

Expert Comment

by:GRayL
ID: 24330989
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
 

Author Comment

by:romieb69
ID: 24331084
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
 
LVL 44

Expert Comment

by:GRayL
ID: 24344280
Anything yet?
0
 

Author Comment

by:romieb69
ID: 24356510
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
 
LVL 44

Expert Comment

by:GRayL
ID: 24357384
Then it looks like you gave me a B because of a MS bug ;-)
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question