This is simple but it just reurns the last line of the textfile. Thanks for
your help.
GM
Function ReadLineTextFile()
Const ForReading = 1, ForWriting = 2
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.OpenTextFile("c:\testfile.txt", ForReading)
Do Until MyFile.atendofline
ReadLineTextFile = MyFile.ReadLine ' Returns "Hello world!"
MsgBox MyFile.ReadLine
Loop
End Function
> What goes wrong? Perhaps if you post your code ....
>
[quoted text clipped - 35 lines]
> > > > Thanks
> > > > GM
No wonder you're getting confused if you're using the FSO for this. Stick to
VB's native methods. Open the file, read your variables, close the file.
That really is all there is to it.
Check Help on the Open and Input methods.
> This is simple but it just reurns the last line of the textfile. Thanks for
> your help.
[quoted text clipped - 53 lines]
> > > > > Thanks
> > > > > GM
I agree with Jezebel. I would junk the use of the FileSystemObject.
FSO is seductive because it's easy to use and you can do a lot with
very few lines of code. However, it can easily come back to bite you
in the butt because there are so many different versions of FSO from
operating system to operating system.
Also, when you declare your variables like "Dim fso, MyFile" without
specifying a data type, VB automatically makes them variants.
Variants are the most resource hungry of any data type and can also
come back to bite you in the butt.
A lot of my progs save and retrieve variables to and from text files
all the time. In your case I would just save those two lines as two
string variables and retrieve them as such later when you need them.
Here are two subs of mine that I use and reuse for storing variables.
They store many more than the two strings that you need to use and
store some other datatypes, but you could pare them down to easily
make them store and retrieve the two strings that you're using. When
you're using the "SaveVariables" sub, the first string you feed it is
used to name the file. Then if you feed the RetrieveVariables sub the
same string, it opens the same file to grab the saved data. For
example, if you feed SaveVariables "allthestuff", it saves it in a
file named "allthestuff.dat".
I'm also including a stock Function that one of the subs relies on. I
hope this is helpful.
'---------------------------------------------------------------------------------------
' Procedure : SaveVariables
' Purpose : Save variables to a file for later retrieval.
' : Sub can be used over and over called by many different
subs.
' : Use in conjunction with the "RetrieveVariables" Sub.
'---------------------------------------------------------------------------------------
'
Public Sub SaveVariables(ByVal strFileName As String, _
ByVal str1stString As String, ByVal
str2ndString As String, _
ByVal str3rdString As String, ByVal
str4thString As String, _
ByVal str5thString As String, ByVal int1stInt
As Integer, _
ByVal int2ndInt As Integer, ByVal int3rdInt As
Integer, _
ByVal bln1stBoo As Boolean, ByVal bln2ndBoo As
Boolean, _
ByVal bln3rdBoo As Boolean)
Dim intFF As Integer
' kill previous hold file if it exists
If FileExists(App.Path & "\" & strFileName & ".dat") Then
Kill App.Path & "\" & strFileName & ".dat"
End If
intFF = FreeFile
Open App.Path & "\" & strFileName & ".dat" For Output As #intFF
Write #intFF, str1stString, str2ndString, str3rdString,
str4thString, str5thString, _
int1stInt, int2ndInt, int3rdInt, bln1stBoo, bln2ndBoo, bln3rdBoo
Close #intFF
End Sub
'---------------------------------------------------------------------------------------
' Procedure : RetrieveVariables
' Purpose : Retrieve variables that have been saved to sequential
access file.
' : Use in conjunction with the "SaveVariables" Sub.
'---------------------------------------------------------------------------------------
'
Public Sub RetrieveVariables(ByRef strFileName As String, _
ByRef str1stString As String, ByRef
str2ndString As String, _
ByRef str3rdString As String, ByRef
str4thString As String, _
ByRef str5thString As String, ByRef int1stInt
As Integer, _
ByRef int2ndInt As Integer, ByRef int3rdInt As
Integer, _
ByRef bln1stBoo As Boolean, ByRef bln2ndBoo As
Boolean, _
ByRef bln3rdBoo As Boolean)
Dim intFF As Integer
intFF = FreeFile
Open App.Path & "\" & strFileName & ".dat" For Input As intFF
Do While Not EOF(intFF)
Input #intFF, str1stString, str2ndString, str3rdString,
str4thString, str5thString, _
int1stInt, int2ndInt, int3rdInt, bln1stBoo, bln2ndBoo, bln3rdBoo
Loop
Close #intFF
End Sub
Public Function FileExists(strCheckEe As String) As Boolean
Dim strCheck_File As String
strCheck_File = Dir(strCheckEe)
If Len(strCheck_File) > 0 Then
FileExists = True
Else
FileExists = False
End If
End Function
J French - 31 Oct 2004 13:41 GMT
<snip>
>Public Function FileExists(strCheckEe As String) As Boolean
>Dim strCheck_File As String
> strCheck_File = Dir(strCheckEe)
Just don't do that Dir() is dangerous here
> If Len(strCheck_File) > 0 Then
> FileExists = True
> Else
> FileExists = False
> End If
>End Function
DanS - 31 Oct 2004 16:40 GMT
erewhon@nowhere.uk (J French) wrote in news:4184ea69.177687117
@news.btclick.com:
><snip>
>
[quoted text clipped - 9 lines]
>> End If
>>End Function
There's also the option of using an INI file instead of just a flat text
file. Then you could read any data saved under a setting group easily,
without having to be concerned about the order of the data in the text
file.
DanS