Ok I have a list box and a label...I wish to do the following.

I want to click on a object in the list box and I want information from a .txt file to show up in the label, I need different information for each object in the list box with it clearing each time i click something new in the list box...i have tried several codes and have been trying for hours..anyone able to help me out here

I have this code so far

Private Sub lstteams_Click()
Dim FF As Long
Dim LineOfText As String
FF = FreeFile
Open "C:\Documents and Settings\Me\Desktop\New Folder\stats.txt" For Input As #FF
Do While Not EOF(FF)
Line Input #FF, LineOfText

If Len(LineOfText) Then List1.AddItem LineOfText
Loop
Close #FF
End Sub

Now what I want is, I want to click the next object in the listbox and clear the stats.txt file and have like another file come up instead like stats2.txt ect.. Please help me

Hi

I am attaching a sample program which initially loads all the file name into a List Box and then when you click on each item in the List Box it opens up the specified text file and display it in a Label.

Sample code is shown below. Attachment has the complete program.

Private Sub List1_Click()
Dim i As Integer

If (FExists("C:\sample\" + List1.Text + ".txt") = True) Then
ReadFromFile ("C:\sample\" + List1.Text + ".txt")
End If



    For i = 1 To TotalLine
    Label1.Caption = FileText(i)
    Next i

End Sub

Marikanna

---------------------------------------------------------------------

Success is a Journey, not a Destination

Ok I have got it so you click on one and the file.txt opens...but I want it now so you click on the next object in the listbox and it gets rid of file.txt and opens up file2.txt in the label
and another thing...

It will only add one line in the .txt file...say the file looks like this

Testing
Hello This Is A Test
This Should Work

It will only put "This Should Work" down no matter what I do, unless I delete it and move "Hello This Is A Test" down a few lines

Ok I have got it so you click on one and the file.txt opens...but I want it now so you click on the next object in the listbox and it gets rid of file.txt and opens up file2.txt in the label
and another thing...

It will only add one line in the .txt file...say the file looks like this

Testing
Hello This Is A Test
This Should Work

It will only put "This Should Work" down no matter what I do, unless I delete it and move "Hello This Is A Test" down a few lines

Hi

Change all the file path to C:\sample\sample\ . It was previously written C:\sample .

It should work now.

Marikanna

Yeah that will fix your sample problem, but my problem is, I need it so you click on the next object in the listbox and it gets rid of the file.txt and puts in file1.txt in the listbox and so on...and also with the .txt files it is only puting 1 line in the label

Hi

This will solve the muti line display problem in the label. Still I couldn't get a correct picture of what you are loading in the list box when the form is loaded and what it should do when you click on an item in the list box, basically the relationship between the listbox and the items in the list box.

Quote from your reply
"I need it so you click on the next object in the listbox and it gets rid of the file.txt and puts in file1.txt in the listbox and so on..."

Private Sub List1_Click()

Dim i As Integer
Label1.Caption = ""
If (FExists("C:\sample\" + List1.Text + ".txt") = True) Then
ReadFromFile ("C:\sample\" + List1.Text + ".txt")
End If

    For i = 1 To TotalLine
    Label1.Caption = Label1.Caption + FileText(i) + vbCrLf
    Next i

End Sub

Marikanna

Ok thanks that got one problem solved...now..

What that does is, you click on any of the items in the list which I have 15 items in my listbox...it makes the list.txt file appear in the label all fine...what I want is the list.txt file to appear in the label when you click on the 1st item in the list box, but when you click on the 2nd item the list.txt is replaced with list1.txt in the label and so on...hope that clears it up with this

Hi

I hope I understood the problem. So you have text files like File1, File2, File3, File4 and in the List box say you have A,B,C,D,E. Now when you click on A, File1 should show up in the label and for B it should show up File2...and so on. I guess this is what the problem is...


Check out the attachement.

Marikanna

ok, that is basically doing the same thing, I click on say "A" then it opens up file1.txt I click on "B" then file1.txt is still open. I want file1.txt to go away and file2.txt to replace it in the label when "B" is clicked on ect.

Hi

I got a doubt. When you run the project in the sample folder and then click on A(item in the list box) it shows up the contents of file1.txt in the Label. And then when u click on B is the Label showing the contents of file2.txt? or no ?

Its coded to show the specified file when u click on the item and the Label1.Caption = "" will clear the Label(Old file contents) before displaying the next file.

Also can u please explain whether u want to close the old file when u click on the new one.

Marikanna

Yes I wish to close the old file when you click on the new one

Option Explicit

Dim strFileName As String
Dim wmWriteMode As WMode
Dim strFileText() As String
Dim intTotalLine As Integer

Public Enum WMode
  AddToFile = 1
  NewFile = 2
End Enum


Private Sub Form_Load()
Dim i As Integer
WriteMode = NewFile

If (FExists("C:\Documents and Settings\Me\Desktop\New Folder\Stats\file.txt") = True) Then
ReadFromFile ("C:\Documents and Settings\Me\Desktop\New Folder\Stats\file.txt")
End If

    For i = 1 To TotalLine
      lstteams.AddItem FileText(i)
    Next i
    

End Sub
Public Function FExists(OrigFile As String)

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
FExists = fs.fileexists(OrigFile)

End Function
Public Function ReadFromFile(Optional ByVal FileName As String) As String
  On Error GoTo ErrHandle
  Dim FileNo As Integer
  Dim Counter As Integer
  Dim ReturnText As String
  
  If FileName <> "" Then
    strFileName = FileName
  End If
  Counter = 0
  FileNo = FreeFile
  
  Open strFileName For Input As FileNo
  Do Until EOF(FileNo)
    ReDim Preserve strFileText(Counter + 1)
    Line Input #FileNo, strFileText(Counter)
    
    If ReturnText <> "" Then ReturnText = ReturnText & vbCrLf
    ReturnText = ReturnText & strFileText(Counter)
    Counter = Counter + 1
  Loop
  intTotalLine = Counter
  Close FileNo
  ReadFromFile = ReturnText
  Exit Function
ErrHandle:
  MsgBox Err.Number & " : " & Err.Description
End Function

Public Property Get FileText(LineNumber As Integer) As String
  FileText = strFileText(LineNumber - 1)
End Property

Public Property Get TotalLine() As Integer
  TotalLine = intTotalLine
End Property

Private Sub Class_Initialize()
  wmWriteMode = AddToFile 'Sets the default value of WriteMode property
End Sub

Public Property Get FileName() As String
  FileName = strFileName
End Property

Public Property Let FileName(ByVal FName As String)
  strFileName = FName
End Property

Public Property Get WriteMode() As WMode
  WriteMode = wmWriteMode
End Property

Public Property Let WriteMode(ByVal WrMode As WMode)
  wmWriteMode = WrMode
End Property



Private Sub lsttone_Click()

Dim i As Integer
Dim item As Integer

item = lstteams.ListIndex + 1

Label1.Caption = ""
If (FExists("C:\Documents and Settings\Me\Desktop\New Folder\Stats\broncos.txt" & item & "file.txt") = True) Then
ReadFromFile ("C:\Documents and Settings\Me\Desktop\New Folder\Stats\broncos.txt" & item & "file.txt")
End If



    For i = 1 To TotalLine
    Label1.Caption = Label1.Caption + FileText(i) + vbCrLf
    Next i


End Sub

That is the code I have I might of stuffed it up somewhere but not sure.

I have found another error...with the code I have recently put up the file.txt is also in the listbox aswell as the other list of things I have in the List in the property menu

Hi


Try this code. It will close the file.

Private Sub Form_Load()

Open "C:\sample\List.txt" For Input As #1
While Not EOF(1)
Line Input #1, temp$
List1.AddItem (temp$)
Wend
Close #1

End Sub
Private Sub List1_Click()

Dim i As Integer
Dim item As Integer

item = List1.ListIndex + 1
Label1.Caption = ""

Open "C:\sample\File" & item & ".txt" For Input As #1
While Not EOF(1)
Line Input #1, temp$
alltext$ = alltext$ & temp$ & vbCrLf
Wend
Label1.Caption = alltext$
Close #1

End Sub

I get a compile error variable not defined

Private Sub Form_Load()
Open "C:\list.txt" For Input As #1
While Not EOF(1)
Line Input #1, temp$
List1.AddItem (temp$)
Wend
Close #1
End Sub

Hi

Can you say which line its pointing to or highlighting when you click debug ? Because its running without any error in my system.


Marikanna

its the one in bold which is coming up the error..the temp$ from the Line Input #1, temp$ line

Hi

Can you please check whether you have entered Option Explicit in General Declaration or anywhere in the code. If its then please delete the Option Explicit.


Marikanna

ok now I am getting run time error '53' file not found error

Hi

Copy all the files in the sample folder and paste it in the folder currently you have the project. Also check for the file path.


Marikanna

I have checked everything, the file path is correct and everything

Private Sub Form_Load()
Open "C:\Documents and Settings\Me\Desktop\New Folder\Stats\list.txt" For Input As #1
While Not EOF(1)
Line Input #1, temp$
lstteams.AddItem (temp$)
Wend
Close #1
    

End Sub

Private Sub lstteams_Click()

Dim i As Integer
Dim item As Integer

item = lstteams.ListIndex + 1
Label1.Caption = ""

Open "C:\Documents and Settings\Me\Desktop\New Folder\Stats\file.txt" & item & "file.txt" For Input As #1
While Not EOF(1)
Line Input #1, temp$
alltext$ = alltext$ & temp$ & vbCrLf
Wend
Label1.Caption = alltext$
Close #1


End Sub

would it be easier if I did it as a combobox instead of a listbox...

Hi

Here is the correction. Change this line

"C:\Documents and Settings\Me\Desktop\New Folder\Stats\file.txt" & item & "file.txt"

in Private Sub lstteams_Click() to

"C:\Documents and Settings\Me\Desktop\New Folder\Stats\file" & item & ".txt"

This should solve the problem.

Marikanna

bahaha this is getting ridiculas (sp?) anywho now I did that I get this error when I click on a object in the listbox

Run Time Error '76'

Path Not Found

Sharpy, have you considered using a random access file to store your data? It would mean that instead of 15 files to keep track of, you would just have 1 file containing 15 records, which can be read and edited quite easily. If you're interested I can post a prog that demonstrates.

yes can you post a demostration for it that would be great

Hi Sharpy, sorry about the delay, I'm up to the neck in things I don't wanna do but have to.
First create a new folder in Program Files and call it "AddBook", open the zip file and run it. It's kind of rough & ready and could be improved on a LOT, but it serves to demonstrate random file creation and methods of searching, editing, reading, writing and display.

You could also use an access database. with a primary key on each table it would be faster and easier to manage if it gets large.

This article has been dead for over six months. Start a new discussion instead.