473,388 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How can I count the nmber of occurances of a word in a text file using Access VBA?

Hi. I would like to know how to count the number of occurances of a word in a text file.

I am using Microsoft Access VBA.

Any ideas?
Jul 8 '10 #1
7 8275
ADezii
8,834 Expert 8TB
@vgnadeau
I'll keep the Logic parallel to your previous Thread:
Expand|Select|Wrap|Line Numbers
  1. 'Set a Reference to the Microsoft Scripting Runtime Library
  2. Dim objFSO As FileSystemObject
  3. Dim ts As TextStream
  4. Dim strText As String
  5. Dim strSearchText As String
  6. Dim strFileName As String
  7. Dim lngPos As Long
  8. Dim lngStringCount As Long
  9.  
  10. strSearchText = "cb: TRINGLE"
  11.  
  12. strFileName = CurrentProject.Path & "\Test.txt"
  13.  
  14. 'Create instance of FileSystemObject.
  15. Set objFSO = CreateObject("Scripting.FileSystemObject")
  16. Set ts = objFSO.OpenTextFile(strFileName, ForReading)
  17.  
  18. 'Read entire contents of file, save to strText variable
  19. strText = ts.ReadAll
  20.  
  21. lngPos = 1
  22.  
  23. Do
  24.   lngPos = InStr(lngPos, strText, strSearchText)
  25.     If lngPos > 0 Then
  26.       lngStringCount = lngStringCount + 1
  27.       lngPos = lngPos + Len(strSearchText)
  28.     End If
  29. Loop Until lngPos = 0
  30.  
  31. MsgBox "The String [" & strSearchText & "] appears " & lngStringCount & " time(s) in the " & _
  32.        "File " & strFileName & "!"
Jul 8 '10 #2
You are awesome, thanks so much for your help!
Jul 8 '10 #3
ADezii
8,834 Expert 8TB
@vgnadeau
You are very welcome.
Jul 8 '10 #4
Ok. Last problem of the day. Now I want to be able to repeat the cycle. I've counted the numner of occurances of "cb:TRINGLE" and divided the result by 4 to give me the number of times I need to repeat the Find/replace. So, I want to replace the first and fourth insances and then the first and fourth again ...

Example:
cb:MATERIAL
cb:TRINGLE
cb:TRINGLE
cb:MATERIAL
cb:MATERIAL
cb:TRINGLE
cb:TRINGLE
cb:MATERIAL
cb:MATERIAL
cb:TRINGLE
cb:TRINGLE
cb:MATERIAL

Here's my code so far;

Expand|Select|Wrap|Line Numbers
  1. Function FindAndReplace()
  2. Dim sSearchText As String
  3. Dim sReplaceText As String
  4. Dim sFileName As String
  5. Dim lngPosition As Long
  6. Dim intNumOfMatches As Integer
  7. Dim strText As String
  8. Dim strNew As String
  9. Dim lngNewPos As Long
  10. Dim strMsg As String
  11. Dim intCount As Integer
  12.  
  13. Const ForReading = 1
  14. Const ForWriting = 2
  15.  
  16. sSearchText = "cb:TRINGLE"
  17. sReplaceText = "cb:MATERIAL"
  18.  
  19. sFileName = "C:\test.txt"
  20. intCount = CountWordOccurances(sSearchText, sFileName)
  21. intCount = intCount \ 4
  22.  
  23.  
  24. Set objFSO = CreateObject("Scripting.FileSystemObject")
  25. Set ts = objFSO.OpenTextFile(sFileName, ForReading)
  26. strText = ts.ReadAll
  27.  
  28.  
  29. lngPosition = InStr(strText, sSearchText)
  30. If lngPosition = 0 Then Exit Function
  31.  
  32. strNew = Left$(strText, lngPosition - 1) & sReplaceText & _
  33.                Mid$(strText, lngPosition + Len(sSearchText))
  34.  
  35. Starting
  36. intNumOfMatches = intNumOfMatches + 1
  37. lngNewPos = lngPosition + 1
  38.  
  39. Do
  40.   If InStr(lngNewPos, strText, sSearchText) <> 0 Then
  41.     lngPosition = InStr(lngNewPos, strText, sSearchText)
  42.     intNumOfMatches = intNumOfMatches + 1
  43.       lngNewPos = lngPosition + 1
  44.         If intNumOfMatches = 4 Then     '4th occurrence
  45.           strNew = Left$(strNew, lngPosition - 1) & "/" & sReplaceText & _
  46.                    Mid$(strNew, lngPosition + Len(sReplaceText))
  47.         End If
  48.   End If
  49. Loop Until intNumOfMatches = 4 Or lngPosition = 0
  50.  
  51. Set objFile = objFSO.OpenTextFile(sFileName, ForWriting)
  52. objFile.WriteLine strNew
  53.  
  54. objFile.Close
  55.  
  56. End Function
  57.  
  58. Function CountWordOccurances(WordToCount As String, Filename As String) As Integer
  59. Dim strText As String
  60. Dim strSearchText As String
  61. Dim strFileName As String
  62. Dim lngPos As Long
  63. Dim lngStringCount As Long
  64.  
  65. strSearchText = WordToCount
  66. strFileName = Filename
  67.  
  68. Set objFSO = CreateObject("Scripting.FileSystemObject")
  69. Set ts = objFSO.OpenTextFile(strFileName, 1)
  70.  
  71. strText = ts.ReadAll
  72.  
  73. lngPos = 1
  74.  
  75. Do
  76.   lngPos = InStr(lngPos, strText, strSearchText)
  77.     If lngPos > 0 Then
  78.       lngStringCount = lngStringCount + 1
  79.       lngPos = lngPos + Len(strSearchText)
  80.     End If
  81. Loop Until lngPos = 0
  82.  
  83. End Function
  84.  
