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

Appending text files from VB6

Status
Not open for further replies.

DK47

Programmer
Jun 3, 2003
118
US
My latest project requires accessing text files from the user's hard drive for appending. Actually I want the user to be able to add lines of text to an existing file from data he needs which he accesses from from my server.
I have tried everything I can think of but can't seem to get the contents of a file loaded into the text box for appending.

This is my latest effort. It raises no error, so I guess the syntax is correct, but when I run the program the text box remains blank.

Public Function ReadTextFileContents(filename As String) As String
Dim fnum As Integer, isOpen As Boolean
On Error GoTo Error_Handler
fnum = FreeFile()
Open filename For Append As #fnum
isOpen = True
ReadTextFileContents = Append(LOF(fnum), fnum)
Error_Handler:
If isOpen Then Close fnum
If Err Then Err.Raise Err.Number, , Err.Description
End Function
Text1.Text = ReadTextFileContents("C:\VISUAL BASIC FILES\VB98\VB6 projects\FILEOPENTEST\FileTest.rtf")

Any suggestions would help.

Thanks,
Dwight
 

>>Open filename For Append As #fnum

This open the file for appending to it, not for reading it in. Use Input to read it in or Binary Read Write for your purposes. Or, you could use 2 procedures, one to read it in and another to save the contents.
[tt]
Option Explicit

Private Sub Form_Load()

Text1.Text = ReadFileIn("D:\binary.txt")

End Sub

Private Sub Command1_Click()

If PrintFileOut(Text1.Text, "D:\Binary.txt") = False Then MsgBox Err.Description

End Sub

Public Function ReadFileIn(FileName As String) As String

On Error GoTo ReadFileInError

Dim FNumb As Integer

If Dir(FileName) = vbNullString Then Exit Function

FNumb = FreeFile

Open FileName For Binary As #FNumb

ReadFileIn = Input(FileLen(FileName), #FNumb)

Close #FNumb

Exit Function
ReadFileInError:

MsgBox Err.Description

End Function

Public Function PrintFileOut(StringToPrint As String, FileName As String) As Boolean

On Error GoTo PrintFileOutError

Dim FNumb As Integer

FNumb = FreeFile

Open FileName For Output As #FNumb

Print #FNumb, StringToPrint

Close #FNumb

PrintFileOut = True

Exit Function
PrintFileOutError:

MsgBox Err.Description

End Function
[/tt]

Good Luck

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top