Visual Basic FAQ

Pure VB: Save a Text File from a String or Text Box in One Call
Posted:   Thursday December 26, 1996
Updated:   Monday December 26, 2011
Applies to:   VB3, VB4-16, VB4-32, VB5, VB6
Related:   Pure VB: Blasting a Text File into a String or Text Box in One Call
Pure VB: Retrieve Multiple Filenames from the Common Dialog Control
Using the GetSaveFileName Common Dialog API

Here's the corresponding method to the Load demo - saving the buffer or text box contents to disk in one call.  This works just as well with a string buffer rather than a text box.
 BAS Module Code

 Form Code
Add a command button to a form along with the following code:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Sub Command1_Click

   Dim hFile as Long
   Dim sFilename as String

   sFilename = "c:\demo.txt"
  'obtain the next free file handle from the 
  'system and and save the text box contents
   hFile = FreeFile  
   Open sFilename For Output As #hFile
      Print #hFile, Text1.Text
   Close #hFile
End Sub
Text boxes have limitations as to the largest amount of data they can hold, based on the operating system in use. In any application, it is prudent to first determine both the operating system and the size of the file being loaded (using the FileLen() function).

The limit for Windows 95 or Windows 98 is 32k. For NT-based systems the limit is 64k. Applications requiring the loading of files or portions of files exceeding these maximums should use the Rich Textbox control.

Even though the default property of a textbox is the Text property, it's good form to explicitly reference the property when using it in a method.



PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter