Solved

VB String Function

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

696 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