I don't know what to do next ...
Jul 8 '10 #5
ADezii
8,834 Expert 8TB
  1. You should have clearly stated your Primary Objective up front and saved me some extra effort. I am referring to this new Cycle Requirement which I have heard for the first time.
  2. This Thread is clearly related to your initial Thread on this Topic, and you should not have split it off.
  3. The above being said, you do not need to calculate the number of occurrences of one String in another. I am referring to the Routine that I created for you which really is not needed.
  4. The simplest approach is to build the Logic into the Loop itself, namely: if a Match Number MOD 4 = 0 it gets Replaced. Match 1 is handled independently, but if a Match Number is 5, 9, 13, 17 ..., etc. it also gets replaced since it is the start of a New Cycle of 4.
  5. Study the revised Code Segment which currently allows for 6 Cycles, and download the New Attachment. Again, Unzip 'both' Files to the same Directory.
    Expand|Select|Wrap|Line Numbers
    1. Do
    2.   If InStr(lngNewPos, strText, sSearchText) <> 0 Then
    3.     lngPosition = InStr(lngNewPos, strText, sSearchText)
    4.     intNumOfMatches = intNumOfMatches + 1
    5.       lngNewPos = lngPosition + 1
    6.         If intNumOfMatches Mod 4 = 0 Or intNumOfMatches = 5 Or intNumOfMatches = 9 Or intNumOfMatches = 13 _
    7.                            Or intNumOfMatches = 17 Or intNumOfMatches = 21 Then
    8.           strNew = Left$(strNew, lngPosition - 1) & sReplaceText & " " & _
    9.                    Mid$(strNew, lngPosition + Len(sSearchText) + 1)
    10.         End If
    11.   Else
    12.     lngPosition = 0
    13.   End If
    14. Loop Until lngPosition = 0
Attached Files
File Type: zip Replace_2.zip (16.0 KB, 147 views)
Jul 9 '10 #6
Well ...
1) I did not know that I would have to 'cycle' until after I had originally posted my question. The XML file I was given was a sample. It's the sort of situation where if I ask the question I get the answer, if I don't then no one voluteers the info.
2) I was actually thinking about the usefulness of the answers for someone who was searching for a specific topic (Counting the number of occurances is not the same as find/replace).
3) I do need to calculate the number of ocurrances because I do not know how many times <cb:TRINGLE> will be in the file. I only want to change the first and fourth, first and fourth, etc. That's why I thought I should count the number of occurances and devide by 4. After that I did not know how to use this info to properly cycle through your code.

I do appreciate your help but ... well if you don't like accumulative questions from newbies perhaps you shoulden't answer them? I am learning something from each of your answers.
Jul 13 '10 #7
Yes, I have typos in my previous message. I think common sense will tell you what I meant.
Jul 13 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Simon Verona | last post by:
Not sure if this is the best group... it may be better off in one of the ADO groups, but I'm sure somebody here knows the answer: I'm trying to load up a text file using ADO.net, as follows: ...
2
by: Praveen_db2 | last post by:
Hi all Db2 8.1.3 windows Is there any way to write data into a text file using a stored procedure? The way we return a cursor output to the calling application, can we return data in a text...
2
by: asenthil | last post by:
Hai, i'm a beginner to java... just now i had tried to read and write files using java... and then i had tried to connect a database using jdbc... now i want to export the data's from a...
4
by: varsha desai | last post by:
Hello there, I want to change some data(which is in one line only) of text file using VB 6.0. Which is the best method for it? Another question is I want to delete last two, three lines...
1
by: Phil | last post by:
I want to add a button to a form to import a text delimited file. The File is delimited by "^" and so I have created a specification file by manually impoting the text file, using the advanced...
2
by: charlesbritto | last post by:
A C++ program for counting individual string in a text file using file handling functions., for ex: if the text file contains, am a boy,am studying +2,am from chennai Now the result shoud...
2
by: sajidali | last post by:
i am trying to append a text file using c#. when i executes my application i writes to the text file using more then one functions. i want to append file only during execution of programe. next time...
0
by: Thiem Teddy | last post by:
Dear all I use VB6 to export data into xls or text file, I followed this example ( http://bytes.com/topic/visual-basic/answers/530866-how-export-table-data-into-xls-text-file-using-vb6 ). but I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.