473,434 Members | 1,631 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,434 software developers and data experts.

Query/Report - Calculate using If statements

I have a simple query, which consists of a few project dates, which in return need to be charged based on completion.

That said, I wanted to create a CHARGE field which would calculate the total charge based on the projects completed (Estimates & Layouts). Just to make this complicated, there are 2 branches that get discounted charges. Below, I have included the if statements that I desire to take affect, I'm not sure how to implement them into calculating my field. That is, I have no idea where to include these if statements for my report.

If [Branch] = "580" Then

[Charge] = "0"
If Month([Estimate Completed]) = Month(Now()) Then
[Charge] = [Charge] + 40
End If

ElseIf [Branch] = "585" Then

[Charge] = "0"
If Month([Estimate Completed]) = Month(Now()) Then
[Charge] = [Charge] + 40
End If

Else
[Charge] = "0"
If Month([Estimate Completed]) = Month(Now()) Then
[Charge] = [Charge] + 100
End If
If Month([Layout Completed]) = Month(Now()) Then
[Charge] = [Charge] + 100
End If
End If

Can somebody please help me with this? Thanks!
Aug 29 '07 #1
2 3413
JConsulting
603 Expert 512MB
I have a simple query, which consists of a few project dates, which in return need to be charged based on completion.

That said, I wanted to create a CHARGE field which would calculate the total charge based on the projects completed (Estimates & Layouts). Just to make this complicated, there are 2 branches that get discounted charges. Below, I have included the if statements that I desire to take affect, I'm not sure how to implement them into calculating my field. That is, I have no idea where to include these if statements for my report.

If [Branch] = "580" Then

[Charge] = "0"
If Month([Estimate Completed]) = Month(Now()) Then
[Charge] = [Charge] + 40
End If

ElseIf [Branch] = "585" Then

[Charge] = "0"
If Month([Estimate Completed]) = Month(Now()) Then
[Charge] = [Charge] + 40
End If

Else
[Charge] = "0"
If Month([Estimate Completed]) = Month(Now()) Then
[Charge] = [Charge] + 100
End If
If Month([Layout Completed]) = Month(Now()) Then
[Charge] = [Charge] + 100
End If
End If

Can somebody please help me with this? Thanks!

The IIF statement would look like this

Expand|Select|Wrap|Line Numbers
  1. MyCharge:IIF(Month([Estimate Completed]) = Month(Now()) AND ([Branch] = "580" OR [Branch]="585"), [Charge] = [Charge] + 40, IIF(Month([Estimate Completed]) <> Month(Now()) AND ([Branch] = "580" OR [Branch]="585"), [Charge] = 0,IIF(Month([Estimate Completed]) = Month(Now()) AND ([Branch] <> "580" OR [Branch]<>"585"),[Charge] = [Charge] + 100,[Charge] = 0)))
  2.  
You can put it into a query grid. If you have null values in these fields, it's going to cause you problems.
J
Aug 29 '07 #2
Jim Doherty
897 Expert 512MB
I have a simple query, which consists of a few project dates, which in return need to be charged based on completion.

That said, I wanted to create a CHARGE field which would calculate the total charge based on the projects completed (Estimates & Layouts). Just to make this complicated, there are 2 branches that get discounted charges. Below, I have included the if statements that I desire to take affect, I'm not sure how to implement them into calculating my field. That is, I have no idea where to include these if statements for my report.

If [Branch] = "580" Then

[Charge] = "0"
If Month([Estimate Completed]) = Month(Now()) Then
[Charge] = [Charge] + 40
End If

ElseIf [Branch] = "585" Then

[Charge] = "0"
If Month([Estimate Completed]) = Month(Now()) Then
[Charge] = [Charge] + 40
End If

Else
[Charge] = "0"
If Month([Estimate Completed]) = Month(Now()) Then
[Charge] = [Charge] + 100
End If
If Month([Layout Completed]) = Month(Now()) Then
[Charge] = [Charge] + 100
End If
End If

Can somebody please help me with this? Thanks!

Look like two folks been working on this at the same time. JConsulting is right blank values do cause headaches

Heres a FUNCTION in VBA for you to try out and test against your data
Paste it into a new or existing vba module

You place a call to the function by adding the function to the query Grid as a field like this

ChargeBand: charge([Branch],[Estimate Complete],[Estimate Layout])

