[RESOLVED] "Run time error '52': Bad file name or number" when i open associated text file-VBForums
Results 1 to 10 of 10

Thread: [RESOLVED] "Run time error '52': Bad file name or number" when i open associated text file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2009
    Location
    B.C. Canada
    Posts
    199

    Resolved [RESOLVED] "Run time error '52': Bad file name or number" when i open associated text file

    i am getting error "Run time error '52': Bad file name or number" when i use this code to open a text file that is accociated with my app.

    File association is set up using inno.

    when i replace Command$ with "C:\Users\Dave\Documents\dave.cppl" and run it in design mode there is no error.

    Code:
    Private Sub Form_Load()
    
    Dim DoubleClickedFile As String
    Dim sFileText As String
    Dim iFileNo As Integer
    
    DoubleClickedFile = Command$
    
    If DoubleClickedFile <> "" Then
    DoubleClickedFile = Mid(DoubleClickedFile, 4, Len(DoubleClickedFile) - 3)
    
    Text1.Text = DoubleClickedFile 'to check if the file and path is good
    
    iFileNo = FreeFile
     Open DoubleClickedFile For Input As #iFileNo
     Input #iFileNo, sFileText
     Text1.Text = sFileText
     Close #iFileNo
    End If
    
    End Sub

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,133

    Re: "Run time error '52': Bad file name or number" when i open associated text file

    What does Command$ return?
    Place break pointer right on that line and you will see the actual value.
    Or you can at least print value to debug (Debug.Print DoubleClickedFile).

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2009
    Location
    B.C. Canada
    Posts
    199

    Re: "Run time error '52': Bad file name or number" when i open associated text file

    DoubleClickedFile = Mid(DoubleClickedFile, 4, Len(DoubleClickedFile) - 3)
    returns this: \Users\Dave\Documents\dave.cppl"

    and DoubleClickedFile returns this: "C:\Users\Dave\Documents\dave.cppl"

    i was trying to remove the drive letter but did nit remove the right side quote marks.

    Does the drive letter need to be removed of this to work?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    36,790

    Re: "Run time error '52': Bad file name or number" when i open associated text file

    Why are you trying to remove the drive letter?

    That is an important part of the file location, and as you are opening the file you want to know the full file location.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2009
    Location
    B.C. Canada
    Posts
    199

    Re: "Run time error '52': Bad file name or number" when i open associated text file

    I saw the same problem posted elswhere in google and the solution was to remove the drive letter. but this does not work.

  6. #6

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    36,790

    Re: "Run time error '52': Bad file name or number" when i open associated text file

    I can't think of any situation like this where removing the drive letter would be a good thing.


    Does Command$ actually contain the " character at the start and end?

    If so, this should solve it:
    Code:
    DoubleClickedFile = Command$
    
    If DoubleClickedFile <> "" Then
      If Left$(DoubleClickedFile, 1) = """" Then DoubleClickedFile = Mid$(DoubleClickedFile, Len(DoubleClickedFile) -2)
    
      Text1.Text = DoubleClickedFile 'to check if the file and path is good
      iFileNo = FreeFile
    ...

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Dec 2009
    Location
    B.C. Canada
    Posts
    199

    Re: "Run time error '52': Bad file name or number" when i open associated text file

    this code returns this: "C:\Users\Dave\Documents\dave.cppl" in text1.text
    Code:
    Private Sub Form_Load()
    On Error Resume Next
    
    Dim DoubleClickedFile As String
    Dim sFileText As String
    Dim iFileNo As Integer
    
    
    DoubleClickedFile = Command$
    
    If DoubleClickedFile <> "" Then
      'If Left$(DoubleClickedFile, 1) = """" Then DoubleClickedFile = Mid$(DoubleClickedFile, Len(DoubleClickedFile) - 2)
    Text1.Text = DoubleClickedFile
    
    iFileNo = FreeFile
     Open DoubleClickedFile For Input As #iFileNo
     Input #iFileNo, sFileText
     Text2.Text = sFileText
     Close #iFileNo
    End If
    
    End Sub
    and this code returns this: pl"
    Code:
    Private Sub Form_Load()
    On Error Resume Next
    
    Dim DoubleClickedFile As String
    Dim sFileText As String
    Dim iFileNo As Integer
    
    
    DoubleClickedFile = Command$
    
    
    
    If DoubleClickedFile <> "" Then
      If Left$(DoubleClickedFile, 1) = """" Then DoubleClickedFile = Mid$(DoubleClickedFile, Len(DoubleClickedFile) - 2)
    
    Text1.Text = DoubleClickedFile
    
    iFileNo = FreeFile
     Open DoubleClickedFile For Input As #iFileNo
     Input #iFileNo, sFileText
     Text2.Text = sFileText
     Close #iFileNo
    End If
    
    End Sub
    either way i get "Run time error '52': Bad file name or number" when i remove "on error resume next"

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    36,790

    Re: "Run time error '52': Bad file name or number" when i open associated text file

    Ooops, I missed a parameter:
    Code:
      If Left$(DoubleClickedFile, 1) = """" Then DoubleClickedFile = Mid$(DoubleClickedFile, 2, Len(DoubleClickedFile) -2)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Dec 2009
    Location
    B.C. Canada
    Posts
    199

    Re: "Run time error '52': Bad file name or number" when i open associated text file

    so my problem was quotes around the file and path.
    Thanks, this works.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.