Code Snippets

  

Visual Basic Source Code


Welcome to Dream.In.Code
Become a VB Expert!

Join 148,803 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,175 people online right now. Registration is fast and FREE... Join Now!





Open & Saving .txt files

how to open and save text files

Submitted By: pal121
Actions:
Rating:
Views: 35,157

Language: Visual Basic

Last Modified: February 10, 2006
Instructions: copy & paste & add Microsoft Common Dialog Control 6.0

you need a button and a textbox

Snippet


  1. Private Sub cmdsave_Click()
  2.  
  3. Dim filelocation As String
  4.  
  5. ' loads save as box
  6.     commondialog1.ShowSave
  7.    
  8.     filelocation = commondialog1.FileName
  9.    
  10. ' append saves over file if it assists
  11.     Open filelocation For Append As #1
  12.         Print #1, text1.text
  13.     Close #1
  14.  
  15. End Sub
  16.  
  17. Private Sub cmdopen_Click()
  18.  
  19. Dim filelocation As String
  20.  
  21. ' show open box
  22.     commondialog1.ShowOpen
  23.    
  24.     filelocation = commondialog1.FileName
  25.  
  26. ' input files into text1.text   
  27.     Open filelocation For Input As #1
  28.    
  29. Do Until EOF(1)
  30.  
  31.         Input #1, Data
  32.         text1.text = text1.text + Data + vbNewLine
  33.     EOF (1)
  34.     Loop
  35.     Close #1
  36.    
  37. End Sub
  38.  
  39.  
  40.  

Copy & Paste


Comments


karlamohan 2008-07-26 05:51:12

database in MSAccess has been connected I want to open a document in wordpad format. the file address is in a field of access. Private Sub Form_Load() Set DB = DBEngine.OpenDatabase("C:\Documents and Settings\madhu\My Documents\dbdbase.mdb") Set RS = DB.OpenRecordset(" Select * from tblBdata ") MsgBox "Database connected" Call Disp End Sub Private Sub Disp() txtStNo.Text = RS!stno txtName.Text = RS!Name txtDesgn.Text = RS!desgn txtbiodn.text = Rs!biodtfn End Sub

akhileshbc 2008-09-28 04:20:29

Append means adding the new data to the end of the file. Output means, replacing the whole textfile with the new data. :)

akhileshbc 2008-10-01 01:12:58

The loop can be something like this also: While Not EOF(1) Line Input #1, Data Text1.Text=Text1.Text & Data & vbNewLine Wend


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month