473,809 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculated Expressions in a Query

109 New Member
The thread title probably isn't very clear but I can't think how to word my problem properly. Here goes:

I have a form that has textboxes for users to enter information for first appointments and then a subform (continuous) for them to enter subsequent appointments into (Date and Outcome boxes). After 12 weeks and 6 months (from the first appointment) an appointment is arranged to check progress of the client.

What I need to do is produce a query that will show me when these dates are (12 weeks and 6 months onwards). These will then go into a report so the user can select a month from a drop down box on a form and it will display all clients that have either a 12 week or 6 month update appointment in the selected month.

Now, this sounds fairly straightforward but I have ran into some problems... which are as follows:

There are the odd occasions when the client will not attend the first appointment for a number of reasons - these are selected from the Outcome drop down box (cancelled, rescheduled, uncontactable etc). Now obviously, it wouldn't make much sense to arrange a 12 week/6 month update appointment from this date for obvious reasons.
I have attempted to run some IIf expressions in the query to only add the 12 week/6 month dates if the first appointment outcome = attended which worked. And then if the outcome didn't = attended then it would take the date from the subsequent form and work out the 12 week/6 month appointment dates from there.
I think this worked but because some clients have several subsequent appointments, the query displays several fields for each individual client when it only needs to display the client name and 12 week/6 month dates. My query felt like it contained way too many calculated expressions than were necessary and after playing around with it for most of today I've drawn a blank.

I'm aware this probably makes little sense but I've tried to explain this as best I could. The database I'm working on was created by someone else and I have been drafted in to try and improve it in a number of ways so I'm working with quite a tempermental beast so to speak.

Any help would be massively appreciated as I'm completely stuck really! I've probably forgot to mention something so if anymore information is needed then I'll try and provide it!

Cheers
Oct 20 '09 #1
11 1689
NeoPa
32,579 Recognized Expert Moderator MVP
This will work much better if you can post the meta-data (info about the layout / structure) of the table in the same way as I use in my example. Click on the Reply button and you will have access to all the codes I've used. PK & FK stand for Primary Key & Foreign Key respectively. Never use TABs in this as the layout gets mucked up. Use spaces and all is fine.
Table Name=[tblStudent]
Expand|Select|Wrap|Line Numbers
  1. Field           Type      IndexInfo
  2. StudentID       AutoNumber    PK
  3. Family          String        FK
  4. Name            String
  5. University      String        FK
  6. Mark            Numeric
  7. LastAttendance  Date/Time
Oct 20 '09 #2
hedges98
109 New Member
Okay, here is the information for the table Appointments. However, the query I am using requires info from 2 further tables (Person and Practitioner), let me know if you need the info from those too...

