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

Producing a Word Index From a Text Box

Status
Not open for further replies.

JARIEL

Technical User
Nov 15, 2000
2
US
Could you please help me with the following problem! The VB assignment is as follows, (I'VE ENCLOSED WHAT I HAVE DONE THUS FAR), presumably all it requires it some fine tuning, as I believe the bulk of the work is done.

(WHAT WOULD REALLY BE USEFUL IS ATTACH THE VB FILES, IS THAT POSSIBLE?)

1. To open and read in text files, so that they can be viewed in a text box.

2. By pressing a command button the words contained in the text box have to be viewed in a list box in alphabetical order.

3. Beside each word has a number(s), representing on what line(s) that word has occured.

4. For a word which has occured previously in the same text file, it obviously does not need to create a new instance of that word as one should exist.

(The following pseudo-program should further illustrate the task)
----------------------------------------------------------
A: Click on a command button to open a file, e.g. a:\data.txt

B: TEXT BOX
In the dark dark woods there
was a dark dark house in the
dark dark house there was a
dark dark cupboard.

C: Click on another command button to order the words in the list box alphabetically and also displaying on what line(s) each word has occured.

D: LIST BOX (Word Index)
------------------------
A 2,3
CUPBOARD 4
DARK 1,1,2,2,3,3,4,4
HOUSE 2,3
IN 1,2
THE 1,2
THERE 1,3
---------------------------------------------------------------
At present when running the program the following happens:
When clicking the 'OPEN' command button a file (e.g. a:\Data.txt) opens up and is displayed in the text box (no problems there!). When clicking the execute button a 424 run-time error is displayed. Had all gone well the user should be able to click the 'Display Index' command button to display the results in the list box. When I click on debug button to identify the problem it points to one of two sub routines, 'Execute' or 'Space'.
---------------------------------------------------
CURRENT VERSION OF CODE (P.S I have VB6.0 installed)
---------------------------------------------------

Dim characters, DisplayText As String 'Declare all global
Dim Length, Newline, i, flag1, origin As Integer 'variables

Private Sub OPEN_Click()
FileName = InputBox("Enter File Name")
If FileName = " " Then Exit Sub
Open FileName For Input As #1
Text1.Text = Input(LOF(1), #1)
Close #1
End Sub

Private Sub space() 'Subroutine if charcter read
Text.SelStart = i - Length 'is a space. Selects where to
Text.SelLength = Length - 1 'start reading the text file, the
DisplayText = Text.SelText 'number of characters to extract &
End Sub

Private Sub additem() 'Subroutine for adding words to the
If flag1 = 1 Then
words.Recordset.MoveFirst
Do While Not words.Recordset.EOF
DisplayText = words.Recordset.Fields("word").Value
DisplayNum = words.Recordset.Fields("line number").Value
DisplayText = DisplayText + DisplayNum
List1.additem DisplayText
words.Recordset.MoveNext
Loop
'wordlist & sorts alphabetically using
End If
End Sub 'word list property

Private Sub Command1_Click()
Call additem
End Sub

Private Sub Command2_Click()
List1.Clear
End Sub

Private Sub delete_Click()
If flag1 = 1 Then
words.Recordset.MoveFirst
Do While Not words.Recordset.EOF
words.Recordset.Delete
words.Recordset.MoveNext
Loop
words.Recordset.MoveFirst
End If
flag1 = 0
End Sub

Private Sub Execute_click() 'subroutine when button is pressed
flag1 = 1
Dim chars, Intext As String

Intext = Text1.Text

Lenstr = Len(Intext) 'initialise variables
Length = 1
Newline = 1
For i = 1 To Lenstr
chars = Mid(Intext, i, 1) 'extract character from string

If Asc(chars) = "13" Then 'if charcter = return character

Text.SelStart = i - Length 'Select where to begin reading
Text.SelLength = Length - 1 'text file, extract characters
DisplayText = Text.SelText 'assign to 'Display text'
Call search 'call additem procedure'
Newline = Newline + 1 'update line number'
Length = 0

ElseIf chars <> &quot; &quot; Then
Length = Length + 1


Else
Call space
Call search
Length = 1

End If

Next i

Call space
Call search

End Sub




Private Sub search()
Dim strSearchFor As String, intFound As Integer
Let strSearchFor = UCase(DisplayText)
If Len(strSearchFor) > 0 Then


Let intFound = 0
Do While intFound = 0 And Not words.Recordset.EOF
If UCase(words.Recordset.Fields(&quot;word&quot;).Value) = strSearchFor Then
Let intFound = 1
words.Recordset.Edit
Let words.Recordset.Fields(&quot;line number&quot;).Value = words.Recordset.Fields(&quot;line number&quot;).Value & &quot;,&quot; & Newline
words.Recordset.Update
Else
words.Recordset.MoveNext
End If
Loop
If intFound = 0 Then
words.Recordset.AddNew
Let words.Recordset.Fields(&quot;word&quot;).Value = strSearchFor
Let words.Recordset.Fields(&quot;line number&quot;).Value = Newline
words.Recordset.Update
End If
End If

words.Recordset.MoveFirst

End Sub
 
could you please tell me what line the debug button brings you to (there should be a little yellow arrow next to it). and what exactly the error message is.

thanks
Karl Pietri
lordhuh.pota.to

 
TO: LORDUH (Programmer)

Firstly thanks for responding so quickly. As requested I've stated on what line the error occurs, and the procedures leading up to it:

1. Press the OPEN Command button, when a box is displayed to type in the text file you want to display. In this case it' a:\data.txt.

2. This is displayed in the text box.

3. Next I click on the Execute command button, when the error caption is displayed. It reads, 'Run-time error 424 Object Required'

4. I then press the 'Debug' button where the yellow arrow points to the following line of code:

Private Sub space()
Text.SelStart = i - Length
 
i think that the variable 'i' is not getting passed try passing it like this

Private Sub space(i as integer) 'Subroutine if charcter read
Text.SelStart = i - Length 'is a space. Selects where to
Text.SelLength = Length - 1 'start reading the text file, the
DisplayText = Text.SelText 'number of characters to extract &
End Sub

Private Sub Execute_click() 'subroutine when button is pressed
flag1 = 1
Dim chars, Intext As String
....

....
Else
Call space i
Call search
Length = 1
End If
.....
End Sub


i think this should work
althought
call space i
may infact have to be
call space(i)

hope that helps
Karl Pietri
lordhuh.pota.to

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top