Solved

Serialize using Subquery

Posted on 2010-09-14
7
622 Views
Last Modified: 2013-11-28
Hi experts,

Up till now I've been using Lebans Serialize function to add sequential numbers to my query.
For a particular query it was very slow, so I decided to take a different path. I use a subquery, & in the form it works fine...much quicker than the serialize function.
The trouble is when I try and view the query Access blows up/Closes down...no message.
Any ideas why it should work in a form, but not able to view it.
I can also view it in design mode.

Attached please find the query..It's based on many others..I can't post them all.

I'm using Access 2003 SP3 + latest hotfixes

Gary
SELECT o1.ContactID, (SELECT COUNT(contactid) FROM QryPreSubTotalFacility AS o2 WHERE o2.DueDate <=O1.DueDate) AS InstNo, o1.DueDate

FROM QryPreSubTotalFacility AS o1;

Open in new window

0
Comment
Question by:tbsgadi
  • 4
  • 2
7 Comments
 
LVL 46

Author Comment

by:tbsgadi
ID: 33672033
BTW the same happens in Access 2007
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 33672688
1. convert the query QryPreSubTotalFacility to a make table query (this is just a test)
   * run a query with the count(*) subquery using the created table, see if you still get the problem

2. can you post the SQL of query QryPreSubTotalFacility
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 33679624
Here is a very fast method using a Collection.
The in-line comments explain typical usage.

/gustav
Public Function RowCounter( _

  ByVal strKey As String, _

  ByVal booReset As Boolean, _

  Optional ByVal strGroupKey As String) _

  As Long

  

' Builds consecutive RowIDs in select, append or create query

' with the possibility of automatic reset.

' Optionally a grouping key can be passed to reset the row count

' for every group key.

'

' Usage (typical select query):

'   SELECT RowCounter(CStr([ID]),False) AS RowID, *

'   FROM tblSomeTable

'   WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True));

'

' The Where statement resets the counter when the query is run

' and is needed for browsing a select query.

'

' Usage (typical append query, manual reset):

' 1. Reset counter manually:

'   Call RowCounter(vbNullString, False)

' 2. Run query:

'   INSERT INTO tblTemp ( RowID )

'   SELECT RowCounter(CStr([ID]),False) AS RowID, *

'   FROM tblSomeTable;

'

' Usage (typical append query, automatic reset):

'   INSERT INTO tblTemp ( RowID )

'   SELECT RowCounter(CStr([ID]),False) AS RowID, *

'   FROM tblSomeTable

'   WHERE (RowCounter("",True)=0);

'

' 2002-04-13. Cactus Data ApS. CPH

' 2002-09-09. Str() sometimes fails. Replaced with CStr().

' 2005-10-21. Str(col.Count + 1) reduced to col.Count + 1.

' 2008-02-27. Optional group parameter added.



  Static col      As New Collection

  Static strGroup As String

  

  On Error GoTo Err_RowCounter

  

  If booReset = True Or strGroup <> strGroupKey Then

    Set col = Nothing

    strGroup = strGroupKey

  Else

    col.Add col.Count + 1, strKey

  End If

  

  RowCounter = col(strKey)

  

Exit_RowCounter:

  Exit Function

  

Err_RowCounter:

  Select Case Err

    Case 457

      ' Key is present.

      Resume Next

    Case Else

      ' Some other error.

      Resume Exit_RowCounter

  End Select



End Function

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 46

Author Comment

by:tbsgadi
ID: 33680081
Cap I tried the temp Table & of course it worked fine.
The question still remains why does a query work within a form but doesn't display?
0
 
LVL 46

Author Closing Comment

by:tbsgadi
ID: 33680092
I've come across this code before, but never tried it,assuming (wrongly) that it wouldn't be any quicker than Serialize.
Thanks!
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 33680254
You are welcome!

/gustav
0
 
LVL 46

Author Comment

by:tbsgadi
ID: 33691324
FYI I've managed to make the original query work..it needed to have ORDER BY o1.DueDate ;
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now