Solved

Load Text File Into Rich Text Box

Posted on 2001-06-12
5
204 Views
Last Modified: 2010-05-02
How would I go about loading all the text from a file, like test.txt into a rich text box on a form by clicking a command button? Thanks in advance!
0
Comment
Question by:cybrthug
5 Comments
 
LVL 4

Expert Comment

by:beckingh
ID: 6182366

This code will read a text file into a regular text box


Dim TextLine as string

Open "c:\list.ini" For Input As #1 ' first open file

Do Until EOF(1)
     Line Input #1, TextLine
     Text1.Text = Text1.Text & vbCrLf & TextLine
Loop
     
Close #1



0
 
LVL 15

Accepted Solution

by:
ameba earned 50 total points
ID: 6182381
    Me.RichTextBox1.LoadFile "c:\test.txt", rtfText
0
 
LVL 5

Expert Comment

by:aminerd
ID: 6182482
Ameba's way is easiest, however, if you want to read the file yourself, I suggest something like the following:

Public Function OpenFile(FileName As String) As String
 
  Dim intFile As Integer
  Dim strTemp As String
 
  'Get a new file number
  intFile = FreeFile
 
  'Read the contents of the file into strTemp
  Open FileName For Input As intFile
    strTemp = Space(LOF(intFile))
    Get intFile, 0, strTemp
  Close intFile
 
  'Return the file contents
  OpenFile = strTemp
 
End Function

If you hardcode a file number (for example, #1), you could potentially run into problems. It's always better to use the FreeFile function. Also, by reading the entire file in at once you save time (and skip a loop).

You could use the function I posted like:

RichTextBox.Text = OpenFile("c:\test.txt")
0
 

Expert Comment

by:gauravdhup
ID: 6182592
here is the code

Open App.Path & "\test.txt" For Input As #1
Do While Not EOF(1)   ' Check for end of file.
  linesfromfile = StrConv(InputB(LOF(1), 1), vbUnicode)
  Text26.Text = linesfromfile
Loop
Close #1


here text26.text is the richtextbox
0
 

Author Comment

by:cybrthug
ID: 6183005
Short and sweet, thats the way I like it, thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now