Jump to content

Visual Basic 6 help


nightsider

Recommended Posts

hi i have a some wat limmeted knowledge of visual basic "my college course doesn't teach me much lol" i need to list the contents of a text file in a list box and i cant figure out how to do it this is a program i wrote for a college assignment its an insurance calculator i have copied the list box part out

Private Sub Form_Load()

'load the following items into the list box when the form loads '

lstCrimConvic.AddItem "Yes"

lstCrimConvic.AddItem "No"

End Sub

what is it i need to put in place of the AddItem "yes"

thanking you in advance

Share this post


Link to post
Share on other sites

im not good with list boxes (dont use them much, but I think there needs to be an =

Private Sub Form_Load()

'load the following items into the list box when the form loads '

lstCrimConvic.AddItem ="Yes"
lstCrimConvic.AddItem ="No"

End Sub

Share this post


Link to post
Share on other sites

You have the construct ok in your code - listbox.AddItem "Item to Add" (no "=" sign).

 

So, what exactly is your question? Are you asking how to open a text file for reading and iterate through it and add the contents to the listbox?

Share this post


Link to post
Share on other sites

You need to go to MSDN and read up on FileSystemObject. This is what gives you access to files, directories etc.

 

This is something I threw together real quick you'll need to clean it up to make it work for you. Make sure you add a reference to the Microsoft Scripting Runtime object in your VB project and change the file name to whatever your path and file name are.

 

Sub readfile()
Dim objFSO As Scripting.FileSystemObject
Dim objTextFile As TextStream

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\testfile.txt", ForReading)

While Not objTextFile.AtEndOfStream

	ListBox.AddItem objTextFile.ReadLine

Wend

End Sub

Edited by Nemo

Share this post


Link to post
Share on other sites

this is the progress I have made so far

Private Sub lstmov()
Sub readfile()
Dim objFSO As Scripting.FileSystemObject
Dim objTextFile As TextStream

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Program Files\TGN Soft\Enigma\list\mov.txt", ForReading)

While Not objTextFile.AtEndOfStream

	lstmov.AddItem objTextFile.ReadLine

Wend

End Sub
Private Sub lstgame()
Dim objFSO As Scripting.FileSystemObject
Dim objTextFile As TextStream

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Program Files\TGN Soft\Enigma\list\game.txt", ForReading)

While Not objTextFile.AtEndOfStream

	lstgame.AddItem objTextFile.ReadLine
End Sub
Private Sub lstmusic()
Dim objFSO As Scripting.FileSystemObject
Dim objTextFile As TextStream

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Program Files\TGN Soft\Enigma\list\music.txt", ForReading)

While Not objTextFile.AtEndOfStream

	lstmusic.AddItem objTextFile.ReadLine
End Sub
Private Sub lstsoft()
Dim objFSO As Scripting.FileSystemObject
Dim objTextFile As TextStream

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Program Files\TGN Soft\Enigma\list\soft.txt", ForReading)

While Not objTextFile.AtEndOfStream

	lstsoft.AddItem objTextFile.ReadLine
End Sub

i have run into this error

enigmahelp.jpg

Can some one tell me where I am going wrong thank you?

Edited by thecryptkeeper

Share this post


Link to post
Share on other sites

I'm not sure what's going on because I can't see your whole project, but I would not put the 4 subroutines you created within the Sub lstmov().

 

Edit: Also, the last subroutine was missing a Wend statement.

 

You should have something like this:

Private Sub lstmov()

'call you other subroutines from here 

End Sub
Sub readfile()
Dim objFSO As Scripting.FileSystemObject
Dim objTextFile As TextStream

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Program Files\TGN Soft\Enigma\list\mov.txt", ForReading)

While Not objTextFile.AtEndOfStream

	lstmov.AddItem objTextFile.ReadLine

Wend

End Sub
Private Sub lstgame()
Dim objFSO As Scripting.FileSystemObject
Dim objTextFile As TextStream

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Program Files\TGN Soft\Enigma\list\game.txt", ForReading)

While Not objTextFile.AtEndOfStream

	lstgame.AddItem objTextFile.ReadLine
End Sub
Private Sub lstmusic()
Dim objFSO As Scripting.FileSystemObject
Dim objTextFile As TextStream

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Program Files\TGN Soft\Enigma\list\music.txt", ForReading)

While Not objTextFile.AtEndOfStream

	lstmusic.AddItem objTextFile.ReadLine
End Sub
Private Sub lstsoft()
Dim objFSO As Scripting.FileSystemObject
Dim objTextFile As TextStream

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Program Files\TGN Soft\Enigma\list\soft.txt", ForReading)

While Not objTextFile.AtEndOfStream

	lstsoft.AddItem objTextFile.ReadLine

Wend
End Sub

Edited by Nemo

Share this post


Link to post
Share on other sites

dont dim in sub, its bad standards. Dim in General

 

and if the txt file is in the same folder as the project, you can just use "app.path\mov.txt". rather than "C:Program Files...\mov.txt"

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...