Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple coding giving me a fit

Status
Not open for further replies.

gearhead03

Technical User
Mar 29, 2003
147
US
I am trying to set up an IF statement. I have done this type thing before but for some reason I just can't seems to get everything to work correctly.

The fields are:
Miles - Numeric
Weight - Numeric
Rate - Currency
RateType - Text

The first Condition is:
RateType = "Flat" then BaseRate = Rate

The second Condition is:
RateType = "Cwt" then BaseRate = (Weight*.100) * Rate

The Third Condition is:
RateType = "P/Mile" then BaseRate = Miles * Rate

with a final else to set to 0 if none of the previous conditions are met.

I want to use this to set a field on a form to display the "baserate". Would also like to have a module to use throughout the database. (queries, forms, etc.). I must just be having a bad day. I just kept getting errors when I tried to set it up. Mostly Syntax items I am sure.

Mark A. Kale
 
Code:
Public Function BaseRate(RateType As String, Rate As Currency, Weight As Single, Miles As Long) As Currency

  If RateType = "Flat" Then
    BaseRate = Rate
  ElseIf RateType = "Cwt" Then
    BaseRate = (Weight*.1)*Rate
  ElseIf RateType = "P/Mile" Then
    BaseRate = Miles * Rate
  Else
    BaseRate = 0
  End If
End Function


 
And onother version using the Select Case statement
Code:
Public Function BaseRate(RateType As String, Rate As Currency, Weight As Single, Miles As Long) As Currency

Select Case RateType
  Case Is "Flat"
    BaseRate = Rate
  Case Is  "Cwt" 
    BaseRate = (Weight*.1)*Rate
  Case Is "P/Mile" 
    BaseRate = Miles * Rate
  Case Else
    BaseRate = 0
End Select

End Function
 
how are ya gearhead03 . . .

. . . yet another method:
Code:
[blue]Public Function BaseRate(RateType As String, _
                         Rate As Currency, _
                         Weight As Single, _
                         Miles As Long) As Currency

  BaseRate = Switch([RateType] = "Flat", [Rate], _
                    [RateType] = "Cwt", ([Weight] * 0.1) * [Rate], _
                    [RateType] = "P/Mile", [Miles] * [Rate], _
                    0 = 0, 0)
End Function[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 

Do we have a Choose Function here, from somebody else?

Going once!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top