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

textbox format

Status
Not open for further replies.

Sw0rdfish149

Programmer
Joined
Jan 18, 2004
Messages
15
Location
GB
Hi everyone,

This may sound stupid but how would I format 2 textboxes in an array of several textboxes to only accept numbers?

I want to do this through code as well, in this kind of way:

If txtQuantityRemaining(X).Text = "" Then
MsgBox "Please Fill in the Quantity Remaining."
Exit Function
End If

where X is the array number.

cheers

Sw0rdfish149
 
Try something like this

Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
Select Case Index
Case 0, 2
If (KeyAscii < vbKey0 And KeyAscii <> vbKeyBack) Or KeyAscii > vbKey9 Then
KeyAscii = 0 'Allow only numbers and the backspace
End If
End Select
End Sub



If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
You could use the following...

[blue]Option Explicit

Private Sub [/blue]txtQuantityRemaining_Change(Index [blue]As Integer[/blue])
[blue]If Not [/blue]ValidateNumeric(txtQuantityRemaining(Index).Text) [blue]Then
On Error Resume Next
[/blue]txtQuantityRemaining(Index) = Left(txtQuantityRemaining(Index).Text, Len(txtQuantityRemaining(Index)) - 1)
txtQuantityRemaining(Index).SelStart = Len(txtQuantityRemaining(Index))
[blue]End If
End Sub

Private Function [/blue]ValidateNumeric(strText [blue]As String[/blue]) _
[blue]As Boolean
[/blue]ValidateNumeric = [blue]CBool[/blue](strText = &quot;&quot; _
[blue]Or [/blue]strText = &quot;-&quot; _
[blue]Or [/blue]strText = &quot;-.&quot; _
[blue]Or [/blue]strText = &quot;.&quot; _
[blue]Or [/blue]IsNumeric(strText))
[blue]End Function
[/blue]

When you are ready to process the text, run a small loop..

[blue]Dim [/blue]i [blue]As Integer
For [/blue]i = 0 [blue]To [/blue]1
[blue]If [/blue]txtQuantityRemaining(Index).Text = vbNullString [blue]Then
[/blue]MsgBox &quot;Please Fill in the Quantity Remaining.&quot;
[blue]End If
Next [/blue]i

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top