473,485 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Invalid Data?

dfg
I do some error checking on my textbox. The problem is that if I enter
a valid number, then backspace until the box is empty, my msgbox pops up
saying "Data Error". Is there a way to avoid this?

Also, if I enter invalid data and try to backspace through it, my "Data
Error" msgbox pops up. This is rather annoying. Does anyone have a
solution to this problem? I posted my code - I hope it formats correctly.

Thanks.
Private Sub txtAmtSold_Change()
If txtAmtSold = "" Then
mnuEditCalculate.Enabled = False
End If

If IsNumeric(txtAmtSold) Then
If Val(txtAmtSold) >= 1 And Val(txtAmtSold) _
<= 100 Then
mnuEditCalculate.Enabled = True
Else
mnuEditCalculate.Enabled = False
MsgBox "Error: Numbers must be in the range of 1 - 100",
vbExclamation, "Range Error"
End If
Else
mnuEditCalculate.Enabled = False
MsgBox "Data Must Be Numeric", vbExclamation, "Data Error"
End If
End Sub

Jul 17 '05 #1
3 2953
If you're not hung up on throwing message boxes every time someone makes an
error, just use this:

Private Sub txtAmtSold_Change()

mnuEditCal.Enabled = Len(txtAmtSold.Text) > 0 And _
IsNumeric(txtAmtSold) And _
Val(txtAmtSold.Text) >= 1 And _
Val(txtAmtSold.Text)

End Sub
--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"dfg" <df*@dfg.net> wrote in message news:zl%mb.193461$6C4.13496@pd7tw1no...
: I do some error checking on my textbox. The problem is that if I enter
: a valid number, then backspace until the box is empty, my msgbox pops up
: saying "Data Error". Is there a way to avoid this?
:
: Also, if I enter invalid data and try to backspace through it, my "Data
: Error" msgbox pops up. This is rather annoying. Does anyone have a
: solution to this problem? I posted my code - I hope it formats correctly.
:
: Thanks.
:
:
: Private Sub txtAmtSold_Change()
: If txtAmtSold = "" Then
: mnuEditCalculate.Enabled = False
: End If
:
: If IsNumeric(txtAmtSold) Then
: If Val(txtAmtSold) >= 1 And Val(txtAmtSold) _
: <= 100 Then
: mnuEditCalculate.Enabled = True
: Else
: mnuEditCalculate.Enabled = False
: MsgBox "Error: Numbers must be in the range of 1 - 100",
: vbExclamation, "Range Error"
: End If
: Else
: mnuEditCalculate.Enabled = False
: MsgBox "Data Must Be Numeric", vbExclamation, "Data Error"
: End If
: End Sub
:
Jul 17 '05 #2
> If IsNumeric(txtAmtSold) Then

I usually try and steer people away from using IsNumeric to "proof"
supposedly numeric text. Consider this (also see note at end of post):

ReturnValue = IsNumeric("($1,23,,3.4,,,5,,E67$)")

Most people would not expect THAT to return True. IsNumeric has some "flaws"
in what it considers a proper number and what most programmers are looking
for.

I had a short tip published by Pinnacle Publishing in their Visual Basic
Developer magazine that covered some of these flaws. Originally, the tip was
free to view but is now viewable only by subscribers.. Basically, it said
that IsNumeric returned True for things like -- currency symbols being
located in front or in back of the number as shown in my example (also
applies to plus, minus and blanks too); numbers surrounded by parentheses as
shown in my example (some people use these to mark negative numbers);
numbers containing any number of commas before a decimal point as shown in
my example; numbers in scientific notation (a number followed by an upper or
lower case "D" or "E", followed by a number equal to or less than 305 -- the
maximum power of 10 in VB); and Octal/Hexadecimal numbers (&H for
Hexadecimal, &O or just & in front of the number for Octal).

NOTE:
======
In the above example and in the referenced tip, I refer to $ signs and
commas and dots -- these were meant to refer to your currency, thousands
separator and decimal point symbols as defined in your local settings --
substitute your local regional symbols for these if appropriate.

As for your question about checking numbers, here are two functions that I
have posted in the past for similar questions..... one is for digits only
and the other is for "regular" numbers:

Function IsDigitsOnly(Value As String) As Boolean
IsDigitsOnly = Not Value Like "*[!0-9]*"
End Function

Function IsNumber(ByVal Value As String) As Boolean
' Leave the next statement out if you don't
' want to provide for plus/minus signs
If Value Like "[+-]*" Then Value = Mid$(Value, 2)
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) > 0 And Value <> "." And _
Value <> vbNullString
End Function

Rick - MVP
Jul 17 '05 #3
On Mon, 27 Oct 2003 02:24:31 GMT, dfg <df*@dfg.net> wrote:
I do some error checking on my textbox. The problem is that if I enter
a valid number, then backspace until the box is empty, my msgbox pops up
saying "Data Error". Is there a way to avoid this?

Also, if I enter invalid data and try to backspace through it, my "Data
Error" msgbox pops up. This is rather annoying. Does anyone have a
solution to this problem? I posted my code - I hope it formats correctly.

Thanks. Gawd - the 'fix' solution is :-

Private Sub txtAmtSold_Change()
If txtAmtSold = "" Then
mnuEditCalculate.Enabled = False Exit Sub ' <==== GET OUT ==== End If

If IsNumeric(txtAmtSold) Then
If Val(txtAmtSold) >= 1 And Val(txtAmtSold) _
<= 100 Then
mnuEditCalculate.Enabled = True
Else
mnuEditCalculate.Enabled = False
MsgBox "Error: Numbers must be in the range of 1 - 100",
vbExclamation, "Range Error"
End If
Else
mnuEditCalculate.Enabled = False
MsgBox "Data Must Be Numeric", vbExclamation, "Data Error"
End If
End Sub


However, code like that is (IMO) completely unreadable.

Possibly because I am allergic to the use of 'Else'

In most cases 'Select Case' provides a clearer solution than messing
around with 'Else'

In this case heavy use of 'Exit Sub' would clear up your logic

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2640
by: Nate | last post by:
Hello, I'd like to somehow put minIncl and maxIncl around the data I am sending from my producer. One of the requirements is to allow the producer to send an invalid number to the consumer. ...
3
29196
by: Jimski | last post by:
Hello all, I am having a problem where I get an error message when I call FlushFinalBlock when decrypting my encrypted text. I am using the Rijndael algorithm. The error message is "Length...
2
5194
by: Brent Burkart | last post by:
Below is the error I am receiving. I have checked SQL Profiler and it is receiving the correct query which runs fine in Query Analyzer. Any ideas? Server Error in '/lockinsheet' Application....
3
2263
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
15
2216
by: David | last post by:
Hi, I have built a web application that will be a very high profile application. We had tested it, demonstrated it and shown that it all works. On a dress rehearsal run through, it failed...
9
6602
by: MR | last post by:
I get the following Exception "The data at the root level is invalid. Line 1, position 642" whenever I try to deserialize an incoming SOAP message. The incoming message is formed well and its...
2
6078
by: js | last post by:
I got this error when I moved my application to a new Windows 2003 server. I installed and recompiled the 'Microsoft.Practices.EnterpriseLibrary - June 2005" then added these assemblies to the...
1
17020
by: Timbo | last post by:
Hi all, This is my first message here so i'll try and include all the information that will help you help me out, if possible. Basically I am using C# in ASP.NET 2.0 and have a Repeater...
2
2153
by: Nathan Sokalski | last post by:
I have a DataList in which the ItemTemplate contains two Button controls that use EventBubbling. When I click either of them I receive the following error: Server Error in '/' Application....
9
4529
by: 200dogz | last post by:
Hi guys, I want to have a button which opens up a new window when pressed. <asp:Button ID="Button1" runat="server" Text="Open new window" /> ... Button1.Attributes.Add("OnClick",
0
7090
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6825
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7275
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5418
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4857
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4551
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
595
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
247
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.