Solved

validating a text box value

Posted on 2001-06-26
20
296 Views
Last Modified: 2008-02-26
I am using the following code to validate a text box
value.The name of text box is txtBatchName.

The valid value contains first two characters
alphabetic and rest numeric
Ex: GH1234567, MN34567, LA78 etc
The code works fine. But when user second time
clicks on text box and place the cursor at starting
point and deletes only first two characters  and
try to enter alphabetic value it is not allowing.

Ex: First time user enters  LA1234
    Second time he deletes LA the value in text box is
    now 1234. He places cursor before 1 then start
    typing alphabetic characters. It is not allowing.
How can i do this?

Thanks,
Suresh podaralla

Private Sub txtBatchName_KeyPress(KeyAscii As Integer)
    Dim i As Integer
    i = 0
    i = Len(txtBatchName.Text)
    Select Case i
    Case 0, 1
        If KeyAscii = 8 Then
            Exit Sub
        End If
        If KeyAscii < 65 Then
            KeyAscii = 0
            Beep
            Exit Sub
        ElseIf KeyAscii > 90 Then
            If KeyAscii < 97 Then
                KeyAscii = 0
                Beep
            ElseIf KeyAscii > 122 Then
                KeyAscii = 0
                Beep
            Else
                KeyAscii = KeyAscii - 32
            End If
        End If
    Case 2, 3, 4, 5, 6, 7, 8
        If KeyAscii = 8 Then
            Exit Sub
        End If
        If KeyAscii < 48 Or KeyAscii > 57 Then
            KeyAscii = 0
            Beep
        End If
    Case Else
        If KeyAscii = 8 Then
            Exit Sub
        End If
        KeyAscii = 0
        Beep
    End Select
End Sub
0
Comment
Question by:sureshpodaralla
  • 6
  • 4
  • 4
  • +3
20 Comments
 
LVL 6

Expert Comment

by:simonbennett
Comment Utility
Hi

Found your problem. Try

Private Sub txtBatchName_KeyPress(KeyAscii As Integer)
   
   Dim i As Integer
   i = Len(txtBatchName.Text)
         
   Select Case i
   Case 0, 1
       If KeyAscii = 8 Then
           Exit Sub
       End If
       If KeyAscii < 65 Then
           KeyAscii = 0
           Beep
           Exit Sub
       ElseIf KeyAscii > 90 Then
           If KeyAscii < 97 Then
               KeyAscii = 0
               Beep
           ElseIf KeyAscii > 122 Then
               KeyAscii = 0
               Beep
           Else
               KeyAscii = KeyAscii - 32
           End If
       End If
   Case 2, 3, 4, 5, 6, 7, 8
       If KeyAscii = 8 Then
           Exit Sub
       End If
       If Not (KeyAscii >= 48 And KeyAscii <= 57) And Not (KeyAscii >= 65 And KeyAscii <= 90) Then
           KeyAscii = 0
           Beep
       End If
   Case Else
       If KeyAscii = 8 Then
           Exit Sub
       End If
       KeyAscii = 0
       Beep
   End Select
End Sub


The problem is with your line

If KeyAscii < 48 Or KeyAscii > 57 Then

which prevents entry of characters.

HTH

Simon
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
It seems that you should either do your validation in the Validate event procedure, rather than the KeyPress event, or replace the textbox with a masked edit box which allows you to do exactly what you want without writing any code.

P.S.
Have you noticed that the user can paste text with the mouse and bypass your entire validation routine because a key is never pressed?  Depressing, isn't it?  That's why you normally validate with the validate event.
0
 
LVL 6

Expert Comment

by:simonbennett
Comment Utility
>>Depressing, isn't it

LOL!
0
 
LVL 4

Expert Comment

by:amit_panjwani
Comment Utility
rspahitz ,

sure it is depressing to see simonbennett  LOL on that since he endorses what suresh is doing, instead of suggesting the right answer.

Going in for such a code is tedious and is error prone.
VALIDATE is much better way to do it.
coupled with other controls causes validation event.

Suresh, I suggest try going through MSDN - causes validation Articles, I am sure it will be of great help.


Moreover , in Validate you could use the following suggestions to make it more reliable

1. Trim the entire txt
2. Use Split Function to see if user used any spaces in
   the box
3. After that , you can check that you can use
  Left(Mystring,2) to take two starting characters
  from the first word that you get after using
  Split Function, Discard Rest words if they dont suit  
  your requirements. you can use val or similar
  functions to determine if first two characters are alphabets or not.


Hope it helps.

Amit


0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
Now, now, amit_panjwani.  simonbennett was responding to the direct question.  I chose an indirect solution because I recognized that patches are sometimes not the best answer, and apparently you see the same way.  I think that simonbennett also recognized this which is why he commented as he did.
0
 

Author Comment

by:sureshpodaralla
Comment Utility
Simon,
Your code does not work.
I found the problem.The problem is with
i = Len(txtBatchName.Text)
when user comes second time to the text box
i value is getting Length of text box.
Now i am assining i value with the cursor position.
But i need to solve one more problem.
If user highlight the text in the text box
it is not deleting until user clicks on delete button.
Customer wants usual windows fashion of deleting
a selected text...how can i do this

