Results 1 to 15 of 15
  1. #1
    Dalagrath is offline Only a Man
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2010
    Location
    Colorado
    Posts
    45

    VBA "Select Case" Troubles


    Hello again! I am currently working on a code that discovers an items weight and uses a conversion method to convert that into a specific type. Now the big issue here is that in my code I have a Select Case for the weight (since there are three columns available for the weight) but it keeps defaulting to the Case Else even though there is clearly other Case matches above it. Here is my code:

    Code:
     
    Private Sub AnalysisUNITS_AfterUpdate()
        
        Dim resultsBegin As Integer
        Dim resultsFinish As Integer
        Dim calculationGramLiter As Integer
        Dim calculationWeightPerc As Integer
        Dim finalSG As Integer
        Dim InvNum As Integer
        
        'Transferring Data from form onto VB code
        [resultsBegin] = [AnalysisResults]
        [InvNum] = 999
        
        Select Case [SampleSG]
            Case SampleSG.Value > 0
                [finalSG] = SampleSG.Value
            Case SampleSG_SP.Value > 0
                [finalSG] = SampleSG_SP.Value
            Case SampleSG_LAB.Value > 0
                [finalSG] = SampleSG_LAB.Value
            Case Else
                MsgBox ("Please Enter a SG before continuing.")
                    [Results] = [InvNum]
        End Select
        
        Select Case [AnalysisUNITS]
            Case [AnalysisUNITS] < 0
                MsgBox ("Please Enter the Analysis Units of Measure.")
                [Results] = [InvNum]
            Case [AnalysisUNITS] = 4
                [calculationWeightPerc] = [resultsBegin]
            Case [AnalysisUNITS] = 1
                [calculationGramLiter] = [resultsBegin]
                [calculationWeightPerc] = [calculationGramLiter] / [finalSG] / 10
            Case [AnalysisUNITS] = 2, 6, 8
                [calculationGramLiter] = [resultsBegin] * 1000
                [calculationWeightPerc] = [calculationGramLiter] / [finalSG] / 10
            Case [AnalysisUNITS] = 7
                [calculationGramLiter] = [resultsBegin] * 1000000
                [calculationWeightPerc] = [calculationGramLiter] / [finalSG] / 10
            Case [AnalysisUNITS] = 56, 68
                [calculationGramLiter] = [resultsBegin] * 0.001
                [calculationWeightPerc] = [calculationGramLiter] / [finalSG] / 10
            Case Else
                'Do Nothing
        End Select
        
        Select Case [calculationWeightPerc]
            Case [calculationWeightPerc] < 0
                [resultsFinish] = [InvNum]
            Case [calculationWeightPerc] > 100
                [resultsFinish] = [InvNum]
            Case Else
                [resultsFinish] = [calculationWeightPerc]
        End Select
        
        [Results] = [resultsFinish]
        'MsgBox ("The Criteria is " & Criteria & " the Value is " & AnalysisUNITS.Value)
    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    Don't repeat the input in a Case structure:

    Case Is < 0
    ...
    Case 4
    ...
    Case 1
    ...
    Case 2, 6, 8
    ...
    Case 7
    ...
    Case 56, 68
    ...
    Case Else
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Dalagrath is offline Only a Man
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2010
    Location
    Colorado
    Posts
    45
    Thanks for the quick reply and help with the bottom coding.

    However, the main issue is with this segment here:

    Code:
     
        Select Case [SampleSG]
            Case SampleSG.Value > 0
                [finalSG] = SampleSG.Value
            Case SampleSG_SP.Value > 0
                [finalSG] = SampleSG_SP.Value
            Case SampleSG_LAB.Value > 0
                [finalSG] = SampleSG_LAB.Value
            Case Else
                MsgBox ("Please Enter a SG before continuing.")
                    [Results] = [InvNum]
        End Select

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    Don't think Case can be used for this. You are changing the input in each case. Use If Then Else instead.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Dalagrath is offline Only a Man
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2010
    Location
    Colorado
    Posts
    45
    As I read it, I thought that Case only went until it found the correct Case conditons and stopped at that point? Or I might be mistaken.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    Goes until it finds condition that meets the input of the Select Case line.

    Select Case replaces:

    If [AnalysisUNITS] < 0 Then
    'do this
    ElseIf [AnalysisUNITS] = 1 Then
    'do this
    ElseIf [AnalysisUNITS] = 2 Then
    'do this
    Else
    'do nothing
    End If

    You want to do:

    If [SampleSG] > 0 Then
    'do this
    ElseIf [SampleSG_SP] > 0 Then
    'do this
    ElseIf [SampleSG_LAB] > 0 Then
    'do this
    Else
    'do this
    End If
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Dalagrath is offline Only a Man
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2010
    Location
    Colorado
    Posts
    45
    I replaced my code with the following (per your instructions) and still got the same thing.

    Code:
     
        If [SampleSG] > 0 Then
         [finalSG] = [SampleSG]
        ElseIf [SampleSG_SP] > 0 Then
            [finalSG] = [SampleSG_SP]
        ElseIf [SampleSG_LAB] > 0 Then
            [finalSG] = [SampleSG_LAB]
        Else
            MsgBox ("Please Enter a SG before continuing.")
            [Results] = [InvNum]
        End If

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    Step debug. Set a breakpoint on the If Then line. Follow the code one line at a time as it executes. Are the values from form getting read by the code?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Dalagrath is offline Only a Man
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2010
    Location
    Colorado
    Posts
    45
    I literally cannot understand this issue. When I break part of the line, part of the Case Structure for the specific calculations works...

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    Do not understand 'break part of the line'.

    Do you want to make project available for analysis? Attach to post, zip if large, run Compact & Repair first.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    Dalagrath is offline Only a Man
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2010
    Location
    Colorado
    Posts
    45
    No I understand the breaking and debugging, and now i discovered more errors. Let me mess with it some more and see if I can get the answer. thank you though.

  12. #12
    Dalagrath is offline Only a Man
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2010
    Location
    Colorado
    Posts
    45
    Through some debugging, I realized that the main issue is with this section:

    Code:
     
        Select Case [AnalysisUNITS]
            Case [AnalysisUNITS] = 4
                [calculationWeightPerc] = [resultsBegin]
            Case [AnalysisUNITS] = 1
                [calculationGramLiter] = [resultsBegin]
                [calculationWeightPerc] = [calculationGramLiter] / [finalSG] / 10
            Case [AnalysisUNITS] = 2, 6, 9
                [calculationGramLiter] = [resultsBegin] * 1000
                [calculationWeightPerc] = [calculationGramLiter] / [finalSG] / 10
            Case [AnalysisUNITS] = 7
                [calculationGramLiter] = [resultsBegin] * 1000000
                [calculationWeightPerc] = [calculationGramLiter] / [finalSG] / 10
            Case [AnalysisUNITS] = 56, 68
                [calculationGramLiter] = [resultsBegin] / 1000
                [calculationWeightPerc] = [calculationGramLiter] / [finalSG] / 10
        End Select
    This is weird because the 2, 6, 9 Case acts funny as well. The 6 and the 9 work perfect, while the 2 just plain ignores the executable code.

    In fact only Case 6 and 9 work...

  13. #13
    drexasaurus's Avatar
    drexasaurus is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    Santa Ana, CA
    Posts
    60
    I notice you are using Windows 7 and Access 2010. Are you creating the program on a Windows 7 machine and running it on an earlier OS, or did you create the file originally on a different OS than you are currently developing this file on? I ask because I had trouble with Select statements and certain types of string comparisons in this scenario. Decompiling and repairing on the oldest OS fixes this problem for me.

    Also, if you suspect that the problem is not with the code and it compiles, you may want to decompile the code and try it again. It usually turns out that I am doing something wrong, but every once in a while this fixes a problem for me.

  14. #14
    Dalagrath is offline Only a Man
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2010
    Location
    Colorado
    Posts
    45
    Thnaks for the tips, I will try those and get back to you.

  15. #15
    AndreT is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jan 2011
    Posts
    25
    Try this -

    Select Case True
    case a>0
    ...
    Case a=1
    Case b=5
    case x=y
    case else
    end select

    the first condition evaluated to be true will be executed.

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

Similar Threads

  1. Replies: 16
    Last Post: 07-22-2011, 09:23 AM
  2. ComboBox "Select" and "DLookUp"
    By witooldas in forum Forms
    Replies: 0
    Last Post: 03-23-2011, 03:31 AM
  3. SELECT a "TWO WORD" field
    By doci4a in forum Queries
    Replies: 11
    Last Post: 03-10-2011, 03:17 AM
  4. "Group By" causes "ODBC--Call Failed" error
    By kaledev in forum Queries
    Replies: 1
    Last Post: 03-09-2011, 02:43 PM
  5. Select "autonumbers" not in sequence
    By jerry525 in forum Queries
    Replies: 5
    Last Post: 11-09-2008, 02:48 PM

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