count specific word in multiline textbox

Posted on 2008-10-19
Last Modified: 2012-05-05
I just need to count how many times a specific word shows up in a multiline textbox. It only occurs once per line. The code only counts the first line and stops and can't figure out how to make it CR without locking up my prog. I am not against using different code, so if it is not possible with this one something else would be fine. I am open to suggestions. There could be anywhere from 1 to 100+  lines in the textbox, it changes depending upon input.
Dim Total As Integer

   Dim Index1 As Integer

   Dim OIndex

   Index1 = 1

   While Index1 >= OIndex

       Index1 = InStr(Index1, Form.TextBox1.text, "SEARCHWORD") + 1

       If Index1 > OIndex Then

         Total = Total + 1

         OIndex = Index1

       End If


Open in new window

Question by:guacolizard
  • 2
  • 2
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 22753820
Dim Count As Long
Count = (Length(Form.TextBox1.text) - Length(Replace(Form.TextBox1.text, "SEARCHWORD", ""))) / Length("SEARCHWORD")


Author Comment

ID: 22753846
I am getting a sub or function not defined error with "length"...what should I dim it as?
LVL 81

Accepted Solution

zorvek (Kevin Jones) earned 250 total points
ID: 22753877
Dim Count As Long
Count = (Len(Form.TextBox1.text) - Len(Replace(Form.TextBox1.text, "SEARCHWORD", ""))) / Len("SEARCHWORD")


Author Closing Comment

ID: 31507657
Thank you, works awesome!
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22754123
Here is an example of doing it like you originally envisioned:
    Private Function GetCount(ByVal strToSearch As String, ByVal searchFor As String) As Integer

        Dim startAt As Integer

        Dim foundAt As Integer

        startAt = 1


            foundAt = InStr(startAt, strToSearch, searchFor)

            If foundAt <> 0 Then

                GetCount = GetCount + 1

                startAt = foundAt + Len(searchFor)

            End If

        Loop While foundAt <> 0

    End Function

Open in new window


Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now