Solved

VB String Function

Posted on 2002-04-24
4
138 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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.
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…

729 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