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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enter Numbers Only into a TextBox 1

Status
Not open for further replies.

007MCSE

IS-IT--Management
Jan 3, 2003
51
US
What do I need to add to the following code so that they can only enter a whole number and no text into the textbox in a subform.

If they dont enter a number then I want it to display a message. If they do enter a number then I want it to save it to the Speed.ini file.

I've been trying the If Else statement but I can't get it to work.

Private Sub Form_Load()
Open "c:\Program Files\PPortLC1\Data\Speed.ini" For Input As #1
Line Input #1, T1
Speed.Text = T1
Close #1
End Sub

Private Sub Quit_Click()
Call PPortLC1.Close_Forms
Call PPortLC1.Open_PPortLC1_Form
End Sub

Public Sub Save_Click()
Open "c:\Program Files\PPortLC1\Data\Speed.ini" For Output As #1
T1 = CInt(Speed.Text)
Write #1, T1
Close #1
Call PPortLC1.Close_Forms
Call PPortLC1.Open_PPortLC1_Form
End Sub

Thanks for any help
Gary
 
007MCSE

There are a number of examples of how to deal with this problem in this forum.

However, I would investigate the isNumeric function and see if that suits.

and BTW I would also think about using GetSetting & writeSetting to write your INI file. These are builtin functions.

Take care

Matt
 

There are a couple of ways to accomplish this.

One way is to use the maskedit box with a mask of "####" for however many characters you want to allow the user to enter.

The other way is through code using a regular text box.
[tt]
Option Explicit

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii = vbKeyBack Then Exit Sub
If KeyAscii = vbKeyTab Then Exit Sub

If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0

End Sub
[/tt]
or
[tt]
Option Explicit

Private Sub Text1_KeyPress(KeyAscii As Integer)

Dim C As String

If KeyAscii = vbKeyBack Then Exit Sub
If KeyAscii = vbKeyTab Then Exit Sub

C = Str(KeyAscii)
Select Case C
Case &quot;0&quot; To &quot;9&quot;
Case Else
KeyAscii = 0
End Select

End Sub
[/tt]

Good Luck

 
Thanks for your input.

It didn't work, so what I decided was to do an error handler, that way I could ensure that the saved firle (Speed.ini) dosen't get saved with a null value and the on error routine would set a default value.

I tried using errortrap and errorhandle, but i can't get it to work. Instead of it going through the error routine it hangs and displays a mismatch error. I thought the idea of error routines was to catch the errors and carry out a error correction routine.

I've enclosed my code below. Can anyone see what I've done wrong.

Public Sub Save_Click()

'On Error GoTo ErrorHandler:
Open &quot;c:\Program Files\PPortLC1\Data\Speed.ini&quot; For Output As #1
T1 = CInt(Speed.Text)
Write #1, T1
Close #1
Call PPortLC1.Close_Forms
Call PPortLC1.Open_PPortLC1_Form
On Error GoTo ErrorTrap
Exit Sub

'ErrorHandler:
ErrorTrap:
MsgBox (&quot;Please enter a valid number!&quot;)
Open &quot;c:\Program Files\PPortLC1\Data\Speed.ini&quot; For Output As #1
T1 = 150
Write #1, T1
Close #1
Call PPortLC1.Close_Forms
Call PPortLC1.Speed_Click

Resume Next
End Sub

Thanks in advance for any help
Gary
 
instead of

Open &quot;c:\Program Files\PPortLC1\Data\Speed.ini&quot; For Output As #1
T1 = CInt(Speed.Text)
Write #1, T1
Close #1
Call PPortLC1.Close_Forms
Call PPortLC1.Open_PPortLC1_Form
On Error GoTo ErrorTrap
Exit Sub


use


On Error GoTo ErrorTrap
Open &quot;c:\Program Files\PPortLC1\Data\Speed.ini&quot; For Output As #1
T1 = CInt(Speed.Text)
Write #1, T1
Close #1
Call PPortLC1.Close_Forms
Call PPortLC1.Open_PPortLC1_Form

Exit Sub


the on error statement needs to be before the error occurs.
 
I allready tried that and it still dosen't work and I can't understand why.
 
i agree your code looks like it should work. have you single stepped it or commented bits out?? does it give the message on th T1= bit??
 
Hi ADozer

Yes i've stepped through it and I get the error as soon as I get to T1 = CInt(Speed.Text).
I get a type mismatch error instead of it going into my error routine.

Gary
 
Sounds like you've got two errors - the type mismatch error which is probably due to how T1 has been declared. T1 should be either Integer, Long, Single or Double. If you have not explicitly put declared T1 as one of the above four data types - then I would do so. Your code should look similar to the following:
Code:
Dim T1 As Integer
' Uncomment and remove trailing colon
Code:
On Error GoTo ErrorHandler
Open &quot;c:\Program Files\PPortLC1\Data\Speed.ini&quot; For Output As #1
   T1 = CInt(Speed.Text)
   Write #1, T1
Close #1
Call PPortLC1.Close_Forms
Call PPortLC1.Open_PPortLC1_Form
Exit Sub
' Uncomment and remove trailing colon
Code:
ErrorHandler
' Remove ErrorTrap - wrong label
Code:
MsgBox (&quot;Please enter a valid number!&quot;)
Open &quot;c:\Program Files\PPortLC1\Data\Speed.ini&quot; For Output As #1
   T1 = 150
  Write #1, T1
Close #1
Call PPortLC1.Close_Forms
Call PPortLC1.Speed_Click
'Do not want to resume next - that would put you back in the first part on the line immediately after the one which generated the error
Code:
'Resume Next
End Sub
Secondly, why is the error handler not being triggered?

In the IDE, on the menu bar on the top click on Tools, then select Options and go to the General Tab. The Upper right hand frame is for errors and make sure that the Break on Unhandled Errors is checked.



Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hi Cajun

In the IDE, on the menu bar on the top click on Tools, then select Options and go to the General Tab. The Upper right hand frame is for errors and make sure that the Break on Unhandled Errors is checked.

Thats what fixed it

But I have to put the colon after &quot;ErrorHandler&quot; otherwise I get a &quot;Compile Error: Sub or Function not defined&quot;.It works fine with the colons.

Thanks, you saved the day yet again.

Gary

 
yes, you're correct. Got to quick on the post, sorry about that. The colon does have to terminate the label itself, but is not necessary in the on error statement. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top