UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Select Case Not Working As Expected, Access 2016    
 
   
InfoHound
post Nov 8 2019, 07:02 AM
Post#1



Posts: 1,175
Joined: 1-December 12



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
Go to the top of the page
 
penfold098
post Nov 8 2019, 07:34 AM
Post#2



Posts: 150
Joined: 5-March 14



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.
This post has been edited by penfold098: Nov 8 2019, 07:45 AM
Go to the top of the page
 
BruceM
post Nov 8 2019, 08:18 AM
Post#3


UtterAccess VIP
Posts: 7,993
Joined: 24-May 10
From: Downeast Maine


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.
Go to the top of the page
 
kfield7
post Nov 8 2019, 08:29 AM
Post#4



Posts: 1,022
Joined: 12-November 03
From: Iowa Lot


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
Go to the top of the page
 
cheekybuddha
post Nov 8 2019, 09:32 AM
Post#5


UtterAccess Moderator
Posts: 11,893
Joined: 6-December 03
From: Telegraph Hill


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?

--------------------


Regards,

David Marten
Go to the top of the page
 
InfoHound
post Nov 13 2019, 07:59 AM
Post#6



Posts: 1,175
Joined: 1-December 12



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!
Go to the top of the page
 
cheekybuddha
post Nov 13 2019, 08:05 AM
Post#7


UtterAccess Moderator
Posts: 11,893
Joined: 6-December 03
From: Telegraph Hill


yw.gif

I'm pleased we all could help.

thumbup.gif

d

--------------------


Regards,

David Marten
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    9th December 2019 - 08:52 PM