borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2009 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (opening excel in VB)Next Topic (create icon for desktop:) New Topic New Poll Post Reply
AndreaVB Forum : VB General : Help Me :)
Poster Message
N17R0
Level: Guest


icon Help Me :)  Archived to Disk

Hello can some one tell me how to Read and Write to text files  :rvd:

03-08-2002 at 07:28 PM
| Quote Reply
Coyote
Level: Guest

icon Re: Help Me :)  Archived to Disk

Well here is a little example.
To run this Open a new project.  Just to keep it working and loggin when you open and close the application. Set the ControlBox property to False. Not real necessary, but it keeps it so they use the Exit button and write to the file.
Next add a TextBox and set the Multiline Property to "True" and set the ScrollBars Property to "Both"
Next Add 3 command buttons...
Command1 change the Caption Property to "Write",
Command2 change the Caption property to "Read" and
Command3 change the Caption to "Exit"

Now Copy and paste the following code to Form1. Then Save the Project to some place and Run it

' **** Code Begins *****
Option Explicit

Private Sub Command1_Click()
    Open App.Path & "history.txt" For Append As #1
    Print #1, Text1.Text
    Close #1
End Sub

Private Sub Command2_Click()
    Dim strTextLine As String
    Dim StrText As String
    
Open App.Path & "history.txt" For Input As #1
  
    Do While Not EOF(1)
        Line Input #1, strTextLine
        If StrText = "" Then
' This keeps the first line from being blank due to the vbcrlf (carriage return)
        StrText = strTextLine
        Else
        StrText = StrText + vbCrLf + strTextLine
        End If
    Loop
    Text1.Text = StrText
        
Close #1

End Sub

Private Sub Command3_Click()
    Dim ff As Integer
    ff = FreeFile
    ' Edit the next line to the path and file name you want to use
    Open App.Path & "history.txt" For Append As ff
        Print #ff, "Application Closed " + Date$ + " " + Time$
    Close ff
    End
End Sub

Private Sub Form_Load()
    ' You could use this
    'Dim ff As Integer
    'ff = FreeFile
    ' and change all the following code where
    ' it has # 2 to ff and VB will take care of assigning the number
    ' and handle this for you. However - make sure you close ff
    ' Just for fun - I used #2 here. This could also be #1 as in
    ' all the above code - as long as you Close the file Example Close #2
    
    ' Edit the next line to the path and file name you want to use
    ' Example: Open "C:mytexthistory.txt For Append as # 2
    ' App.Path below uses the same folder you have you application
    ' saved at.
    Open App.Path & "history.txt" For Append As #2
        Print #2, "Application Opened " + Date$ + " " + Time$
    Close #2
End Sub

' ***** CODE ENDS ********

When you run this code, it will write to the file  the application name, date and time everytime you open and everytime you close the program. Also if you clear the text box and type something in it and click the "Write" button it will write that to the file. To see everything in the file.. click the "Read" button.

Best of luck... hope this answers you question
Take Care...
>>>Coyote<<

04-08-2002 at 03:25 AM
| Quote Reply
Coyote
Level: Guest

icon Re: Help Me :)  Archived to Disk

And.....
Just in case you want to use a RichTextBox (which gives you more options/features) instead of a regular text box. Using the same Form from the previous response. Add the Microsoft Rich TextBox control to your project. Keep the three Command buttons and delete the textbox and add a RichTextBox.
Then delete all the previous code and copy and paste the following code: AND SAVE the project as before.
' **** CODE BEGINS ***********
Option Explicit

Private Sub Command1_Click()
'i think this is the simplest way to save simple text
Open App.Path & "history.txt" For Append As 1

Print #1, RichTextBox1.Text
'close the opened file
Close 1
End Sub

Private Sub Command2_Click()
    RichTextBox1.LoadFile App.Path & "history.txt"
        
Close #1

End Sub

Private Sub Command3_Click()
    Open App.Path & "history.txt" For Append As 1
        Print #1, "Application Closed " + Date$ + " " + Time$
    Close 1
    End
End Sub

Private Sub Form_Load()
    Open App.Path & "history.txt" For Append As 1
        Print #1, "Application Opened " + Date$ + " " + Time$
    Close #1
End Sub
' ********** CODE ENDS *******

Anyway... hope this helps....
Your on a roll.... keep coding
Take Care....
>>>Coyote<<<

04-08-2002 at 04:30 AM
| Quote Reply
N17R0
Level: Guest

icon Re: Help Me :)  Archived to Disk

Thanks Coyote

04-08-2002 at 07:58 PM
| Quote Reply
AndreaVB Forum : VB General : Help Me :)
Previous Topic (opening excel in VB)Next Topic (create icon for desktop:) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2009 Andrea Tincaniborder