
An Unwanted Comma appears in TextBox
I REALLY appreciate your taking time to read-thru my long-winded problem ...!
. . . <unfortunately> ... I don't think we're "there" yet.
If you can spare the proverbial 2 minutes , please try the following :
Put a TextBox and a Label on a form ;
Paste-in the following Text1_KeyPress code.
- - - - - - - - - - - - - - - - - - - - - - - - - - -
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 44 Then ' user entered a comma : so ...
Count_Qs ' "Count" 'integers entered'
Exit Sub
ElseIf KeyAscii = 8 Then 'Allow "Backspacing".
KeyAscii = 8
Exit Sub 'and allow integers.
ElseIf KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
End If
End Sub
End Sub
- - - - - - - - - - - - - - - - - - - - - - - - - - -
Then , paste-in the following Subroutine: * * * * * *
- - - - - - - - - - - - - - - - - - - - - - - - - - -
Private Sub Count_Qs() ' "Count" 'integers entered' to Text2.
Dim T$, A$, N$ ' Text2 "Contents"
Dim P%, Ts%, Full%, Nu%
T$ = Text2.Text ' To determine the number of characters in
string, use the Len function.
Full% = Len(T$)
Open "CountBuf" For Output As #1 ' Using 'Print' instead of 'Write' lets
"Input#2,A$" grab 1 char only!
Dim T$, A$, N$
Dim P%, Ts%, Full%, Nu%
T$ = Text1.Text ' To determine the number of characters in
string, use the Len function.
Full% = Len(T$)
Open "CountBuf" For Output As #1 ' Using 'Print' instead of 'Write' lets
"Input#2,A$" grab 1 char only!
Print #1, T$ ' a Comma(,) = Ascii 44 (Chk if user
"omits" a comma
Close #1 ' i.e. a MsgBox if a 4-digit integer is
entered.)
Static Selex% ' Begin counting at Zero
Selex% = 0
Dim TblRecz% ' Here for Test only (GlobalVar)
TblRecz% = 150 ' For TEST only ( determined by Recordset count elsewhere...)
Open "CountBuf" For Input As #2
Do While Not EOF(2)
Input #2, A$
If Val(A$) > TblRecz% Then ' user entered a # > highest Q# in
table.
MsgBox ("The number entered exceeds the highest-order Q.# in the
available list. Deleting.")
Screen.MousePointer = 11 ' Hourglass (for "slowpokes")
P% = Len(A$)
Ts% = Full% - P%
N$ = Left(T$, Ts%)
Nu% = Len(N$)
If Asc(N$) = 44 Then ' Asc returns 44 IF first
'character is a comma
N$ = Right(N$, Nu% - 1)
'... If so, Trim-it-off.
End If
Text1.Text = N$
Screen.MousePointer = 1 ' no more Hourglass
Exit Do
End If
Selex% = Selex% + 1
Loop
Label1.Caption = Selex%
Close #2
End Sub
- - - - - - - - - - - - - - END * * * * *
Run the "Test" .
In Text1 , type the following :
1,3,4,6,8,367, <Don't forget final comma!
and you'll see "Exactly" what's happening.
PS: the "367" can be anything greater than 150 (in this case).
I have another label explaining to the user to put commas between
each entry, and after the final selection...(which is "reinforced" by
tooltip prompt on the textbox too).
Quote:
> This won't work. If N$ = a comma, then len(N$) = 1. Right$(N$, len-1) then
> means Right$(N$,0) which does nothing. Make it: If N$ = "," then N$=""
> John Tegelaar
> >The "scenario" I'm dealing with is nearly identical to the 'Print Dialog
> >Box / Pages to Print" entry text box.
> <SNIP>
> > P% = Len(A$)
> > Ts% = Full% - P%
> > N$ = Left(T$, Ts%)
> > Nu% = Len(N$)
> > If Asc(N$) = 44 Then '*********
> > N$ = Right(N$, Nu% - 1) '*****
> > End If
> <SNIP>