Announcements

Announcement: We’re aware of an issue affecting starting a new topic from category pages and creating new blog posts. New topics can still be started directly from the relevant board. Learn more here.

populating a listbox with text in a txt file.

populating a listbox with text in a txt file.

Anonymous
Not applicable
452 Views
7 Replies
Message 1 of 8

populating a listbox with text in a txt file.

Anonymous
Not applicable
(im learning)
I would like my vb prog to read a txt file and populate a listbox.
The items in a txt file will be added to from time to time.

I appreciate your help.
thanks
0 Likes
453 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
Assuming each line in the text file is to become one line in a list box:

Create a file called "sample.txt" and save it in your TEMP folder on C:
drive. Here's the data I used (you can use whatever you like):

Line 1
Line 2
Line 3
Line 4
Line 5

Enter the VBAIDE, add a UserForm. Place a listbox on the UserForm and place
this code in the UserForm's Initialize event (fair warning; there's *no*
error checking):

fh = FreeFile
Open "c:\temp\sample.txt" For Input As #fh

Do While Not EOF(fh)

Line Input #fh, tmp
ListBox1.AddItem tmp

Loop
Close fh

Run the form and the contents of the text file will be placed in the list
box.

--
Attitudes are contagious. Is yours worth catching?
http://www.acadx.com

Jeremy wrote in message
news:[email protected]...
> (im learning)
> I would like my vb prog to read a txt file and populate a listbox.
> The items in a txt file will be added to from time to time.
>
> I appreciate your help.
> thanks
>
0 Likes
Message 3 of 8

Anonymous
Not applicable
Thanks, I was hoping someone else was awake out there.
Frank Oquendo wrote in message
news:[email protected]...
> Assuming each line in the text file is to become one line in a list box:
>
> Create a file called "sample.txt" and save it in your TEMP folder on C:
> drive. Here's the data I used (you can use whatever you like):
>
> Line 1
> Line 2
> Line 3
> Line 4
> Line 5
>
> Enter the VBAIDE, add a UserForm. Place a listbox on the UserForm and
place
> this code in the UserForm's Initialize event (fair warning; there's *no*
> error checking):
>
> fh = FreeFile
> Open "c:\temp\sample.txt" For Input As #fh
>
> Do While Not EOF(fh)
>
> Line Input #fh, tmp
> ListBox1.AddItem tmp
>
> Loop
> Close fh
>
> Run the form and the contents of the text file will be placed in the list
> box.
>
> --
> Attitudes are contagious. Is yours worth catching?
> http://www.acadx.com
>
> Jeremy wrote in message
> news:[email protected]...
> > (im learning)
> > I would like my vb prog to read a txt file and populate a listbox.
> > The items in a txt file will be added to from time to time.
> >
> > I appreciate your help.
> > thanks
> >
>
0 Likes
Message 4 of 8

Anonymous
Not applicable
Can you explain to me why you used the "#fh" instead of #1 or any other number for that matter?

Would it work the same if it were #1, #2, etc.?
0 Likes
Message 5 of 8

Anonymous
Not applicable
You generally would use a style similar to the following:

Dim File As Integer

'get a handle to file
File = FreeFile

'write the Text to file
Open strFilename For Append Access Write As #File
Write #File, "Text objects"

'write each Text object to file
For Each objAcadText In objSelectionSet
Write #File, TextString
Next objAcadText

Close #File

Personally, I would opt using the Microsoft FileSystemObject for tasks such
as this.

Joe ...


wrote in message news:[email protected]...
Can you explain to me why you used the "#fh" instead of #1 or any other
number for that matter?

Would it work the same if it were #1, #2, etc.?
0 Likes
Message 6 of 8

Anonymous
Not applicable
And the opposite would be the following:

Dim File As Integer
Dim TextLine As String

File = FreeFile

Open strFilename For Input Access Read As #File
Do While Not EOF(File)
Line Input #File, TextLine
MsgBox TextLine
Loop
Close #File

Joe ...


"Joe Sutphin" wrote in message
news:[email protected]...
You generally would use a style similar to the following:

Dim File As Integer

'get a handle to file
File = FreeFile

'write the Text to file
Open strFilename For Append Access Write As #File
Write #File, "Text objects"

'write each Text object to file
For Each objAcadText In objSelectionSet
Write #File, TextString
Next objAcadText

Close #File

Personally, I would opt using the Microsoft FileSystemObject for tasks such
as this.

Joe ...


wrote in message news:[email protected]...
Can you explain to me why you used the "#fh" instead of #1 or any other
number for that matter?

Would it work the same if it were #1, #2, etc.?
0 Likes
Message 7 of 8

Anonymous
Not applicable
Hi Joe,

What do you see as the advantage of the FSO compared with the VB method?

I looked in your book for a reference to it, but found nothing.

Regards


Laurie Comerford

Joe Sutphin wrote:
> You generally would use a style similar to the following:
>
> Dim File As Integer
>
> 'get a handle to file
> File = FreeFile
>
> 'write the Text to file
> Open strFilename For Append Access Write As #File
> Write #File, "Text objects"
>
> 'write each Text object to file
> For Each objAcadText In objSelectionSet
> Write #File, TextString
> Next objAcadText
>
> Close #File
>
> Personally, I would opt using the Microsoft FileSystemObject for tasks such
> as this.
>
> Joe ...
>
>
> wrote in message news:[email protected]...
> Can you explain to me why you used the "#fh" instead of #1 or any other
> number for that matter?
>
> Would it work the same if it were #1, #2, etc.?
0 Likes
Message 8 of 8

Anonymous
Not applicable
I was surprised at that too
FSO is universally denigrated on the "real" vb groups
security issues, bloated object being stated as the two reasons not to use
it.
mark

"Laurie Comerford" wrote in message
news:[email protected]...
Hi Joe,

What do you see as the advantage of the FSO compared with the VB method?

I looked in your book for a reference to it, but found nothing.

Regards


Laurie Comerford

Joe Sutphin wrote:
> Personally, I would opt using the Microsoft FileSystemObject for tasks
> such
> as this.
>
> Joe ...
>
0 Likes