Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB String Function

Posted on 2002-04-24
4
Medium Priority
?
142 Views
Last Modified: 2010-05-02
I'm using VB6 against an sql server 7 database.

I bring up a form and I'm trying to test a text box for numeric values.
Currently, I'm using this function:
---------------
function isdigit(tmp as string) as integer
 if isnumeric(tmp) then
  isdigit = 1
 else
  msgbox "Field must be numeric"
  isdigit = 0
 end if
end function
-----------------------------
I call this function in a lostfocus() event but I'm wondering if there is a more efficient method to test for numeric values.
I cannot directly say isDigit(blah.Text) ...
I get a parameter type mismatch. Is there a way I can do the call the way I want to ? Or do I have to live with what I have ?


Thanks in Advance.
John
0
Comment
Question by:jtrapat1
  • 2
4 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 6965773
If you only want to allow numeric entries, use something like this:

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If Not IsNumeric(Chr$(KeyAscii)) And KeyAscii <> 8 Then KeyAscii = 0
End Sub
0
 
LVL 5

Expert Comment

by:raizon
ID: 6965887
I agree with Azra but I would add one thing
Private Sub Text1_KeyPress(KeyAscii As Integer)
   If Not IsNumeric(Chr$(KeyAscii)) And KeyAscii <> 8 Then
     KeyAscii = 0
     Beep
   End If
End Sub


I would add the beep so that the user new that they pressed the wrong key.
0
 

Expert Comment

by:WPJillett
ID: 6966062
There is no defence here for the case of an erstwhile user who WILL at some point cut and paste some non numeric data to that field.  

What you propose eliminates 98% of the problem.

Using a belt & suspenders approach you will need a LostFocus routine to check the final submission.
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 200 total points
ID: 6966135
This prevents a cut and past operation (via Ctrl+V or Ctrl+C) because the textbox rejects all key presses that are not numeric (or the backspace and delete key).  The argument may be that some users will want to copy/paste actual numeric values, but the text box would reject them.

If you do want to validate data after focus has been left, I would recommend the Validate event, since that is the real reason it exists.

Granted, it is always best to do a full data validation routine before submitting any sensitive data, no matter how well you think you may have validated data as it was entered.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

773 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