Connecting Tech Pros Worldwide Forums | Help | Site Map

Multiple if statements

Newbie
 
Join Date: Oct 2007
Posts: 28
#1: Oct 4 '07
Hello,

I'm new to the world of VBA & Access (2000), so I really don't know that much. I am trying to setup a VBA multiple if statement for a new field and would really appreciate some help and pointers in the right direction.

Essentially, the statement is designed to look up the payment frequency entered, then divide and multiply the expense field accordingly.

Is this the best way of going about it?


If [frequency] = "1 week" then
([expense] / 7 ) * 30.4
Elseif [frequency] = "2 weeks" then
([expense] / 14 ) * 30.4
Elseif [frequency] = "3 weeks" then
([expense] / 21 ) * 30.4
Elseif [frequency] = "4 weeks" then
([expense] / 28 ) * 30.4
Elseif [frequency] = "1 month" then
[expense]
Elseif [frequency] = "2 months" then
[expense] / 2
Elseif [frequency] = "3 months" then
[expense] / 3
Elseif [frequency] = "6 months" then
[expense] / 6
Elseif [frequency] = "12 months" then
[expense] / 12
Elseif [frequency] = "24 months" then
[expense] / 24
End If

Thankyou!

Expert
 
Join Date: Jun 2007
Location: Derbyshire, UK
Posts: 347
#2: Oct 4 '07

re: Multiple if statements


Quote:

Originally Posted by dozingquinn

Hello,

I'm new to the world of VBA & Access (2000), so I really don't know that much. I am trying to setup a VBA multiple if statement for a new field and would really appreciate some help and pointers in the right direction.

Essentially, the statement is designed to look up the payment frequency entered, then divide and multiply the expense field accordingly.

Is this the best way of going about it?


If [frequency] = "1 week" then
([expense] / 7 ) * 30.4
Elseif [frequency] = "2 weeks" then
([expense] / 14 ) * 30.4
Elseif [frequency] = "3 weeks" then
([expense] / 21 ) * 30.4
Elseif [frequency] = "4 weeks" then
([expense] / 28 ) * 30.4
Elseif [frequency] = "1 month" then
[expense]
Elseif [frequency] = "2 months" then
[expense] / 2
Elseif [frequency] = "3 months" then
[expense] / 3
Elseif [frequency] = "6 months" then
[expense] / 6
Elseif [frequency] = "12 months" then
[expense] / 12
Elseif [frequency] = "24 months" then
[expense] / 24
End If

Thankyou!

Hi

That would do what you want (I assume), but I tend to use Select Case constuct for this type of thing.

However, as I do not know what/where precisely you are using this, but I am guessing that the suggestion below is valid!!

It may be better to create a lookup table with fields like this

Expand|Select|Wrap|Line Numbers
  1.  ID       Frequency        Factor
  2. 1          1 Week           0.2303
  3. 2          2 Weeks          0.4605
  4. 3          3 Weeks          0.6908
  5. 4          4 Weeks          0.9210
  6. 5          1 Month            1
  7. 6          2 Months           2
  8. etc
You can the use this to enter the frequency ID into the table with the expenses using as combo control.

Then you can calculate the monthly expenses for each expense directly by dividing the expeses by the Factor in a query for that record as they are returned ?

Does that make any sense?

This could be total tosh, but I assume the Frequency (now the ID!) is entered somewhere so the factor relating to it will always be known/available with this method.

??

MTB
Newbie
 
Join Date: Oct 2007
Posts: 28
#3: Oct 4 '07

re: Multiple if statements


Quote:

Originally Posted by MikeTheBike

Hi


It may be better to create a lookup table with fields like this

Expand|Select|Wrap|Line Numbers
  1.  ID       Frequency        Factor
  2. 1          1 Week           0.2303
  3. 2          2 Weeks          0.4605
  4. 3          3 Weeks          0.6908
  5. 4          4 Weeks          0.9210
  6. 5          1 Month            1
  7. 6          2 Months           2
  8. etc
You can the use this to enter the frequency ID into the table with the expenses using as combo control.

Then you can calculate the monthly expenses for each expense directly by dividing the expeses by the Factor in a query for that record as they are returned ?

Does that make any sense?

Thanks for your help Mike - you came up with the solution I'm after - I just wasn't clever enough to determine the factor for the lookup table.
Reply