473,399 Members | 4,177 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,399 software developers and data experts.

Creating a query from a continuous form

109 100+
Hello!

I am having a bit of trouble trying to figure out how to create a query based on data retrieved from a continuous form. The table it is based on looks like this:

Table = tbl_Appointment
Expand|Select|Wrap|Line Numbers
  1. Field;Type;IndexInfo
  2. App_ID; Autonumber; PK  
  3. App_Per_ID; Number 
  4. App_Date; Date/Time 
  5. App_Outcome_ID; Number 
  6. App_Comments; Text 
  7.  
What I want to do is run a query to return the last appointment and its outcome from each individual client (App_Per_ID is the client ID).
I've tried using the Last and Max functions but it doesn't do what I need.
For example, a client could have appointment information that looks like the following:
Expand|Select|Wrap|Line Numbers
  1. App_ID    App_Per_ID    App_Date    App_Outcome_ID    App_Comments
  2. 1              2       02/10/2009         2
  3. 2              2       08/10/2009         2
  4. 3              2       19/10/2009         1
  5.  
If I run a query to select the last appointment, outcome and date then it will return the following:
Expand|Select|Wrap|Line Numbers
  1. App_Per_ID    App_Date    App_Outcome_ID    
  2. 2           08/10/2009         2
  3. 2           19/10/2009         1
  4.  
I can see why it is doing it - it's selecting the last result for each different outcome but I'm at a complete loss at how to make it return ONLY the last appointment regardless of how many it has had before (so basically, in the example above I would like it to just return the bottom record...)

Is this possible? I'm probably missing something glaringly obvious

Thanks in advance!
Feb 15 '10 #1

✓ answered by nico5038

You would need a subquery like:
Expand|Select|Wrap|Line Numbers
  1. select App_Per_ID, App_Date, App_Outcome_ID
  2. from tbl_Appointment app
  3. WHERE app.App_Date = (select Max(app1.App_Date) from tbl_Appointment app1 where app1.App_Per_ID = app.App_Per_ID)
There's still one minor "problem", as when there are two appointments on the max date, two will be returned...
This could be solved by adding a unique (autonumber with increment + 1) index and extracting in such a case the highest autonumber.

Nic;o)

12 3705
ADezii
8,834 Expert 8TB
The following SQL Statement will list the latest [App_Date] for a given [App_Per_ID] as well as the Number of [App_Outcome_ID]s for the [App_Per_ID]:
Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_Appointment.App_Per_ID, Max(tbl_Appointment.App_Date) AS [Maximum App_Date], 
  2. Count(tbl_Appointment.App_Outcome_ID) AS [Outcome IDs]
  3. FROM tbl_Appointment
  4. GROUP BY tbl_Appointment.App_Per_ID;
  5.  
OUTCOME:
Expand|Select|Wrap|Line Numbers
  1. App_Per_ID    Maximum App_Date    Outcome IDs
  2.      2           10/19/2009            3
  3.  
Feb 16 '10 #2
hedges98
109 100+
That's not quite what I need, sorry if I wasn't clear. I need to display just the last appointment and its outcome for each client only. From these results I was going to do a count of last outcomes. Although after staring at it for the best part of a day, I'm thinking it isn't possible.

Thanks for your efforts though ADezii!
Feb 16 '10 #3
ADezii
8,834 Expert 8TB
I'm pretty sure that I can arrive at a code-based solution if you are interested, just let me know.
Feb 16 '10 #4
hedges98
109 100+
Yes please! I am definitely interested, total hero!
Feb 16 '10 #5
ADezii
8,834 Expert 8TB
Just give me a little time, since it won't be that simple. In the meantime I'm going to ask a couple of Experts who are more proficient than I if there is, in fact, an SQL based solution.
Feb 16 '10 #6
hedges98
109 100+
Take all the time you need man, I'm thankful enough that you're helping me anyway.
Feb 16 '10 #7
nico5038
3,080 Expert 2GB
You would need a subquery like:
Expand|Select|Wrap|Line Numbers
  1. select App_Per_ID, App_Date, App_Outcome_ID
  2. from tbl_Appointment app
  3. WHERE app.App_Date = (select Max(app1.App_Date) from tbl_Appointment app1 where app1.App_Per_ID = app.App_Per_ID)
There's still one minor "problem", as when there are two appointments on the max date, two will be returned...
This could be solved by adding a unique (autonumber with increment + 1) index and extracting in such a case the highest autonumber.

Nic;o)
Feb 16 '10 #8
hedges98
109 100+
Thank you nico5038, that's absolutely perfect.

There is only one appointment per day so that problem is a total non-issue. My brain can take a bit of a rest now.

Thanks to ADezii for the input as well!
Feb 16 '10 #9
ADezii
8,834 Expert 8TB
Thanks Nico, since my head was hurting from banging it against the wall. I arrived at a code-based solution, but it is considerably more complex! (LOL)
Feb 16 '10 #10
hedges98
109 100+
Out of interest, could you post the code-based solution? It will probably pass completely over my head but I wouldn't mind trying to look over it and understand it!
Feb 16 '10 #11
ADezii
8,834 Expert 8TB
Here is the code-based solution along with the Test Database:
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim strUnique As String
  3. Dim intAppOutcomeID As Integer
  4. Dim dteLatestDateForPerID As Date
  5. Dim rstResults As DAO.Recordset             'To populate tblResults
  6. Dim rstUniqueAppPerID As DAO.Recordset      'Unique [App_Per_ID]s
  7. Dim intResponse As Integer
  8.  
  9. strUnique = "SELECT DISTINCT [App_Per_ID] FROM tbl_Appointment ORDER BY [App_Per_ID];"
  10.  
  11. 'Remove any pre-existing Records from the Results Table (tblResults)
  12. CurrentDb.Execute "DELETE * FROM tblResults", dbFailOnError
  13.  
  14. Set MyDB = CurrentDb
  15. Set rstUniqueAppPerID = MyDB.OpenRecordset(strUnique, dbOpenForwardOnly)
  16. Set rstResults = MyDB.OpenRecordset("tblResults", dbOpenDynaset)
  17.  
  18. With rstUniqueAppPerID
  19.   Do While Not .EOF
  20.     'Retrieve the Latest Date for the given [App_Per_ID]
  21.     dteLatestDateForPerID = DMax("[App_Date]", "tbl_Appointment", "[App_Per_ID] = " & ![App_Per_ID])
  22.  
  23.     'Retrieve the [App_Outcome_ID] for the given [App_Per_ID] and [App_Date]
  24.     intAppOutcomeID = DLookup("[App_Outcome_ID]", "tbl_Appointment", "[App_Per_ID] = " & ![App_Per_ID] & _
  25.                               " And [App_Date] = #" & dteLatestDateForPerID & "#")
  26.       rstResults.AddNew
  27.         rstResults![App_Per_ID] = ![App_Per_ID]
  28.         rstResults![Latest_Date] = dteLatestDateForPerID
  29.         rstResults![Outcome_ID] = intAppOutcomeID
  30.       rstResults.Update
  31.     'MsgBox ![App_Per_ID] & " ==> " & dteLatestDateForPerID & " ==> " & intAppOutcomeID
  32.       .MoveNext
  33.   Loop
  34. End With
  35.  
  36. rstResults.Close
  37. rstUniqueAppPerID.Close
  38. Set rstUniqueAppPerID = Nothing
  39. Set rstResults = Nothing
  40.  
  41. intResponse = MsgBox("Open Results Table?", vbYesNo + vbQuestion + vbDefaultButton1, "Results Table")
  42. If intResponse = vbYes Then
  43.   DoCmd.OpenTable "tblResults", acViewNormal, acReadOnly
  44.     DoCmd.Maximize
  45. End If
Attached Files
File Type: zip Continuous.zip (18.6 KB, 102 views)
Feb 16 '10 #12
hedges98
109 100+
Thanks ADezii, I sort of understand how you came to that solution although I'd never have got there myself as I've never done anything with Recordsets and so on.
Feb 17 '10 #13

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

Similar topics

2
by: Iain Miller | last post by:
Now this shouldn't be hard but I've been struggling on the best way as to how to do this one for a day or 3 so I thought I'd ask the assembled company..... I'm writing an application that tracks...
1
by: Giulio | last post by:
Hello, I have a continuous form with a combo box inside. From the combo box I can pick some values which, by the "after-update" event, change some other combo box values determined by a query....
15
by: Rolan | last post by:
There must be a way to enhance the performance of a query, or find a plausible workaround, but I seem to be hitting a wall. I have tried a few tweaks, however, there has been no improvement. ...
5
by: keith | last post by:
This may seem simple, but I'm having a bit of trouble figuring out exactly how to do it. I'm accessing a database through an ODBC link, and I have a query that returns only jobs completed that day...
11
by: Andy_Khosravi | last post by:
My problem: I'm having trouble with a query taking much too long to run; a query without any criteria evaluating only 650 records takes over 300 seconds to run (over the network. On local drive...
3
by: S P Arif Sahari Wibowo | last post by:
Hi! I would like to make an editable continous form, where most fields will be from table A and editable, except 1-3 fields are a glimpse into table B and uneditable. Table A relate to table B...
1
by: John T Ingato | last post by:
With excel VBA, I can do : Dim ctrGo as commandbutton Set ctrGo = me.controls.add("Go") which would create a button on the form. Can I do with with Access? When I try it, first of all the...
6
by: dufnobles via AccessMonster.com | last post by:
I have a form that excepts text input from a user. I would like to have the form display the same line several times so that the user can enter several different responses on one form screen. ...
46
by: OldBirdman | last post by:
What a mess this question is. I have spent 2 weeks trying to make it concise and clear, and I can't. I do not have the vocabulary for the question. I cannot even TITLE it correctly. Here is my...
6
by: jmarcrum | last post by:
Hi! I have created a Union Query in ACCESS 2003, that combines 130 records from one query (Extra Foreman Radios) and 250 records from another query (Forman Main Radios). I have created a...
0
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
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
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...
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
Oralloy
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,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.