Printable Version of Topic

Click here to view this topic in its original format

UtterAccess Forums _ Access Reports _ Select Case Not Working As Expected

Posted by: InfoHound Nov 8 2019, 07:02 AM

This code generates the error: "Compile Error : End Select without Select Case"


CODE
Private Sub lngCurrentMHrs_AfterUpdate()
Dim lngHoursInSer As Double
On Error GoTo ErrHan
Me.lngCurrHrs = Me.lngCurrentMHrs
Me.lngBHrs = Me.lngPastHrs + Me.lngCurrHrs
lngHoursInSer = Me.lngBHrs
Select Case lngHoursInSer
    Case Is = lngHoursInSer < Me.lngPastHrs
         Me.lngCurrentMHrs = 0
        Me.lngCurrHrs = 0
        Me.lngBHrs = 0
        'GoTo ExitErr
    Case Is = lngHoursInSer > Me.lngPastHrs + 10
        Me.lngCurrentMHrs = 0
        Me.lngCurrHrs = 0
        Me.lngBHrs = 0
        GoTo ExitErr
     Case lngHoursInSer < Me.lngPastHrs + 10
        Me.lngCurrentMHrs = 0
        Me.lngCurrHrs = 0
        Me.lngBHrs = 0
        GoTo ExitErr
End Select

Posted by: penfold098 Nov 8 2019, 07:34 AM

Your last Case statement seems to have lost its "IS".

Should be:

Case Is = (lngHoursInSer < Me.lngPastHrs + 10)

Also, since you are creating booleans (comparisons), shouldn't the SELECT statement be:

SELECT CASE true


BTW, what happens if lngHoursInSer = Me.lngPastHrs+10 ? That case is not covered.

Posted by: BruceM Nov 8 2019, 08:18 AM

Since lngHoursInSer is the Case being examined, don't repeat it for each test. Try:

Case Is < Me.lngPastHrs

and so forth.

In your example lngCurrentMHrs, Me.lngCurrHrs, and Me.lngBHrs are 0 no matter what. I assume it is for testing purposes, but it makes it more cumbersome to figure out which Case was applied.

Are you storing a calculated value in lngBHrs? Best practice is to perform such calculations on the fly.

Another point is that unless you have a specific reason to use Double it often is better to choose Currency. Single and Double are subject to floating decimal point errors. In the same way 1/3 cannot be expressed as a precise decimal value, some binary values are likewise imprecise in ways you won't see unless you display all decimal places. It is a little confusing that you are using the lng prefix for Double. Is that also true of your field values?

Edit: I don't know what happens at ExitErr, but when a test is true the code proceeds only to the next Case statement, then skips past End Select. In other words, the other Cases are ignored. There may be no need for the GoTo.

Posted by: kfield7 Nov 8 2019, 08:29 AM

The only case not going to ExitErr is <Me.lngPastHrs.
In all 3 cases the variables are set to 0.

Wouldn't this accomplish the same?:

if lngHoursInSer <> Me.lngPastHrs+10 then 'as Penfold mentioned, =+10 is not addressed, so I conclude you only need these actions if <>+10.
Me.lngCurrentMHrs = 0
Me.lngCurrHrs = 0
Me.lngBHrs = 0
If lngHoursInSer >= Me.lngPastHrs then GoTo ExitErr ' since ExitErr is commented out in the first test.
end if

Posted by: cheekybuddha Nov 8 2019, 09:32 AM

You won't get the desired effect with the syntax as you have it.

To test different scenarios you need to test against True:

CODE
' ...
Select Case True
    Case lngHoursInSer < Me.lngPastHrs
        Me.lngCurrentMHrs = 0
        Me.lngCurrHrs = 0
        Me.lngBHrs = 0
        'GoTo ExitErr
    Case lngHoursInSer > Me.lngPastHrs + 10
        Me.lngCurrentMHrs = 0
        Me.lngCurrHrs = 0
        Me.lngBHrs = 0
        GoTo ExitErr
     Case lngHoursInSer < Me.lngPastHrs + 10
        Me.lngCurrentMHrs = 0
        Me.lngCurrHrs = 0
        Me.lngBHrs = 0
        GoTo ExitErr
End Select


That said, the same thing happens for each different Case scenario - so what's the point?

Posted by: InfoHound Nov 13 2019, 07:59 AM

Sorry for the delay. I like to express my gratitude to all for helping out.

Although all the solutions were good David yours is the one that works the best.

CODE
Select Case True


Thanks again!

Posted by: cheekybuddha Nov 13 2019, 08:05 AM

yw.gif

I'm pleased we all could help.

thumbup.gif

d