473,480 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Calculated Column in Subform Problem

mshmyob
904 Recognized Expert Contributor
Going to post this real fast since I have to go out, so I hope I get all the pertenent info in the message.

I have a subform and the results are based on a select query. Results are say like so (just a snippet of the results)

Quarter Value Calculated
4 200 25
3 175 -5
2 180 30
1 150 0

Assume the first 2 columns are derived from my select query.
The 3rd column is the one I need to figure out. It is a formula and just assume I need to subtract the value of Qtr4 from Qtr3 for row 1, subtract Qtr3 from Qtr2 for row 2, etc.

I am drawing a blank how I can accomplish this on the subform. Any direction would be appreciated.

cheers,
Oct 9 '08 #1
19 2367
ADezii
8,834 Recognized Expert Expert
You can create a Calculated Field within the Query comprising the Record Source for the Sub-Form. This Calculated Field would call a Public Function and pass to it the Values in the [Quarter] and [Value] Fields for each Record. Logic would be contained within the Function and it would return an Integer Value representing the difference between the Quarters. I had something like this in mind:
  1. Calculated Field in Query
    Expand|Select|Wrap|Line Numbers
    1. Difference:fCalcFormula([Quarter], [Value])
  2. Function definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcFormula(bytQuarter As Byte, intValue As Integer) As Integer
    2.   ...logic here
    3. End Function

P.S. - You stated that this was a snippet of code, which confused me, since there are only 4 Quarters in a Year. Are multiple Years involved? This wouold change the code logic. When I have time, I'll actually work on a simple template for you.
Oct 10 '08 #2
mshmyob
904 Recognized Expert Contributor
Yes multiple years. I will look at this over the weekend.

Thanks Ade.

cheers,

You can create a Calculated Field within the Query comprising the Record Source for the Sub-Form. This Calculated Field would call a Public Function and pass to it the Values in the [Quarter] and [Value] Fields for each Record. Logic would be contained within the Function and it would return an Integer Value representing the difference between the Quarters. I had something like this in mind:
  1. Calculated Field in Query
    Expand|Select|Wrap|Line Numbers
    1. Difference:fCalcFormula([Quarter], [Value])
  2. Function definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcFormula(bytQuarter As Byte, intValue As Integer) As Integer
    2.   ...logic here
    3. End Function

P.S. - You stated that this was a snippet of code, which confused me, since there are only 4 Quarters in a Year. Are multiple Years involved? This wouold change the code logic.
Oct 10 '08 #3
ADezii
8,834 Recognized Expert Expert
Yes multiple years. I will look at this over the weekend.

Thanks Ade.

cheers,
  1. tblMshmyob
    Expand|Select|Wrap|Line Numbers
    1. Quarter    Value
    2. 1           150
    3. 2           180
    4. 3           175
    5. 4           200
  2. qryMshmyob
    Expand|Select|Wrap|Line Numbers
    1. SELECT tblMshmyob.Quarter, tblMshmyob.Value, fCalcDiff([Quarter],[Value]) AS Difference
    2. FROM tblMshmyob
    3. ORDER BY tblMshmyob.Quarter DESC;
  3. Function Definition
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcDiff(bytQuarter As Byte, intValue As Integer) As Integer
    2. Dim intValPrevQuar As Integer
    3.  
    4. If bytQuarter = 1 Then
    5.   fCalcDiff = 0
    6. Else
    7.   'Calculate the Value for the previous Quarter
    8.   intValPrevQuar = DLookup("[Value]", "tblMshMyob", "[Quarter] = " & bytQuarter - 1)
    9.   Select Case bytQuarter
    10.     Case 2, 3, 4
    11.       fCalcDiff = [intValue] - intValPrevQuar
    12.     End Select
    13. End If
    14. End Function
  4. OUTPUT
    Expand|Select|Wrap|Line Numbers
    1. Quarter    Value    Difference
    2. 4           200         25
    3. 3           175         -5
    4. 2           180         30
    5. 1           150          0
  5. P.S. - Multiple Years are not taken into consideration.
Oct 10 '08 #4
mshmyob
904 Recognized Expert Contributor
Thanks Ade. This looks like it will work for me. There is more to it than years and quarters and the formula is also more complicated than subtracting but I am sure with your suggestion I can make it all work, since I have done it outside of the subform :). I was just drawing a blank on getting it to work inside the subform. Your idea looks like it will work. The fact that you used a global function is perfect since I am using a global function now for the calculation outside the subform.

I let you know after the weekend.