If you have branch,Estimate Complete and Estimate Layout in the query as columns then the above sybtax line will automatically use those columns

It relies on those values being passed to the function. The code checks for zero length strings and date testing in other words NO values as JConsulting rightly said. This function is new and specific to you, so needs thorough TESTING to ensure it returns the calculations you want. I have tried it this end and it reproduces ok but I don't know your data soooo usual disclaimers apply

Interestingly enough you if you intend to reproduce anything in a table I am assuming you are not bothered about historical values ie what about when a fixed data value of [Estimate Complete] becomes three months old any calculations as of NOW will drop off if it is in a query if you don't have an anchor? If you are using the current calculation to UPDATE a fixed value then fair enough but watch out for any unexpected results in that area

Good luck with your project

Regards

Jim


Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Function charge(ByRef strBranch, ByRef dtEstComplete, ByRef dtLayoutComplete) As Long
  4. On Error GoTo Err:
  5. 'declare some variables
  6. Dim prmok As Boolean
  7. 'initialise the charge value and flag variable
  8. charge = 0
  9. prmok = False
  10. 'make sure we have parameters passed in to work with if not exit out
  11. If IsNull(strBranch) Or strBranch = "" Then
  12.     charge = 1
  13. Exit Function
  14. End If
  15. 'we got here so we must have a branch code
  16. 'if either one or the other date variable parameters has a value set a flag to true
  17. If IsDate(dtEstComplete) Or IsDate(dtLayoutComplete) Then prmok = True
  18.     'and proceed to work the logic
  19.     If prmok Then   '<<<means if value is true then do something
  20.      'first side of the IF must be 585 0r 580
  21.             If strBranch = "580" Or strBranch = "585" Then
  22.                     'which if either are then return a charge value
  23.                     'based on whether the estimate date OR the layout date has something in it
  24.                 If Month(dtEstComplete) = Month(Now()) Or Month(dtLayoutComplete) = Month(Now()) Then
  25.                    charge = charge + 40
  26.                 End If
  27.             Else
  28.                 'so its any other branch then at this point
  29.                 'who wll be charged the higher rate if either or estimate/layout
  30.                 'dates have something in them
  31.                 If Month(dtEstComplete) = Month(Now()) Or Month(dtLayoutComplete) = Month(Now()) Then
  32.                    charge = charge + 100
  33.                 Else
  34.                 ' we must return something if there is an unforeseen value
  35.                 ' like a PREVIOUS month ie: prior to the current month OR a FUTURE date
  36.                 ' here so return zero
  37.                    charge = 0
  38.                 End If
  39.             End If
  40.     Else
  41.       'prm flag shows false problem with dates? shouldnt be at this point zero out anyway if so
  42. Err:
  43.     charge = 0
  44.     End If
  45.  
  46. End Function
Aug 30 '07 #3

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

Similar topics

22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
1
by: ravi | last post by:
I have created the following interest to calculate the interest for the following currency pairs. I have tried to combine them in macros using conditions but the next query that is run in the macro...
1
by: Simon Bailey | last post by:
I currently have a query calculating the gap in days between two dates. The fields being "DateLookedAt" and "DateResolved" plus the calculated field "TimeTaken". I am looking to add another...
13
by: John young | last post by:
I have been looking for an answer to a problem and have found this group and hope you can assist . I have been re doing a data base I have made for a car club I am with and have been trying to...
1
by: Chasing Gates | last post by:
I have created a database that brings in a new table weekly. I then made a separate query for each sales rep and a separate report for each rep. (The reports are all identical but call different...
2
by: carl.barrett | last post by:
Hi, I'm back with the same question as I still can't get it to display my data the way I want it to. The table lists information about a perpetrator involved with an anti social behaviour...
5
by: Wired Hosting News | last post by:
I tried to be breif and give a scenario so as not to be overlooked because it was soooo long. Let me give you real world. I am a manufacturer of goods and produce 11 items that are distributed...
6
by: Phil Stanton | last post by:
I am running a query that calls a function used to format addresses depending on the width of a control on a report that shows that address. The same query is used as the RecordSource of lots of...
3
by: jambonjamasb | last post by:
Hi I have two tables: email_tbl Data_table Data table is is used to create a Form Data_form
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
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,...
1
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...
0
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.