Solved

VB String Function

Posted on 2002-04-24
4
130 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 50 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

Highfive Gives IT Their Time Back

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

744 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

14 Experts available now in Live!

Get 1:1 Help Now