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

Selecting Diff Files from FileListBox

Status
Not open for further replies.

tomhughes

Vendor
Aug 8, 2001
233
0
0
US
I am working on a program that operates on different files.
After selecting the first file, the program operates on the file, but when I change the file seletion from the FileListBox, the program does not select the next file I click on. I have to completely close out the program and re-open it to select another file from the list box. I am using this code .

Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub


Private Sub File1_Click()

End Sub

 
What other code do you have that operates on the selected file?

At the moment, it won't do anything if you change the file in the FileListBox?

Cheers

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
I open the file with this code

Open Dir1.Path & "\" & File1.FileName For Input As #iFreeFile

Then I put it into an array, and close it.
 

It may be helpfull to see the FileListBox's Click event code:

Code:
Private Sub FileListBox_Click()
[red][b]... ??? [/b][/red]
End Sub

Have fun.

---- Andy
 

OK......

If you would answer HarleyQuinn's first question, we may be able to help you.


Have fun.

---- Andy
 
Here is the code that puts the file into an array


Private Sub LoadFile()

Dim x As Integer
Dim iFreeFile As Integer
iFreeFile = FreeFile
Open Dir1.Path & "\" & File1.FileName For Input As #iFreeFile
Do While Not EOF(iFreeFile)
' 'x' is our counter here. We will
If x Mod 25 = 0 Then
If x = 0 Then
ReDim FileItems(25)
Else

ReDim Preserve FileItems(UBound(FileItems) + 25)
End If
End If

Line Input #iFreeFile, strLine
If strLine <> " " And strLine <> "" Then
FileItems(x) = strLine
'Debug.Print FileItems(x)
End If

x = x + 1

Loop
ReDim Preserve FileItems(x)

bIsLoaded = True

End Sub
 

Your problem is - you overwrite your array.

Whatever you put in it the first time, you wipe it out and write to it again.

This is what I have out of your code:
Code:
Option Explicit
Dim bIsLoaded As Boolean    [green]'Added[/green]
Dim FileItems() As String   [green]'Added[/green]

Private Sub Command1_Click()
Call LoadFile
End Sub

Private Sub Form_Load()
File1.Path = "c:\Text"
End Sub

Private Sub LoadFile()
    Dim strLine As String   [green]'Added[/green]
    Dim z As Integer        [green]'Added[/green]
    [blue]Static[/blue] x As Integer
    Dim iFreeFile As Integer
    
    iFreeFile = FreeFile
    
      Open [blue]"c:\Text\"[/blue] & File1.FileName For Input As #iFreeFile
        Do While Not EOF(iFreeFile)
            [green]' 'x' is our counter here. We will[/green]
            If x Mod 25 = 0 Then
                If x = 0 Then
                    ReDim FileItems(25)
                Else

                    ReDim Preserve FileItems(UBound(FileItems) + 25)
                End If
            End If
            
            Line Input #iFreeFile, strLine
            If strLine <> "  " And strLine <> "" Then
                FileItems(x) = strLine
                [green]'Debug.Print FileItems(x)[/green]
            End If

             x = x + 1
            
        Loop
     [green]'ReDim Preserve FileItems(x) 'Commented out[/green]
    
    bIsLoaded = True
    
    [green]'Added code[/green]
    For z = LBound(FileItems) To UBound(FileItems)
        If Len(FileItems(z)) > 0 Then
            Debug.Print FileItems(z)
        End If
    Next z
End Sub

Have fun.

---- Andy
 
OK Andrzejek Thanks for the correction. Now how do I change the file that I select in the FileListBox ???
 
After I select the file from the FileListBox, I press a Command button that puts the file into an array, and operates on the array, then closes the file. When I try to select another file from the FileListBox, it does not select the other file, but selects the same file I selected initially.
 

When I run the code, it was selecting the file which ever I have selected from File1 (FileListBox) and it worked OK.

See it in code:
Code:
Private Sub File1_Click()
   Me.Caption = File1.FileName
End Sub
and the Caption of the Form will show you selected File name.

Got to go home now, it is 3:27 pm

Have fun.

---- Andy
 
Does anyone know how to select a different file in the FileListBox without closing the program ???
 

When I run the code from my last post, when I click on the file name in the File1 - the Caption of my Form displays the name of the file I have chosen from File1. So, by clicking on the file name in File1 I can choose any file from it.

You should, too, unless somewhere in the code you point to first file in File1 and do not allow to change it by clicking on another file name.

Have fun.

---- Andy
 
Andrzejek

Thanks for your response.
Yes I can also get the same results as you, however when I try to incorporate this into the File Open statement, the program still operates on the same file I initially opened, and not the last one I selected from the FileListBox. Here is the code I tried.

Open Dir1.Path & "\" & Me.Caption For Input As #iFreeFile

It works fine the first time I select it, but after the program runs, and I select another file, it just operates on the same file I initially selected. I have closed out all files in the last subroutine, to be sure no files are still open when the program completes.

 
Well, it must be something on your side, baceuse if I start a new VB project, place just a Command1 and File1 (FileListBox) on the Form1, copy the code from my previous post from 16 Oct 07 15:03 and run it - it works great.

On my C:\ drive I have a folder Text, and in it 3 simple text files:
One.txt:
One 1
One 2
One 3

Two.txt
Two 1
Two 2
Two 3
Two 4
Two 5

and Three.txt.
Three 1
Three 2
Three 3
Three 4

And when I run it, in my Immidiate Window in VB:
I click on Three.txt, I get:
Three 1
Three 2
Three 3
Three 4
(I delete all in Imm Window)

Then I click on Two.txt, I get:
Three 1
Three 2
Three 3
Three 4
Two 1
Two 2
Two 3
Two 4
Two 5
(I delete all in Imm Window)

Then I click on One.txt, I get:
Three 1
Three 2
Three 3
Three 4
Two 1
Two 2
Two 3
Two 4
Two 5
One 1
One 2
One 3

Try it.


Have fun.

---- Andy
 
Andrzejek
Yes - I think you are correct. I am beginning to believe the problem I am having is not the FileListBox, but the Array that I am writing the file to. I don't think the program is overwriting the array, so I am working on either erasing the array, or overwriting it with null data. I appreciate all your help.
 

Your best way to solve your problem is to put a few break points in your code and step thru it (F8) and see what's going on.


Have fun.

---- Andy
 
Andrzejek
Thanks for your help. I finally figured out what was wrong. I assumed the origonal array would be overwritten after the program completed, however it was not, and the next file I selected from the FileListBox was not written to the array, but the origonally selected file was still there. Therefore it appeared that the next file was not being read, but in fact the array was not updated with new information.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top