Results 1 to 8 of 8

Thread: Count occurrences

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Location
    Colorado
    Posts
    23

    Arrow

    Could anyone help with the code for the following event? Thanks.

    Count Occurrences
    Write code to count the occurrences of "txtEntry2" in "txtEntry1" when the user clicks the button "cmdCount". Display the result in the label "lblResult". For example, entries of

    "This is some text this is" and "is"

    would cause the following text to appear in the label "lblResult" when the user presses the button "cmdCount".

    "Occurrences Found : 4"

    Clear the result display.

    The count should NOT be case-sensitive.

    Remove leading spaces from the text before reversing it.

    Check that the user has entered at least one character in both text boxes.

    Save the project.

    Run several tests.

    Hints:
    use Mid and Instr functions among others

  2. #2
    Guest


    Code:
    Dim iCount As Integer
    
    Private Sub Form_Load()
    iCount = 0
    End Sub
    
    Private Sub cmdCount_Click()
    iCount = iCount + 1
    lblResult = iCount
    End Sub
    I'm sorry, got lost with the rest of the group .

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Location
    Colorado
    Posts
    23
    Won't this just count the occurences of characters in the first text box? I need to count the occurrences of what is in the first text box based on what the user enters in the second text box. In other words, they may pick a string from the first text box which is placed in the second text box and want to count the occurrences of that string in the first text box and place that number in the label box.

  4. #4
    Guest
    lblResult = Len(txtEntry2)

    We may have to do this step by step. It's easy, but I am confused, maybe I'm just tired .

  5. #5
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372

    Is this what you're looking for?

    Code:
    Private Sub cmdCount_Click()
    Dim I As Integer, Occur As Integer, tempstr As String
    
    If Len(txtEntry1.Text) = 0 Or Len(txtEntry2.Text) = 0 Then
        MsgBox "Evil shall not prevail!"
        Exit Sub
    End If
    
    For I = 1 To Len(txtEntry1.Text)
        tempstr = Mid(txtEntry1.Text, I, Len(txtEntry2.Text))
        If StrComp(tempstr, txtEntry2.Text, 1) = 0 Then Occur = Occur + 1
    Next I
    lblResult = Occur
    End Sub

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Location
    Colorado
    Posts
    23
    Thanks for the help Matthew and Marnitzg. That worked with only one question yet. I need to have the string "Occurrences Found :" placed in the label box as the result of occur is placed there. Any thoughts?

  7. #7
    Guest
    You mean this?
    Code:
    lblResult = "Occurrences Found: " & Occur

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Location
    Colorado
    Posts
    23
    That worked...thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width