cheers,

  1. tblMshmyob
    Expand|Select|Wrap|Line Numbers
    1. Quarter    Value
    2. 1           150
    3. 2           180
    4. 3           175
    5. 4           200
  2. qryMshmyob
    Expand|Select|Wrap|Line Numbers
    1. SELECT tblMshmyob.Quarter, tblMshmyob.Value, fCalcDiff([Quarter],[Value]) AS Difference
    2. FROM tblMshmyob
    3. ORDER BY tblMshmyob.Quarter DESC;
  3. Function Definition
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcDiff(bytQuarter As Byte, intValue As Integer) As Integer
    2. Dim intValPrevQuar As Integer
    3.  
    4. If bytQuarter = 1 Then
    5.   fCalcDiff = 0
    6. Else
    7.   'Calculate the Value for the previous Quarter
    8.   intValPrevQuar = DLookup("[Value]", "tblMshMyob", "[Quarter] = " & bytQuarter - 1)
    9.   Select Case bytQuarter
    10.     Case 2, 3, 4
    11.       fCalcDiff = [intValue] - intValPrevQuar
    12.     End Select
    13. End If
    14. End Function
  4. OUTPUT
    Expand|Select|Wrap|Line Numbers
    1. Quarter    Value    Difference
    2. 4           200         25
    3. 3           175         -5
    4. 2           180         30
    5. 1           150          0
  5. P.S. - Multiple Years are not taken into consideration.
Oct 10 '08 #5
NeoPa
32,556 Recognized Expert Moderator MVP
Thanks Ade.
Just to clarify - you may have seen references to Ade in threads with ADezii involved. That's generally a reference to me (Abbreviation of Adrian in real life as ADezii knows).

I can't say too much about another member's personal details, but I do know that Ade is not an abbreviation for ADezii.
Oct 12 '08 #6
ADezii
8,834 Recognized Expert Expert
Just to clarify - you may have seen references to Ade in threads with ADezii involved. That's generally a reference to me (Abbreviation of Adrian in real life as ADezii knows).

I can't say too much about another member's personal details, but I do know that Ade is not an abbreviation for ADezii.
You mean I'm not Ade!
Oct 12 '08 #7
NeoPa
32,556 Recognized Expert Moderator MVP
You KNOW I can never be as uncomplicated as that ADezii!
Oct 13 '08 #8
mshmyob
904 Recognized Expert Contributor
Sorry Ade and sorry ADe.

cheers,
Oct 13 '08 #9
ADezii
8,834 Recognized Expert Expert
Tou KNOW I can never be as uncomplicated as that ADezii!
I was just laying back and waiting to see how long it would take you to catch on! (LOL).
Oct 13 '08 #10
ADezii
8,834 Recognized Expert Expert
Sorry Ade and sorry ADe.

cheers,
No problem, mshmyob, NeoPa and I are known by many names, not all of them flattering! (LOL).
Oct 13 '08 #11
NeoPa
32,556 Recognized Expert Moderator MVP
I was just laying back and waiting to see how long it would take you to catch on! (LOL).
Of course there should have been quotes - but it only took a second scan to get the gist of what you were trying to say. Almost 5 seconds then :D
Oct 13 '08 #12
mshmyob
904 Recognized Expert Contributor
Finally got back to this problem. Is it possible to reference a recordset inside a module? I have tried the following (and variations) with no luck.

Expand|Select|Wrap|Line Numbers
  1. Set rst = Forms!frmContactInfo!RORDetailsubform.Form.RecordsetClone
  2.  
I have tried it outside the module and it works. Inside a module called by the function you indicated in the select query I get error # 7951 Expression has invalid reference to recodersetclone property.

If I can get a recordset based on my select query it will make my calculation easier.

cheers,
Oct 16 '08 #13
mshmyob
904 Recognized Expert Contributor
OK I modified my query like so

Expand|Select|Wrap|Line Numbers
  1. SELECT tblRORDetail.RORYear, tblRORDetail.RORQtr, tblRORDetail.RORQtrMarket, tblRORDetail.RORQTRCredits, tblAccountType.AccountTypeName, tblRORDetail.RORQTRDebits, fCalcRORQtr(RORYear,RORQtr,RORQtrMarket,RORQtrCredits,RORQTRDebits) AS RORPeriod FROM tblAccountType INNER JOIN tblRORDetail ON tblAccountType.AccountTypeID=tblRORDetail.AccountTypeID WHERE tblRORDetail.ContactID=forms!frmContactInfo.txtClientID AND tblAccountType.AccountTypeID=forms!frmContactInfo.RORDetailsubform.form.cboSelectAccountType ORDER BY tblRORDetail.RORYear DESC , tblRORDetail.RORQtr DESC , tblAccountType.AccountTypeName; 
  2.  