Thanks,
suresh

Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" _
  (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private Const EM_GETSEL = &HB0
Private Const EM_SETSEL = &HB1
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Private Const EM_LINEFROMCHAR = &HC9

Private Sub txtBatchName_KeyPress(KeyAscii As Integer)
    Dim overallCursorPos As Long
    Dim i As Integer
    overallCursorPos = SendMessageLong(txtBatchName.hwnd, EM_GETSEL, 0, 0&) \ &H10000
    i = overallCursorPos
    Select Case i
    Case 0, 1
        If KeyAscii = 8 Then
            Exit Sub
        End If
        If KeyAscii < 65 Then
            KeyAscii = 0
            Beep
            Exit Sub
        ElseIf KeyAscii > 90 Then
            If KeyAscii < 97 Then
                KeyAscii = 0
                Beep
            ElseIf KeyAscii > 122 Then
                KeyAscii = 0
                Beep
            Else
                KeyAscii = KeyAscii - 32
            End If
        End If
    Case 2, 3, 4, 5, 6, 7, 8
        If KeyAscii = 8 Then
            Exit Sub
        End If
        If KeyAscii < 48 Or KeyAscii > 57 Then
            KeyAscii = 0
            Beep
        End If
    Case Else
        If KeyAscii = 8 Then
            Exit Sub
        End If
        KeyAscii = 0
        Beep
    End Select
End Sub
0
 

Author Comment

by:sureshpodaralla
Comment Utility
Hi,
I have solved this problem on my own.

The solution is like this

Private Sub txtBatchName_KeyPress(KeyAscii As Integer)
   Dim overallCursorPos As Long
   Dim i As Integer
   overallCursorPos = SendMessageLong(txtBatchName.hwnd, EM_GETSEL, 0, 0&) \ &H10000
   i = overallCursorPos
   If txtBatchName.SelText <> "" then
      txtBatchName.SelText = UCase(Chr(KeyAscii))
   End if
   Select Case i
   Case 0, 1
       If KeyAscii = 8 Then
           Exit Sub
       End If
       If KeyAscii < 65 Then
           KeyAscii = 0
           Beep
           Exit Sub
       ElseIf KeyAscii > 90 Then
           If KeyAscii < 97 Then
               KeyAscii = 0
               Beep
           ElseIf KeyAscii > 122 Then
               KeyAscii = 0
               Beep
           Else
               KeyAscii = KeyAscii - 32
           End If
       End If
   Case 2, 3, 4, 5, 6, 7, 8
       If KeyAscii = 8 Then
           Exit Sub
       End If
       If KeyAscii < 48 Or KeyAscii > 57 Then
           KeyAscii = 0
           Beep
       End If
   Case Else
       If KeyAscii = 8 Then
           Exit Sub
       End If
       KeyAscii = 0
       Beep
   End Select
End Sub
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
I still have to wonder what will happen when the user pastes into the field and they find that their data is not validated...

If you really want to go with your code, also put something in the validate event procedure.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 50 total points
Comment Utility
Hi sureshpodaralla,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Refund points and save as a 0-pt PAQ.

EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
sureshpodaralla, why did you accept my comment as an answer?  I'm going to be in the VB top 15 soon if this keeps happening.  -- Dan
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:sureshpodaralla
Comment Utility
Because I answered the question my self and dont know
how to get points my self.

Cheers!!!
Suresh
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
Way to go Dan!  Top 10 by year end?

(Obviously the points should be refunded since user said, "I have solved this problem on my own." in 06/26/2001 comment.)
0
 
LVL 5

Expert Comment

by:Netminder
Comment Utility
Points refunded per DanRollins' recommendation.

Netminder
CS Moderator

rspahitz,

Just so you know, they automatically get deducted from DanRollins' total. He has, to my knowledge, earned every point he's gotten, and then some.

Netminder
CS Moderator
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
<eyes downcast in ignoble defeat> Alas!  Just as my foot was about to touch that golden stairway...
-- Dan
0
 

Author Comment

by:sureshpodaralla
Comment Utility
<Luck play with our lives. Especially with talented ones>
Thanks to Dan and rspahitz,
Suresh
0
 
LVL 5

Expert Comment

by:Netminder
Comment Utility
Dan,

You'll make the Golden Stairway soon enough... as long as you avoid the Dark Side inhabited by the Evil Twisted SPLITLizard... <smile> ... Don't worry, Little Grasshopper...

N
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
I really wasn't concerned about Dan getting those points.  He's done a great job at cleaning up VB, and has gotten practically every recommendation I've seen right.  (The only exceptions were a very close call and one that I convinced him to PAQ rather than delete.)  With such a success rate, he probably deserves to keep a few of those points!
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
Thanks rspahitz!  But, by law I must advise you that I have printed that out and laminated it for my wallet.  It may be used against you at some point in the future.  -- Dan
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
>laminated it for my wallet
LOL!

>used against you at some point in the future
At that point, I may have to either quit EE or become an administrator.  Either one would be painful...
0
 
LVL 5

Expert Comment

by:Netminder
Comment Utility
It's only painful when COBOLdinosaur gets on one of his "split the points in every question" moods...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now