Problem with "Select Case" in VB6

  Simsy 16:52 24 Aug 2003

Hi chums,
I'm having a little problem with some VB code...

The basic premise is that there are 2 text boxes, txtWeek and txtRota. I am trying to restrict the entry in to the txtWeek box to be a number in the range 1-53.

I am using the ascii value of the keypress to permit an entry, or otherwise, and I'm using the length of the entry in the textbox to determine which ascii codes, (i.e. which digits), are permissible.

I'm trying to use the "select case" function but am having a problem. The error messageI'm getting is;

Compile error
Case without Select Case

and when I click "debug" it is "Case 2" that is highlighted.

Here is the code for the procedure;

Private Sub txtWeek_KeyPress(KeyAscii As Integer)

Select Case Len(txtWeek)
Case 0
If Not (KeyAscii > 47 And KeyAscii 58) Then
KeyAscii = 0
End If
Case 1
If Val(txtWeek) = 5 Then
If Not (KeyAscii > 47 And KeyAscii 52) Then
KeyAscii = 0
End If
Case 2
KeyAscii = 0
End Select
End Sub

Can anyone help correct this for me please?
(Note that I realise this may not be the best way to achieve what I am after... this is a learning exercise and I want to get to the bottom of the "Select case" method.)

Thanks in anticipation,



Your Select Case code is slightly awry. Basically what you are saying is that depending upon the length of txtWeek you want something to happen.

Now each case event is purely derived by the length of txtWweek. I will use pseudo code

Select Case Len(txtWeek)

'the length of txtWeek is one character long

Case 1: Action 1

'the maximum length of 2 characters gives

Case 2: Action 2

End Select

I think that you will realise that you cannot use a keypress event to trigger the Select Case as it will only ever contain the one character (or should!) perhaps Exit might work but seriously the best option is to discard the textbox in favour of the Listbox.


  Simsy 20:31 24 Aug 2003

I have discovered what the problem was, with advice from a VB forum..... I had missed an "End if"... though why the error message was indicating a case "select error" has me baffled!

Site formatting didn't help reasing my code there!

I have refined the code SLIGHTLY since my original post; it now starts with an "If" statement to exit the sub if the ascii is 8, (backspace), or 127,(delete), i.e. those are always actioned as pressed.

However, assuming that it isn't one of those, the Case Select does come into play immediately...

When the textbox is empty the Len at this point is "0". If the keypress is a "5" then the "5" is entered. This makes the Len 1. For the next keypress the Len is now "1" so the conditions for the Case of the Len = 1 comes into play, only permitting an entry from 0-3.

The Len will only be one of the following;

0 (Textbox empty)

1 (Having a single character 1-9)

2 (Having a 2 digit number, maximum 53)

It does work.... so far... I've the rest of it to complete still!



This thread is now locked and can not be replied to.

Elsewhere on IDG sites

Galaxy Note 8 vs iPhone X

Awful clip art from 1994 is being tweeted every hour by a bot

iPhone X vs Samsung Galaxy Note 8

Les meilleurs navigateurs internet 2017