VB School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

Visual Basic Source Code


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

Join 300,309 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,081 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: 60,920

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

paperclipmuffin 2009-04-17 01:36:10

I cant get it to work for me. When i run, it crashes and highlights the "commondialog1.showsave" saying "object required". Help anyone?

binyamin 2009-07-01 15:39:51

You just need to add this control and put it somewhere on your form: 1) Goto Project --> Components . . . 2) Scroll down and make sure Microsoft Common Dialog control 6.0 (SP6) is checked. 3) Click OK 4) This will add the CommonDialog control to your to your toolbar. 5) Find the icon for CommonDialog. It looks kinda like a page from a Powerpoint presentation or old radio. It will be outlined in black. 6) Once you locate it, add one to your form. It will not show up as an icon when you run your program, but it will allow you to open up a saveas window, etc. 7) Rerun your program. It should work now. Hope this helps!


Add comment


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





Live VB Help!

Be Social

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

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month