473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to solve this problem

284 100+
i am using vb6.

in a form i had a text box where i only want to accept numeric values
it may be decimal value
so i use the following code in key press event of the textbox

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub amounttxt_KeyPress(KeyAscii As Integer)
  3. Dim comp As Boolean
  4. comp = Chr(KeyAscii) Like "[0-9.]"
  5. If Chr(KeyAscii) = vbBack Then Exit Sub
  6. If comp = False Then
  7. KeyAscii = 0
  8. End If
  9. End Sub
the code is working fine the problem arises
when the user mistaken writes some values like this

Expand|Select|Wrap|Line Numbers
  1.  200.62.365 instead of 20062.365
  2. or 
  3. 20..36 instead of 20.36 
  4.  
  5. (by mistake user may enter two time decimal in the box )
  6.  
  7.  
as i am also doing some more calculations in it so it give error .

so how to avoid this . what is the solution?
thanks
Jun 23 '08 #1
5 1137
mafaisal
142 100+
Hello Nasir

Try This

Expand|Select|Wrap|Line Numbers
  1. Private Sub Text1_KeyPress(KeyAscii As Integer)
  2. Dim TmpStr As String
  3. TmpStr = "0123456789."
  4. If KeyAscii = 8 Then Exit Sub
  5. If InStr(TmpStr, Chr(KeyAscii)) = 0 Then KeyAscii = 0
  6. Dim Tmp As Integer
  7. Tmp = InStr(Text1.Text, ".")
  8. If Tmp > 0 And KeyAscii = 46 Then KeyAscii = 0
  9. End Sub
  10.  
  11.  
  12.  
I Think This Solve ur Problem

Faisal
Jun 23 '08 #2
kadghar
1,295 Expert 1GB
I'd rather use the ISNUMERIC function.

it does the same thing:
Expand|Select|Wrap|Line Numbers
  1. Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  2. If (KeyAscii = 8 Or KeyAscii = 45) Then Exit Sub
  3. If Not IsNumeric(TextBox1.Text & Chr(KeyAscii)) Then KeyAscii = 0
  4. End Sub
HTH

Now that i read my code, you should check that the Ascii 45 only is valid when len(textbox1.text) = 0
Jun 23 '08 #3
muddasirmunir
284 100+
i think isnumeric function is much easier
Jun 23 '08 #4
muddasirmunir
284 100+
Hy

i haved used this
  1. <LI style="FONT-SIZE: 8pt; BACKGROUND: #fcfcfc">[font='Courier New', Courier, monospace]If (KeyAscii = 8 Or KeyAscii = 45) Then Exit Sub[/font]
  2. [font='Courier New', Courier, monospace]If Not IsNumeric(TextBox1.Text & Chr(KeyAscii)) Then KeyAscii = 0[/font]
but this is acceptiong + and - also (i-e +2336.36 or -36693)
i had reduced the keyascii codes and try till 35 but still its accpting + but - is remonve.

how can i remonved both
thanks
Jun 23 '08 #5
kadghar
1,295 Expert 1GB
well,

something like this will do:

Expand|Select|Wrap|Line Numbers
  1. If Not IsNumeric(TextBox1.Text & Chr(KeyAscii)) Or KeyAscii = 45 Or KeyAscii = 43 Then KeyAscii = 0
HTH
Jun 23 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: BVM | last post by:
Hi, All: I have this error. It seems execution time is too long. Actually the execution time is about 30 seconds(I tested in Query analyzer). How do I solve this problem? ...
3
by: Alex | last post by:
Hi, I have a problem involving some design issue. I have two unrelated (that is, they do not derive from the same base) classes: ClassA ClassB Both have a quite similar interface, so they can...
7
by: Shapper | last post by:
Hello, I have an ASP:ImageButton where I want to call a function and pass a string: OnClick="Change_Photo("John")" I am having problems with "". I tried
6
by: Federico | last post by:
Hi, this is what I can do: - Create new solutions using VS.Net ASP.Net - Save the solutions, build the solution, view in browser with the solution still open. But, once I close the solution, I...
0
by: Jitesh | last post by:
I am facing a problem in webservice, I want to know what will be the exact procedure to solve the problem............. What I want to do............ I have a table named order in SQL Server....
27
by: John Salerno | last post by:
Ok, here's a problem I've sort of assigned to myself for fun, but it's turning out to be quite a pain to wrap my mind around. It's from a puzzle game. It will help if you look at this image: ...
8
by: vj | last post by:
Hi all, I want to solve the two equations u*tan(u)=w and u^2 + w^2=V^2, where V is a known constant, and u and w are the two unknowns to be determined. Please can someone suggest me how to...
1
by: arun | last post by:
Query is too complex -------------------------------------------------------------------------------- Hi, I was trying to solve this problem since last two days but couldn't find any solution. ...
17
by: Michael Reichenbach | last post by:
Here is the example code. int main(int argc, char *argv) { string Result; WIN32_FIND_DATA daten; HANDLE h = FindFirstFile(TEXT("c://test"), &daten); system("PAUSE"); return EXIT_SUCCESS; }
2
by: itsvineeth209 | last post by:
My task is to create login control without using login control in tools. I shouldnt use sqldatasource or any other. I should use only data sets, data adapters and data readers etc. U had created...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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
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,...

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.