For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > March 2006 > Msgbox if data not valid -maskedEditControl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Msgbox if data not valid -maskedEditControl
Sandy

2006-03-26, 6:55 pm

Hello -

I have a masked edit control formatted as

>??-#####


It works like it's supposed to, however, I would like to display a message
if someone, for instance, tries to type a number in the first or second
place. How do I go about doing that?

Any help will be appreciated!
--
Sandy
nrford

2006-03-26, 9:55 pm

"Sandy" <Sandy@discussions.microsoft.com> wrote in message
news:ADEE3996-7A9A-4694-ACB7-05160388C684@microsoft.com...
> Hello -
>
> I have a masked edit control formatted as
>
>
> It works like it's supposed to, however, I would like to display a message
> if someone, for instance, tries to type a number in the first or second
> place. How do I go about doing that?


I just use a regular Text box and the KeyPress event
to test each character entered. Then I can do anything
I want with it. Here's an example from one of my apps.
This is a "floating" text box used to input data into two
grids:

Sub t_Inp_Keypress(Index)

Dim ky As String
Dim i As Integer
Dim j As Integer
Dim ss As Integer

If KeyAscii = vbKeyBack Then
' Let Backspace thru: '
Exit Sub
End If

With t_Inp(Index)

If KeyAscii = 27 Then
' Esc: restore original text: '
.Text = Grid1(Index).Text
Exit Sub
End If

ky = Chr$(KeyAscii)

If Index = 0 Then 'Top grid:
If Grid1(0).Col = 1 Then
' Maximum length = 16 = 13 cards + 3 dashes '
If Len(.Text) = 16 Then
KeyAscii = 0
Beep
Exit Sub
End If

' Convert a space to a dash to make '
' entering hands and bids easier. '
If ky = " " Then
ky = "-"
KeyAscii = 45
End If

' If card letter entered is in '
' lowercase, convert to upper: '
If InStr("akqjt", ky) > 0 Then
KeyAscii = KeyAscii - 32
ky = UCase$(ky)
End If

' Valid keypresses are AKQJT, 2-9, '
' If not one of these, cancel/exit.'
If InStr("AKQJT", ky) = 0 And _
(ky < "2" Or ky > "9") And _
ky <> "-" _
Then
KeyAscii = 0
Exit Sub
End If

i = InStr("AKQJT98765432", ky)
' Validate card entered: '
If .SelStart > 0 And i > 0 Then
j = InStr("AKQJT98765432", _
Mid$(.Text, .SelStart, 1))
If j >= i Then 'card entered is out of rank order
ss = .SelStart
.Text = Left$(.Text, .SelStart) + ky + Mid$(.Text, _
.SelStart + 1)
.SelStart = ss + 1
Beep
MsgBox "Card spec is out of order."
KeyAscii = vbKeyBack
Exit Sub
End If
End If
End If

Else 'Bottom grid

Select Case .SelStart
' .SelStart is the character '
' position minus one. '

Case 0 'player number
If InStr("1234", ky) = 0 Then
MsgBox "Not a valid character."
' Cancel: '
KeyAscii = 0
End If

Case 1 'colon
KeyAscii = Asc(":")

Case 2 'suit, t for total, or b for bid '
If InStr("CDHSTB", ky) > 0 Then
' Change to lowercase: '
KeyAscii = KeyAscii + 32

ElseIf InStr("cdhstb", ky) = 0 Then
MsgBox "Not a valid character. "
' Cancel: '
KeyAscii = 0
End If

Case 3 ' Type or bid round '
If InStr("QPC", ky) > 0 Then
' Change to lowercase: '
KeyAscii = KeyAscii + 32

ElseIf InStr("qpc12", ky) = 0 Then
MsgBox "Not a valid character. "
' Cancel: '
KeyAscii = 0
End If

Case 4 ' Math symbol: '
If InStr("<=>", ky) = 0 Then
MsgBox "Not a valid character. "
' Cancel: '
KeyAscii = 0
End If

Case Is > 4 'number or bid
If InStr("akqjtcdhsndprx", ky) > 0 Then
' Change to uppercase: '
KeyAscii = KeyAscii - 32

ElseIf InStr("1234567890AKQJTCDHSNPDRX", ky) = 0 Then
MsgBox "Not a valid character. "
' Cancel: '
KeyAscii = 0
End If

End Select

End If

End With

End Sub


Michael C

2006-03-27, 3:55 am

"nrford" <nrfordmsvb@REMOVE_THIS_cardsharkgames.com> wrote in message
> I just use a regular Text box and the KeyPress event
> to test each character entered. Then I can do anything
> I want with it. Here's an example from one of my apps.
> This is a "floating" text box used to input data into two
> grids:


Yikes!! Your app must be pretty frustrating to use. The user types the wrong
chr and before they get the chance to change it a messagebox appears. Most
users would uninstall pretty quick.

Michael


Tony Spratt

2006-03-27, 7:55 am

Michael,

"Michael C" <nospam@nospam.com> wrote in message
news:uG2A72VUGHA.2444@TK2MSFTNGP14.phx.gbl...
> "nrford" <nrfordmsvb@REMOVE_THIS_cardsharkgames.com> wrote in message
>
> Yikes!! Your app must be pretty frustrating to use. The user types the

