opening a file to a text box

sundance1231
sundance1231 used Ask the Experts™
on
what is the best to open a file (text file) into a text box using a drop down menu?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Here is a fast way:

' Load a text file into the textbox
Public Sub LoadFile(fileName As String)
    Dim lFileLength As Long
    Dim iFileNum As Integer
   
    If fileName = "" Or Dir(fileName) = vbNullString Then
        Err.Raise 53    ' File not found
        Exit Sub
    End If

    'Get a free file number and open the file
    iFileNum = FreeFile
    Open fileName For Input As iFileNum

    'Get the length of the file and
    'read it into the text box
    lFileLength = LOF(iFileNum)
    myTextBox.Text = Input(lFileLength, #iFileNum)

    Close iFileNum

End Sub

Pay attention, because the textbox can only accept files not bigger than 64K.
If the file is bigger, use a RichEdit control: this one has its own method to open a file.

M.

Commented:
"textbox can only accept files not bigger than 64K" in Windoze 95/98/Me

Author

Commented:
Lets go back a second, how would I use this with a drop down menu?

Chris
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!


What do you mean exactly with "with a drop down menu" ??
Maybe you want to open a file in the menu_Click() event?
M.

Commented:
What's the caption of the "drop down menu" ?

Author

Commented:
Yes Exactly

Ok, so:

Add a "Microsoft Common Dialog control" to your form

Add the following code:

Private Sub myMenu_Click()
    With CommonDialog1
        .DialogTitle = "Open a file"
        .Filter = "All files |*.*"
        .fileName = vbNullString
        .ShowOpen
        If .fileName <> vbNullString Then
            LoadFile(.fileName)
        End If
    End With
End Sub

Hope this helps...
M.

Author

Commented:
Im trying to run this script, its accesses the folder but when I open the file it only puts the path on the text line.

Private Sub Command8_Click()
CommonDialog1.Filter = "text files|*.txt|rich text files|*.rtf|all files|*.*"
CommonDialog1.ShowOpen
Text1.Text = CommonDialog1.fileName


Any help...

Thanks

Author

Commented:
I got it to work using this code from another source. It was:

Private Sub Command8_Click()
Dim sfilename As String
CommonDialog1.Filter = "TC57(*.tc57)|*.tc57"
CommonDialog1.ShowOpen
sfilename = CommonDialog1.FileName
Open sfilename For Input As #1
sText = ""
iLineCount = 0
Do While Not EOF(1) And iLineCount < 50
Line Input #1, sLine
sText = sText & sLine & Chr(13) & Chr(10)
iLineCount = iLineCount + 1
Loop
Close #1
Text1.Text = sText

Thanks for gettting me in the right direction.


Chris

Author

Commented:
I didnt explain this very well, Im new to the VB game and was not sure of the right terminology. Thanks for putting up with it.


Chris

Commented:
So, nothing to do with drop down menus then  :-)

Just for completeness, marconovaro's original "load into textbox" code is the most efficient:

   'Get a free file number and open the file
   iFileNum = FreeFile
   Open fileName For Input As iFileNum

   'Get the length of the file and
   'read it into the text box
   lFileLength = LOF(iFileNum)
   myTextBox.Text = Input(lFileLength, #iFileNum)

   Close iFileNum

Much better than looping input.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial