Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

allow whole numbers ONLY 3

Status
Not open for further replies.

LMCRYER

Programmer
Jul 30, 2001
388
US
Stupid question, but...

I'm trying to create a very simple front end app for one of my clients and use it to run their crystal reports. I usually use compiled crystal rpts to do this but need to use vb for client specific reasons --

I have a good lid on the crystal end, but theres a text box
in vb that requires entry of a number - and I need it to be a whole number only.

I was under the impression that datatype LONG would do that, but it does not seem to be working that way.

What is happening is this:

if the user puts in 8.5 it rounds it up to 9, I need it to
NOT allow them to enter 8.5 to begin with, and I do have the msgbox with the error msg in it set up, but yet I dont seem to be able to force it to not allow that stupid 8.5...

what am I doing wrong? help?

lmc
cryerlisa@hotmail.com
 
In the textbox_change event: check for numbers only (and the Enter & Tab keys presses - so that the client may exit the box.) The following is only in psudeocode, since you are a programmer it is up to you code in the specifics, but you can use the following as a basis for your work.


If keypress= 0 to 9 then
add to existing textbox
elseif keypress= Enter or Tab then
change focus to next box
else (all other keystrokes EX: the period or comma)
msgbox there's a problem here.....
endif


Good luck, --MiggyD

Never be afraid to try something new. Remember that amateurs built the Ark. Professionals built the Titanic.
 
(banging head) - see, so simple I didnt think of it!! keypress... thank you!
 
<placing pillow before bloodshed> You're welcomed.

Sorry, I didn't post more exact code, but as I said it's up to you to code it the way you want it to work. (like not have the tab or enter keys work until there's a certain amount of numbers in the (text) field or whatever.)

Again, glad I could help. --MiggyD

Never be afraid to try something new. Remember that amateurs built the Ark. Professionals built the Titanic.
 
thanks to both for your help - I've been working all weekend and my brain is just not working the way it is suppposed to!

:)

lmc
cryerlisa@hotmail.com
 
Hi!
Here is the way you can control keys only you allow to enter in the field.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Function AllowChar(KeyAscii As Integer, s As String) As Boolean
If InStr(s, Chr(KeyAscii)) Then
AllowChar = True
Else
AllowChar = False
End If
End Function

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 32 Then Exit Sub
If KeyAscii = vbKeyReturn Then KeyAscii = vbKeyTab
KeyAscii = 0
If Not AllowChar(KeyAscii, &quot;1234567890.,&quot;) Then
KeyAscii = 0
End If

End Sub
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-
Bye....
 
you're GREAT! I was &quot;almost&quot; here myself, I found the function but was struggling to tweak it...

thanks for the help!

lmc
cryerlisa@hotmail.com
 
JYingling - this goes in the keypress event for the text box?
Its throwing all kinds of strange errors when I try it your way...

???

lmc
cryerlisa@hotmail.com
 
Sorry, missing &quot;End If&quot; but the compiler should have told you that.
If Not(Chr$(KeyAscii) Like &quot;[0-9.,]&quot;) then
KeyAscii = 0
Exit Sub
End If
Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 


Here is the code I have so far, it will not take the bottom one for vbkeyreturn, ...your thoughs, anyone???

Private Sub txtNumTickets_KeyPress(KeyAscii As Integer)

Select Case KeyAscii
Case 1 To 7
KeyAscii = 0
'leave out 8 because this is the backspace key
Case 9 To 48
KeyAscii = 0
'allow 49 to 57 (numbers 1 to 9)
Case Is > 58
KeyAscii = 0
Case Is = vbKeyTab
KeyAscii = vbKeyTab
txtNumTickets.SetFocus
Exit Sub
End Select


'trying to make the return key work as a tab but doesnt work?
' Case Is = vbKeyReturn
' KeyAscii = vbKeyTab
 
Couldn't you use:

Code:
if textbox.text mod 1 = 0 then
    'do stuff
else: msgbox &quot;only whole numbers!&quot;
end if

just a thought...
 
I hadn't thought of that but will certainly give it a try!


Anybody happen to understand why the code I am trying to use for the vbKeyReturn isnt working? Even if I don't use it (if the code from doeboy works) I still want to understand what I am doing wrong???

lmc
cryerlisa@hotmail.com
 
LMCRYER,

vbKeyReturn has a value of 13

you have a 'Case 9 To 48'
before 'Case Is = vbKeyReturn'
 
vbKeyTab is 9 and you are excluding it.
Put this first.
Case Is = vbKeyTab
KeyAscii = vbKeyTab
txtNumTickets.SetFocus

Also MOD may not work
&quot;Remarks

The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result. For example, in the following expression, A (result) equals 5.&quot;
Besides
textbox.text mod 1 = 0
will probably throw an error when just a &quot;.&quot; is entered. It does not pay to perform arithmetic operations on uncompleted and &quot;suspect&quot; input without ON ERROR.


Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
NO kidding, I was almost asleep last night and it hit me, return is 13!! I fell asleep trying to remember that so I could fix it this morning...

ok, I did try the mod last night and it does not work.

but I do see why my case statement is not listening -

thank you to all who have posted, you guys are GREAT!!
 
ok, (getting ready to kick the monitor in)...

I understand why the vbKeyTab was being ignored and that works fine, but the stupid vbKeyReturn is NOT cooperating

I thought well, ok...I want to set a case for all of the ones I want to come in and then just dump everything else in the ELSE and set THAT to 0..so I did this:

Private Sub txtNumTickets_KeyPress(KeyAscii As Integer)

Select Case KeyAscii
Case Is = vbKeyBack
Case Is = vbKeyReturn
Case Is = vbKeyTab
Case 49 To 57
Case Else
KeyAscii = 0

Now, why WONT THIS WORK? I allowed for a case for each of the keys I want to &quot;allow&quot; and dumped the rest into the ELSE. The vbKeyBack and vbKeyTab WORK! why not the vbKeyReturn???



 
WHHHOOOO HOOOOO !! Yes!!! It works, it works! (jumping up and down with joy)

I finally decided to try something that I thought was just TOO simple for VB and use SendKeys...and it worked! here is the code, just in case anybody is interested!!

NOTE: this allows the user to hit any number key, (EXCEPT ZERO) and also allows tab, return, and backspace keys.

Private Sub txtNumTickets_KeyPress(KeyAscii As Integer)

Select Case KeyAscii
Case Is = vbKeyBack
Case Is = vbKeyReturn
SendKeys &quot;{TAB}&quot;
KeyAscii = 0
Case Is = vbKeyTab
Case 49 To 57
Case Else
KeyAscii = 0
End Select

End Sub


All of THAT work just for that little bit of code? I think I need to forget about mastering VB!! Once I'm done with this client's front end app, thats it - Im going back to my beloved Crystal Reports....

lmc
cryerlisa@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top