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

How to display how many times a letter is entered in a text box 2

Status
Not open for further replies.

VbAche

Programmer
Nov 28, 1999
8
0
0
US
I am a student, just starting out in VB. I need help with a particular homework assignment. I have to write a program that prompts the user to enter from one to twelve alphabetic characters in a text box. Then the program should print as output each letter that was actually entered and the number of times it was entered. I need help with the code. I am using Do While loops, and a table, but it just isn't working. <br>
Please help. <br>

 
The following source code was posted last week on :<br>
<A HREF=" TARGET="_new"> This is a subpage of the DEVX<br>
website that covers numerous programming languages. It also<br>
has archives. A good source for information in the future.<br>
<br>
Modify the following code as you see fit to accomplish<br>
your task. Done in VB5. Good luck !<br>
<br>
Jim - aka Logit<br>
<br>
'Start a default project with FORM1 ... add one<br>
'Command1 button and three text boxes named<br>
'cnt1, cnt2 & cnt3. Paste the following code<br>
'and run. The function will locate and count the<br>
'number of occurences in the string &quot;S&quot; of the<br>
'characters indicated.<br>
<br>
Public Function DCount(ByVal vData As String, _<br>
SP As String) As Integer<br>
Dim x As Integer<br>
Dim n As Integer<br>
If vData = &quot;&quot; Or SP = &quot;&quot; Then Exit Function<br>
vData = Trim(vData)<br>
n = 1<br>
DCount = 1<br>
Do<br>
x = InStr(n, vData, SP, vbTextCompare)<br>
If x &gt; 1 And x &lt; (Len(vData) - Len(SP)) _<br>
Then<br>
DCount = DCount + 1<br>
End If<br>
n = x + Len(SP)<br>
Loop Until x = 0<br>
End Function<br>
Private Sub Command1_Click()<br>
s = &quot;GTL-00030/22*M121222*C001&quot;<br>
<br>
cnt1.Text = DCount(s, &quot;*&quot;) '-&gt; cnt=3<br>
cnt2.Text = DCount(s, &quot;/&quot;) '-&gt; cnt=2<br>
cnt3.Text = DCount(s, &quot;0&quot;) '-&gt; cnt=6<br>
End Sub
 
Hi VbAche,<br>
<br>
I have an alternative solution that is a little less complicated and I think it still produces the result you are looking for. You will need to use a few InStr and Mid functions, some for/next loops, and a few If/Then statements. ZBeing a novice myself I can't guarantee that it is exactly what you are looking for but I tested the code and it seems to produce the results you are looking for. Here it is with some explinations :)<br>
<br>
Private Sub cmdButton_Click()<br>
Dim word As String, i As Integer, x As Integer, n As Integer<br>
word = UCase(Trim(txtTextBox.Text))<br>
'Gets word from textbox without any spaces and all Uppercase<br>
For i = 65 To 90 'ANSI values of A to Z<br>
x = 0 'Resets counter to 0<br>
If InStr(word, Chr(i)) &gt; 0 Then<br>
'Checks to see that letter is present <br>
For n = 1 To Len(word) <br>
If Mid(word, n, 1) = Chr(i) Then<br>
x = x + 1<br>
End If<br>
Next n<br>
picOutput.Print &quot;The letter &quot; & Chr(i); &quot; appears&quot;; x;<br>
picOutput.Print &quot;time(s) in your word.&quot;<br>
picOutput.Print<br>
End If<br>
Next i<br>
End Sub<br>
<br>
The ANSI characters make it easy to check the alphabet.<br>
Hope this helps :)<br>
<br>
Thanks,<br>
Aleena (Who is in shock there was a post she could answer..hehe)<br>
<br>
p.s. Isn't it annoying that the tabs don't work.. bah!
 
this is an easier way to do exactly what you want.<br>
use it as a function<br>
<br>
use this code to find you number<br>
<br>
dim lngResult as long<br>
lngResult = CountOccurrences( text1.text,&quot;t&quot;,)<br>
the lngResult = the number of times &quot;t&quot; is found <br>
in text1's text<br>
<br>
<br>
Public Function CountOccurrences( _<br>
strIn As String, _<br>
strFind As String) _<br>
As Long<br>
' Comments : Returns the number of times <br>
' a string appears in a string<br>
' Parameters: strIn - String to search in<br>
' strFind - String to search for<br>
' Returns : Number of occurrences<br>
'<br>
<br>
Dim lngPos As Long<br>
Dim lngWordCount As Long<br>
<br>
On Error GoTo PROC_ERR<br>
<br>
lngWordCount = 1<br>
<br>
' Find the first occurrence<br>
lngPos = InStr(strIn, strFind)<br>
<br>
Do While lngPos &gt; 0<br>
' Find remaining occurrences<br>
lngPos = InStr(lngPos + 1, strIn, strFind)<br>
If lngPos &gt; 0 Then<br>
' Increment the hit counter<br>
lngWordCount = lngWordCount + 1<br>
End If<br>
Loop<br>
<br>
' Return the value<br>
CountOccurrences = lngWordCount<br>
<br>
PROC_EXIT:<br>
Exit Function<br>
<br>
PROC_ERR:<br>
MsgBox &quot;Error: &quot; & Err.Number & &quot;. &quot; & Err.Description, , _<br>
&quot;CountOccurrences&quot;<br>
Resume PROC_EXIT<br>
<br>
End Function<br>
<br>
tony brenke<br>
<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top