Results 1 to 6 of 6

Thread: [Resoved] Text Reader Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    11

    Resolved [Resoved] Text Reader Help

    hi!
    i have a Text Editing software where i can load any .txt file.
    this works perfectly just i want to get rid of the error message " file cannot be found" and replace it with a msg box saying some thing like "Windows cannot fild the file "abc" , please make sure you have typed the name correctly."
    here is the code i have created

    VB Code:
    1. Private Sub cmdLoad_Click()
    2.  
    3. Dim strName As String
    4. Dim strFile As String
    5. Dim strTemp As String
    6.  
    7.     strName = InputBox("Filename:") ' file name to load
    8.     Open "C:\" & strName & ".txt" For Input As #1 ' loaded file
    9.         strFile = ""
    10.             Do Until EOF(1)
    11.                     Line Input #1, strTemp
    12.                     strFile = strFile & strTemp & vbCrLf
    13.             Loop
    14.             txtout.Text = strFile 'print file to textbox
    15.     Close #1
    16.  
    17. End Sub
    Thx in advance!!
    Last edited by Mick Grey; Oct 7th, 2006 at 11:52 PM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Text Reader Help

    VB Code:
    1. strName = InputBox("Filename:")
    2. if dir(strname) = "" then 'file not found
    3.      msgbox "File " & strname & " Not found"
    4.      exit sub
    5. end if
    6. ' rest of your code
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    11

    Re: Text Reader Help

    Sorry but that didnt work
    instead of the msg box poping up when it cant find a file it popups eveytime even the file is located in c:\ and nothing comes up in the text box this is how i placed ur code.
    Sorry for the Trouble im still a noob with VB!!
    VB Code:
    1. Private Sub cmdLoad_Click()
    2.  
    3. Dim strName As String
    4. Dim strFile As String
    5. Dim strTemp As String
    6.  
    7. strName = InputBox("Filename:")
    8. if dir(strname) = "" then 'file not found
    9.      msgbox "File " & strname & " Not found"
    10.      exit sub
    11. end if
    12.     Open "C:\" & strName & ".txt" For Input As #1 ' loaded file
    13.         strFile = ""
    14.             Do Until EOF(1)
    15.                     Line Input #1, strTemp
    16.                     strFile = strFile & strTemp & vbCrLf
    17.             Loop
    18.             txtout.Text = strFile 'print file to textbox
    19.     Close #1
    20.  
    21. End Sub

    ive also tryed other ways like this:
    VB Code:
    1. strName = InputBox("Filename:") ' file name to load
    2.     Open "C:\" & strName & ".txt" For Input As #1 ' loaded file
    3.         strFile = ""
    4. If Dir(strName) = "" Then 'file not found
    5.      MsgBox "File " & strName & " Not found"
    6.      Exit Sub
    7.   End If
    8. 'rest of the code
    Last edited by Mick Grey; Oct 7th, 2006 at 11:25 PM.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Text Reader Help

    VB Code:
    1. strName = InputBox("Filename:")
    2. [B]strName = "C:\" & strName & ".txt"[/B]
    3.  
    4. If Dir(strName) = "" Then 'file not found
    5.      MsgBox "File " & strName & " Not found"
    6.      Exit Sub
    7. End If
    8.     [B]Open strName For [/B]Input As #1  ' loaded file
    9.         strFile = ""
    10.             Do Until EOF(1)
    a small change, you need to have the full name for the file in the string
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    11

    Re: Text Reader Help

    thx , fix it
    thx forthe fast reply too

  6. #6
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: [Resoved] Text Reader Help

    Just use a CommonDialog its easier.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width