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

File Help 2

Status
Not open for further replies.

Ablecken

Programmer
Jun 5, 2001
130
0
0
US
Say I have a txt file with four or whatever lines in it. I want to be able to read that file and put line one into say a textbox, line two into a different textbox, and so on. How would i do that without using FileSystemObject(Cause for some reason the dll or something is bad and vb cant load it). How would I do this?
Thanx
Able
 
It is a good idea to use FileSystemObject to handle text files from now on -- and I was just reminded of this a few days ago in this very forum. If the .dll is bad, you can probably reload it from your VS/VB disk without too much trouble.

If, for some reason, you simply cannot get FileSystemObject to work, then you can get the job done with low-level file handling code. Try this:


Dim llFile As Long
Dim lsText As String

llFile = FreeFile()
Open "c:\MyFolder\MyFile" For Input As llFile
While Not EOF(llFile)
Input #llFile, lsText 'Get a line.
'
'<--- Do whatever with lsText
'
Wend
Close llFile
 
Thanx a lot WildBill. That worked. Any suggestions on how to make sure that say line one goes with textbox 5 and line 2 goes with text box 3. For testing purposes im doing a select case:


Dim llFile As Long
Dim lsText As String
Dim whatline As Integer

whatline = 0
llFile = FreeFile()
Open What For Input As llFile
While Not EOF(llFile)
Input #llFile, lsText 'Get a line.
whatline = whatline + 1

Select Case whatline
Case &quot;1&quot;
frmMain.Text1.Text = lsText
Case &quot;2&quot;
frmMain.Text2.Text = lsText
End Select
Wend
Close llFile

Thanx again on the response. it really did help:)
Able
 
-----------------------------------------------------------
Dim llFile As Byte
Dim lsText As String

llFile = FreeFile()
Open What For Input As llFile
Input #llFile, lsText 'Get 1. line.
frmMain.Text1.Text = lsText
Input #llFile, lsText 'Get 2. line.
frmMain.Text2.Text = lsText
Close llFile
-----------------------------------------------------------
or name your textboxes with an array and:
-----------------------------------------------------------
Dim llFile As Byte
Dim lsText As String
Dim I as integer

llFile = FreeFile()
Open What For Input As llFile
for i = 1 to NoOfTextBoxes
Input #llFile, lsText 'Get 1. line.
frmMain.MyText(i).Text = lsText
next i
close llFile
-----------------------------------------------------------

 
Able --

Sunaj's solution is a good one, as long as you can answer &quot;Yes&quot; to the following questions:

Q1. Do all of your text boxes belong to the same control array?

Q2. Does the sequence of your text boxes (within the control array) match the sequence of lines in your text file?

If your answer to either question is &quot;No,&quot; then there are two possible solutions that I can think of:

S1. If practical, organize your controls and text lines in such a way that Sunaj's solution will work (recommended).

OR

S2. Use the SELECT CASE code that you originally came up with above.

 
I should have read more carefully. Sunaj actually offered 2 solutions, and both will work. The 2nd solution is the one I referred to above. The 1st solution would be an alternative to using a SELECT CASE statement.
 
Hi,

Thanks for the nice words WildBillH.
My first suggestion will, however work in all cases, but is only practical with a limited amount of textboxes. Ablecken mentions 4 in his first post, for which my first suggention is straigthforward and requires less code than the 'select case' method.

Ablecken: If you are storing your textbox values in a file (usually .ini) I can recommend to take a look at savesetting and getsetting - a quick and easy way to store values in the windows registry.

Sunaj
 

- and I wrote my reply before I saw your 2nd post...
 
Thanx for the suggestions. Pretty much each file is going to be written the same way it is going to be read. textbox 1 to line one and so on. I'll probably end up using sunaj's first option. Thanx a lot guys:)
Able
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top