What I notice is that it seems to call the function only twice. Once on the first row of the subform and on the last row of the subform. Any row in between does not get called.

The function also only gets called when the value of cboSelectAccountType is = 1 (which is the first accounttype in the table tblAccountType). Any other value the function never gets called.

I will keep looking at it to see where I messed up but if you spot anything please let me know.

cheers,
Oct 16 '08 #14
FishVal
2,653 Recognized Expert Specialist
Obviously, seeing fCalcRORQtr() function code would help spot anything. :)
Oct 16 '08 #15
mshmyob
904 Recognized Expert Contributor
Maybe :)

I figured something out though. If any value such as RORCredits or RORDebits for instance is null then the query actually doesn't seem to call the function. If I put in values of 0 or greater for these 2 fields everything works as it should.

Therefore I will just set my constraints for the fields to be >= 0.

cheers,

Obviously, seeing fCalcRORQtr() function code would help spot anything. :)
Oct 16 '08 #16
FishVal
2,653 Recognized Expert Specialist
Maybe :)

I figured something out though. If any value such as RORCredits or RORDebits for instance is null then the query actually doesn't seem to call the function. If I put in values of 0 or greater for these 2 fields everything works as it should.

Therefore I will just set my constraints for the fields to be >= 0.

cheers,
IMHO, its a bit overkill to set table constraint for the sake of single peace of code not loving Nulls. :)
Did you try to declare function arguments as Variant and handle Nulls inside function logic?
Oct 16 '08 #17
mshmyob
904 Recognized Expert Contributor
Good point Fish. I also do not like settings those types of constraints.

In my module function I changed the dblCredit and dblDebit variables from double to variant and it works without the contraints.

Thanks for the help guys.

cheers,

IMHO, its a bit overkill to set table constraint for the sake of single peace of code not loving Nulls. :)
Did you try to declare function arguments as Variant and handle Nulls inside function logic?
Oct 16 '08 #18
FishVal
2,653 Recognized Expert Specialist
Not a problem.
Good luck.
Oct 16 '08 #19
ADezii
8,834 Recognized Expert Expert
Finally got back to this problem. Is it possible to reference a recordset inside a module? I have tried the following (and variations) with no luck.

Expand|Select|Wrap|Line Numbers
  1. Set rst = Forms!frmContactInfo!RORDetailsubform.Form.RecordsetClone
  2.  
I have tried it outside the module and it works. Inside a module called by the function you indicated in the select query I get error # 7951 Expression has invalid reference to recodersetclone property.

If I can get a recordset based on my select query it will make my calculation easier.

cheers,
The code itself, assigning RecordsetClone to the Object Variable rst is sound ,as long as rst is declared with the proper Scope.
Oct 16 '08 #20

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

Similar topics

14
3803
by: Allen Browne | last post by:
Subform is based on a single-table query that contains a calculated field: Amount: Round(CCur(Nz(*,0)),2) Continuous subform displays this field in a text box named Amount. As user enters new...
2
1982
by: Phil Stanton | last post by:
I have a form of club members with a continuous subform on it showing boat details. I hold the boat length and beam (in meters) in a table. On the subform I also calculate and show the imperial...
0
1515
by: Del | last post by:
Thanks in advance for any help. I have a database that was created in Access 2000. Several users have been upgraded to Access 2003. Since upgrading to 2003 we have noticed that one of the...
1
2952
by: tconway | last post by:
I have an Access program that displays Customer data into a form and OrderID data into a subform. The totals in the Subform are based on calculated fields, i.e. the Total Amount field Calculates...
3
2959
by: jburris | last post by:
I have been through enough of these threads to think that this should be an easy fix... but, are there circumstances in which the following code syntax does not work? =!!.Form! (this is out of...
6
5868
by: dhowell | last post by:
I have a "form" and "subform" where I would like a calculated control on the form which sums the values of a datasheet column of the subform. (datasheet on subform may have a variable number of...
15
2632
by: Jimmy Stewart | last post by:
I want to use a calculated function for the caption on my tab controls. I used the following code: Me.Page28.Caption = "Expenses & "]" This should display the following: " Expenses " where...
17
18295
by: MariaTorvalds | last post by:
I have a problem where if I use a calculated control, for example =Date()) on my form, PLUS I have a subform on a tab control on the same form, then when I refresh the record (by menu command or VBA...
5
1769
by: royals | last post by:
Hello Access gurus, I am a self-taught Access “developer” and have a ton to learn so please allow me to sound stupid. I am using 2003 on a XP platform. I manage properties for an...
0
7041
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
7043
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
7081
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6737
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...
1
4776
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...
0
4481
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...
0
2995
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...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
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 ...

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.