Visual Basic FAQ

Pure VB: Blasting a Text File into 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: Save a Text File from a String or Text Box in One Call
Pure VB: Retrieve Multiple Filenames from the Common Dialog Control
Using the GetSaveFileName Common Dialog API

This shows how to load a file from disk into a text box (or a string buffer if you prefer) in one blast, rather than line-by-line.
 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 load the file into the textbox
   hFile = FreeFile  
   Open sFilename For Input As #hFile
      Text1.Text = Input$(LOF(hFile), hFile)
   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.


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