Creating a function using IF and referencing qry field

Gary_Mc_Mahon

Board Regular
Joined
Apr 30, 2003
Messages
87
Hi,

I was wondering if anyone can help with this one....

I want to create a function that will look at a revenue period and return the revenue for that period.

E.g

If Period# = "06" then show me the reveune field "June Rev" from the query "qry_Key_Rev"

Here's as far as I've got so far...all help welcome. The reason I dotn want to do it as an IIF statement in a select query is cos i dont want to have to update the query every month.

Function FindKeyRev(Period As String) As Integer

If Period = "06" Then FindKeyRev = [qry_Key_Rev]![Jun03 Rev $]
Else
FindKeyRev = 0
End If

End Function

Cheers
Gary
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi Gary,

you can do this with a parameter query. Each time you run it, you'll be prompted to enter the revenue period. Here's how...

Open your query in Design view.
Go to the Period field (presumably it's Text if you're using 06, but whatever...)
In the Criteria row below this field (where you want the filter to operate), type
[Enter the Revenue Period]
Save and close.

Now when you run the query, a dialog pops up. Enter the period and you're away.

Denis
 
Upvote 0
Thanks Denis, but I dont want to do it that was as it will only pull out static fields, my example should have been clearer.....

If Period = "06" Then FindKeyRev = [qry_Key_Rev]![Jun03 Rev $]
Else
If Period ="07" Then FindKeyRev = [qry_Key_Rev]![Jul03 Rev $]
Else
If Period ="08" Then FindKeyRev = [qry_Key_Rev]![Aug03 Rev $]
Else
FindKeyRev = 0
End If
End If
End If

And so on......

As you can see, if the period moves the field selected out needs to move as well.

Hope this is clearer.
Gary
 
Upvote 0
Hi Gary,

do you have each month in a separate field? If you do, you'll create endless hassles for yourself. The standard database way to do it is to have 2 fields: RevenueDate and RevenueAmount. You can then filter these any way you want to pull out the relevant data -- and a crosstab query will show the revenue broken out by months.

HTH
Denis
 
Upvote 0
OK, perhaps I need to look at the solution again, its something I've been passed over.

Thanks for the help and tips.
Gary
 
Upvote 0

Forum statistics

Threads
1,221,622
Messages
6,160,887
Members
451,676
Latest member
Assy Bissy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top