Table Name=[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. App_FirstDate     Date/Time
  8. App_FirstComments Text
  9. App_First_Outcome_ID Number
Oct 21 '09 #3
hedges98
109 New Member
Hmm, just after I posted that, I figured out that I could query the current query which (I think) has solved my problem...

You can tell this is the first time I'ved used Access since 6th form!

EDIT: Actually, I think I may need some help with my calculated expressions, I currently have this (and the same in the 12 week column although, obviously, the 6 month parts are replaced with 12 week parts!):

Expand|Select|Wrap|Line Numbers
  1. 6MonthCalc: IIf(([Per_Closure_Status_ID]<> 2 Or 5),IIf(([App_First_Outcome_ID]=1),DateAdd("m",6,[App_FirstDate]),IIf(([App_First_Outcome_ID]<>1),DateAdd("m",6,[App_Date]))))
Basically, if either 'Closed' (2) or 'Never Seen' (5) are selected from the Closure Status drop down then I don't want the 12 week/6 month milestones to be calculated. However, using this it still calculates values if 'Never Seen; (5) is selected. Should I be using something other than 'Or'? It appears to be working fine other than that.

Sorry if this makes no sense!
Oct 21 '09 #4
hedges98
109 New Member
Right, sorry for the continuous posting (I should probably refrain from posting whilst I'm toying around with Access!) but I think I've sussed it although it's a bit of a botch job.
I altered the code above to something less complicated and more readable:
Expand|Select|Wrap|Line Numbers
  1.  IIf(([Per_Closure_Status_ID]<>2),DateAdd("m",6,[App_FirstDate])))
And then added Per_Closure_Sta tus_ID to the query and in the Criteria part set it to <> 5 so the query wouldn't display the 'Not Seen' statuses. It appears to be working but I'm sure there is a more professional way than this though.
Oct 21 '09 #5
NeoPa
32,579 Recognized Expert Moderator MVP
Hedges, you're doing fine.

Let me just see if I can post a few tips related to some of what you've posted, then you can tell mewhere you want to go from there. Whether you want help with the original question or you're happier proceeding by batting ideas backwards and forwards.

BTW. Nice job on the Meta-Data. So many posters fail to make a decent job of that. Well done.
Oct 21 '09 #6
NeoPa
32,579 Recognized Expert Moderator MVP
@hedges98
You can query other queries of course, but you can also use SubQueries (See Subqueries in SQL) within your query. Generally using existing queries makes more sense when they are needed for other purposes too in their own right, or when you already have them available.
@hedges98
should be written as :
Expand|Select|Wrap|Line Numbers
  1. IIf([Per_Closure_Status_ID] In(2,5),DateAdd('m',6,IIf([App_First_Outcome_ID]=1,[App_FirstDate],[App_Date])),Null)
At least that's the best I can do without a better understanding of what you're trying to do. It may be that your [Per_Closure_Sta tus_ID] test should be in a WHERE clause as a filter instead.
Oct 21 '09 #7
NeoPa
32,579 Recognized Expert Moderator MVP
@hedges98
It looks as if you got pretty close all by yourself.

How about posting the full SQL and I'll see if I can make some better sense of it. I'll comment on anything I see.
Oct 21 '09 #8
hedges98
109 New Member
NeoPa - That's all awesome help and incredibly useful for the future, thanks very much. I went back through and added the 'In(2,5)' bit in place of what I had so it's tidier and more practical.

I've managed to produce the report that was required now but I imagine I'll be back on this forum in the near future for help with more report-y stuff!

As an aside - should I rename the thread (if that's possible on these forums?!) to something that makes more sense to what this thread is about?

Thanks again
Oct 22 '09 #9
NeoPa
32,579 Recognized Expert Moderator MVP
@hedges98
We'll look forward to it. Welcome to Bytes!
@hedges98
Generally an appropriate title helps both you and those coming after you, but yours isn't too bad already. If you'd like to though, simply post or PM me the new title & I'll do it for you. Only moderators can update posts once the post-editing delay is up.
Oct 22 '09 #10

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

Similar topics

7
11446
by: mark | last post by:
Access 2000: I creating a report that has a record source built by the user who selects the WHERE values. An example is: SELECT * FROM CHARGELOG WHERE STDATE Between #10/27/2003# And #11/2/2003# And VehicleID='00000000' And BattID='LKO500HF'. I need to use the records returned to populate text boxes, but the data requires further manipulation. I attempting to use expressions in the control source
1
5392
by: Miguelito Bain | last post by:
hi everybody. i'm trying to find out what you can and can't do with/in calculated fields. any help, pointers, and/or advice would be greatly appreciated. i'm a newbie, but i want to learn, and i've tried a lot before posting. here's my situation. i have a simple form with dates on it. i have an indate and outdate
5
4060
by: John Bahran | last post by:
I am trying to use calculated fields in my query but all the results are zero ven when they're not. Please help. Thanks.
1
2347
by: Norbert Lieckfeldt | last post by:
MS Access 2002 here. I am just trying to set up a simple database for a friend who's an optician. Basically, all clients with address details, date of last eyetest and a drop-down combo box to choose a number of months to the next eyetest (3,6,12,24). I then have a calculated field in the form which works out the date of the next appointment on the basis of the previous two sets of information . All that works . I am now trying to set...
10
1440
by: MLH | last post by:
MyLocPhone: Format$(,"(@@@) @@@-@@@@") The above expression is a field in a select query. On one XP box (the dev box, of course). It works fine. On two other XP boxen - it fails saying "Function isn't available in expressions in query expression ..." ==> citing the MyLocPhone: Format$(... as the problem. The query is evaluated on-click of a command button. If it returns records, there are things to be done. I have installed the A97...
2
6013
by: Unforgiven | last post by:
Hello All, I have the following query... SELECT Demographics.Full_Name, Demographics.Year_of_birth, Status.Status_OK, SUM(2004 - Demographics.Year_of_birth) AS Age FROM Demographics INNER JOIN Status ON Demographics.PrimaryKey = Status.PrimaryKey
5
5890
by: Henrik | last post by:
The problem is (using MS Access 2003) I am unable to retrieve long strings (255 chars) from calculated fields through a recordset. The data takes the trip in three phases: 1. A custom public function returns a long string. This works. 2. A query has a calculated field based on the custom function above. This works when the query is run directly.
2
10760
by: Jim.Mueksch | last post by:
I am having a problem with using calculated values in a WHERE clause. My query is below. DB2 gives me this error message: Error: SQL0206N "APPRAISAL_LESS_PRICE" is not valid in the context where it is used. SQLSTATE=42703 SELECT DISTINCT S3.OPR_APPLICATION_NR, S3.APPLICATION_ID, S3.APPRAISAL_TYPE_CD, S3.Appraisal_Used_Amount, S3.RPT_LEVEL2_NR,
3
6020
by: myemail.an | last post by:
I use Access 2007 and create queries with a number of calculated fields/expressions (I'm still a novice so please forgive me if my wording is imprecise...), like: MyCalculation = Field1 - Field2. However, I noticed that I cannot query the database setting a condition on this newly created field (eg <0); if I do, when I run the query , Access shows me an input box asking for the value of MyCalculation. The only solution I managed to find...
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10379
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7660
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.