Solved

VB String Function

Posted on 2002-04-24
4
140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…
Suggested Courses
Course of the Month10 days, 15 hours left to enroll

632 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