wrong
> chr and before they get the chance to change it a messagebox appears. Most
> users would uninstall pretty quick.
>
> Michael


I once worked at a place that used NCR COBOL for its apps (before PCs were
popular, of course). When defining the screen layou, you could append an
"EXACT" tag, so that if you had a six-digit account number (for example)
after you typed six digits in, it would tab out to the next field and do any
in-line validation. Trouble was, the space, backspace, delete and arrow keys
all counted. Type three digits, backspace them out and you would still be
tabbed forward and validated. Company policy was to use "EXACT" for all
numeric fields. You can imagine what the users said when I innocently
informed them that it was an optional extension and we didn't have to do
it...

Cheers,

Tony.


Sandy

2006-03-27, 7:55 am

Thanks, everyone, for your comments, but my REAL question is:

How do I display a MessageBox if a user starts typing a number where a
character should be? In other words, what "if [what?] --- then" language
should be used with a masked edit control? E.G., if not [valid character for
that position] then msgbox.

--
Sandy


"Sandy" wrote:

> Hello -
>
> I have a masked edit control formatted as
>
>
> It works like it's supposed to, however, I would like to display a message
> if someone, for instance, tries to type a number in the first or second
> place. How do I go about doing that?
>
> Any help will be appreciated!
> --
> Sandy

DRBarkley

2006-03-27, 6:55 pm


"Sandy" <Sandy@discussions.microsoft.com> wrote in message
news:4BB03DD0-402B-4BB8-97A3-9453946BC473@microsoft.com...
> Thanks, everyone, for your comments, but my REAL question is:
>
> How do I display a MessageBox if a user starts typing a number where a
> character should be? In other words, what "if [what?] --- then" language
> should be used with a masked edit control? E.G., if not [valid character

for[color=darkred]
> that position] then msgbox.
>
> --
> Sandy
>
>
> "Sandy" wrote:
>
message[color=darkred]

Sandy,

Check out the KeyDown and KeyPress events, and the ASC() function.

HTH,

DRBarkley


DRBarkley

2006-03-27, 6:55 pm


"Sandy" <Sandy@discussions.microsoft.com> wrote in message
news:4BB03DD0-402B-4BB8-97A3-9453946BC473@microsoft.com...
> Thanks, everyone, for your comments, but my REAL question is:
>
> How do I display a MessageBox if a user starts typing a number where a
> character should be? In other words, what "if [what?] --- then" language
> should be used with a masked edit control? E.G., if not [valid character

for[color=darkred]
> that position] then msgbox.
>
> --
> Sandy
>
>
> "Sandy" wrote:
>
message[color=darkred]

Sandy,

You can also look at the textbox's Change event.

N = ASC(Right(Text1.Text,1))
If N >= ASC("0") And N <= ASC("9") Then

DRBarkley


nrford

2006-03-27, 6:55 pm

"Michael C" <nospam@nospam.com> wrote in message news:uG2A72VUGHA.2444@TK2MSFTNGP14.phx.gbl...
> "nrford" <nrfordmsvb@REMOVE_THIS_cardsharkgames.com> wrote in message
>
> Yikes!! Your app must be pretty frustrating to use. The user types the wrong chr and before they
> get the chance to change it a messagebox appears. Most users would uninstall pretty quick.


I posted this code as an example because it is the most
extreme I had for controlling input.

When I posted the code, I added all those MsgBox lines
which say "Not a valid character" to illustrate that you get
more control over showing message boxes.

In the actual code, if you enter an invalid character, the
keypress is cancelled and you get a beep.

To the best of my knowledge, nobody has uninstalled it.


nrford

2006-03-27, 6:55 pm

"Michael C" <nospam@nospam.com> wrote in message news:uG2A72VUGHA.2444@TK2MSFTNGP14.phx.gbl...
> "nrford" <nrfordmsvb@REMOVE_THIS_cardsharkgames.com> wrote in message
>
> Yikes!! Your app must be pretty frustrating to use. The user types the wrong chr and before they
> get the chance to change it a messagebox appears. Most users would uninstall pretty quick.


Oh, and in case you missed it, the OP specifically asked:

"I would like to display a message if someone, for instance,
tries to type a number in the first or second place. How do
I go about doing that?"

So while I would not do what he is asking, I *did* answer
his question.


nrford

2006-03-27, 6:55 pm

"Sandy" <Sandy@discussions.microsoft.com> wrote in message
news:4BB03DD0-402B-4BB8-97A3-9453946BC473@microsoft.com...
> Thanks, everyone, for your comments, but my REAL question is:
>
> How do I display a MessageBox if a user starts typing a number where a
> character should be? In other words, what "if [what?] --- then" language
> should be used with a masked edit control? E.G., if not [valid character for
> that position] then msgbox.


Uh, I showed you how in the code I posted.


Sandy

2006-03-28, 6:56 pm

Thanks everyone for your responses!
--
Sandy


"Sandy" wrote:
[color=darkred]
> Thanks, everyone, for your comments, but my REAL question is:
>
> How do I display a MessageBox if a user starts typing a number where a
> character should be? In other words, what "if [what?] --- then" language
> should be used with a masked edit control? E.G., if not [valid character for
> that position] then msgbox.
>
> --
> Sandy
>
>
> "Sandy" wrote:
>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com