Results 1 to 10 of 10
  1. #1
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544

    New to Case statements

    Hi Everyone!



    I admit this is my first attempt at writing case statements, and I am not sure I have my head around it. I am trying to calculate various measurements; ie: square feet, cubic feet, linear feet, cubic feet, cubic yards. Is my statement correct? My numbers are not coming out right at all!
    Thank you in advance.

    Code:
    Private Sub Height_AfterUpdate()
        On Error GoTo errHandler
    
        Select Case MeasurementType
    
    Case "cuyd"
    TotalMeasurement = Length * Width * Height / 27
    'TotalMeasurement = Int(-100 * [TotalMeasurement]) / -100
    
    Case "lf"
    TotalMeasurement = Length * 1
    'TotalMeasurement = Int(-100 * [TotalMeasurement]) / -100
    
    Case "sqft"
    TotalMeasurement = Length * Width
    'TotalMeasurement = Int(-100 * [TotalMeasurement]) / -100
    
    Case "cuft"
    TotalMeasurement = Length * Width * Height
    'TotalMeasurement = Int(-100 * [TotalMeasurement]) / -100
    
    
    Case "sqyd"
    TotalMeasurement = Length * Width / 9
    'TotalMeasurement = Int(-100 * [TotalMeasurement]) / -100
    
    
    
    End Select
    
    Dim numvalue As Double
    numvalue = [TotalMeasurement]
    If (numvalue - Int(numvalue)) = 0 Then
    Exit Sub
    Else
    numvalue = Int(numvalue) + 1
    'MsgBox numvalue ' display the new value
    
    '[Value1] = numvalue
    
    
    End If
    
    Exit Sub
    
    errHandler:
        MsgBox "Error " & Err.Number & ": " & Err.Description & " in " & _
               VBE.ActiveCodePane.CodeModule, vbOKOnly, "Error"
    
    End Sub

  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
    The structure seems correct. Is TotalMeasurement a variable or textbox? What values are in the various fields and what is the result?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Quote Originally Posted by pbaldy View Post
    The structure seems correct. Is TotalMeasurement a variable or textbox? What values are in the various fields and what is the result?
    TotalMeasurement is a textbox. So, my measurement = cuft, and I enter 10 (L) 10(W) 10(h) and get 1401000. UGH.

  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
    Can you attach the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Ok, I finally got it down to a size I could attach. Thanks for taking the time!!
    Attached Files Attached Files

  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
    How do I recreate the problem? Offhand, it looks like the data types of the fields are text. I'd use a numeric data type if you want to do numeric math on them.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Yes, I thought that too, however when I changed them to number, I got the same answer. Go into Contacts and Bids and in the tab control go to bid details.

  8. #8
    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 should have thought of this. I think your problem is reserved words. If you step through the code, Width is the wacky number, and I think it's because it's picking up the width property of the form rather than your field. You can work around it using bang:

    TotalMeasurement = Me![Length] * Me![Width] * Me![Height]

    But personally I'd change the field names (both width and height, since both are reserved words). I'd still use numeric data types.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    I've changed the field names etc....that is so weird, I've been using length x width forever to calculate square feet with no problem. But when I decided to add cubic measurements and case statements, Access lost its mind!

    Thank you again Paul - you always manage to save my butt.

  10. #10
    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! One problem with reserved words is that sometimes Access will make the correct assumption, so you may not realize the problem until later.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 1
    Last Post: 03-06-2015, 11:16 AM
  2. Replies: 3
    Last Post: 10-27-2014, 07:37 AM
  3. Many many if/then statements or case?
    By breakingme10 in forum Programming
    Replies: 13
    Last Post: 07-11-2014, 09:11 AM
  4. Debugging a Select Case Statements
    By dccjr in forum Access
    Replies: 4
    Last Post: 03-05-2013, 04:14 PM
  5. Debugging a Select Case Statements (Still)
    By dccjr in forum Programming
    Replies: 13
    Last Post: 02-28-2013, 09:47 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