Results 1 to 14 of 14
  1. #1
    breakingme10 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Oklahoma
    Posts
    153

    Many many if/then statements or case?

    We have a truck (#117) that is cheaper in labor then all our other trucks. But the price does differ for each company we bill. (Water haul for lease sites in Oil field industry)



    I broke it down like this

    Code:
    If [BillTo]=5 AND [TruckNum]=117 Then 77.00
         else
              93.50
    End If
    If [BillTo]=4 AND [TruckNum]=117 Then 65.00
         else
              80.00
    End If
    If [BillTo]=3 AND [TruckNum]=117 Then 78.00
         else
              88.00
    End If
    If [BillTo]=6 Then 90.00
    End If
    If [BillTo]=11 Then 1.00
    End If
    If [BillTo]=15 AND [TruckNum]=117 Then 80.00
    End If
    If [BillTo]=12 Then 100.00
    End If
    If [BillTo]=8 AND [TruckNum]=117 Then 80.00
         else
               90.00
    End If
    That determines the rate charges, on my report i'd like the txt box (unbound) to be autopopulated based on the criteria of the billto company and the trucknum.
    Should I do a Case statement or a switch of some sort? I'm really bad with code. Please help me out?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Well for starters you can't just say "Then 77.00" It would look more like

    Code:
    If [BillTo]=5 AND [TruckNum]=117 Then 
       Something = 77.00
    else
       Something = 93.50
    End If
    "Something" could be a variable, a form control or whatever you're trying to give that value.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    breakingme10 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Oklahoma
    Posts
    153
    no that was just an example of what my situation is, the code at the moment looks like this

    Code:
    If Me.BillTo.Value=5 AND Me.TruckNum.Value=117 Then Me.txtRate.Value =  77.00
         else
             Me.txtRate.Value =  93.50
    End If
    If Me.BillTo.Value=4 AND Me.TruckNum.Value=117 Then Me.txtRate.Value =  65.00
         else
             Me.txtRate.Value =  80.00
    End If
    If Me.BillTo.Value=3 AND Me.TruckNum.Value=117 Then Me.txtRate.Value =  78.00
         else
              Me.txtRate.Value = 88.00
    End If
    If Me.BillTo.Value=6 Then Me.txtRate.Value =  90.00
    End If
    If Me.BillTo.Value=11 Then Me.txtRate.Value =  1.00
    End If
    If Me.BillTo.Value=15 AND Me.TruckNum.Value=117 Then Me.txtRate.Value =  80.00
    End If
    If Me.BillTo.Value=12 Then Me.txtRate.Value =  100.00
    End If
    If Me.BillTo.Value=8 AND Me.TruckNum.Value=117 Then Me.txtRate.Value =  80.00
         else
              Me.txtRate.Value =  90.00
    End If
    getting an error "ELSE WITHOUT IF" tho

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    You're mixing the block and one-line formats of an If/Then. Personally I stick with the block format, but if you put the Then on the same line as the If, you don't want the End If.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    breakingme10 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Oklahoma
    Posts
    153
    I didn't know that! Thank you!

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    These values should really be put in a lookup table..(rate table). then there would be no IF ELSE statements at all.
    Your table would link to the lookup table and pull the correct rate depending on Truck#.

  8. #8
    breakingme10 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Oklahoma
    Posts
    153
    It still isn't working it is just giving me 90 for every one

  9. #9
    breakingme10 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Oklahoma
    Posts
    153
    Your right I didn't even consider that because I was using the field as part of a calculation field. But I can't think of a way to list the columns so that it would work for me?

  10. #10
    breakingme10 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Oklahoma
    Posts
    153
    |CompanyName|LaborPrice|LaborIf117| ?

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I agree about putting things in a table. If you follow the logic of your code, you'll basically always get the result of the last test. You'd want ElseIf statements rather than separate If/Then blocks. As is it runs every test and the last one wins.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    breakingme10 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Oklahoma
    Posts
    153
    as i was switching it to else if statements the code was auto correcting to this
    Code:
    Private Sub Report_Current()
    If Me.BillTo.Value = 5 And Me.TruckNum.Value = 117 Then Me.txtRate.Value = 77
    Else: If Me.BillTo.Value = 5 Then Me.txtRate.Value = 93.5
    Else: If Me.BillTo.Value = 4 And Me.TruckNum.Value = 117 Then Me.txtRate.Value = 65
    Else: If Me.BillTo.Value = 4 Then Me.txtRate.Value = 80
    Else: If Me.BillTo.Value = 3 And Me.TruckNum.Value = 117 Then Me.txtRate.Value = 78
    Else: If Me.BillTo.Value = 3 Then Me.txtRate.Value = 88
    Else: If Me.BillTo.Value = 6 Then Me.txtRate.Value = 90
    Else: If Me.BillTo.Value = 11 Then Me.txtRate.Value = 1
    Else: If Me.BillTo.Value = 15 And Me.TruckNum.Value = 117 Then Me.txtRate.Value = 80
    Else: If Me.BillTo.Value = 12 Then Me.txtRate.Value = 100
    Else: If Me.BillTo.Value = 8 And Me.TruckNum.Value = 117 Then Me.txtRate.Value = 80
    Else: If Me.BillTo.Value = 8 Then Me.txtRate.Value = 90
    End Sub
    but i still get ELSE WITHOUT IF error

  13. #13
    breakingme10 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Oklahoma
    Posts
    153
    well i fixed that problem, but now it just changes all values to whichever billto txtbox i've clicked on, my report is grouped by BillTo

  14. #14
    IslandHydro is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2013
    Location
    Whidbey Island, Washington State, USA
    Posts
    21
    Perhaps something like this:

    If TruckNum.Value = 117 then
    ' Is Truck 117
    Select Case BillTo.Value
    Case 1
    Var = 2
    Case 2
    Var = 3
    Case 3
    Var = 4
    Case else
    Var = 4
    end select
    else
    ' Isn't Truck 117
    Select Case BillTo.Value
    Case 1
    Var = 3
    Case 2
    Var = 4
    Case 3
    Var = 5
    Case else
    Var = 6
    end select
    end if

    I'm not sure what your solving for (where the result goes), in the above, I solved for Var.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Debugging a Select Case Statements
    By dccjr in forum Access
    Replies: 4
    Last Post: 03-05-2013, 04:14 PM
  2. Debugging a Select Case Statements (Still)
    By dccjr in forum Programming
    Replies: 13
    Last Post: 02-28-2013, 09:47 PM
  3. Replies: 5
    Last Post: 10-23-2012, 03:55 PM
  4. Case when
    By krufruf in forum Queries
    Replies: 2
    Last Post: 07-20-2012, 03:59 PM
  5. Case Else
    By FinChase in forum Programming
    Replies: 3
    Last Post: 02-01-2012, 10:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums