Run-time Error 52 Bad filename or number
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Thread: Run-time Error 52 Bad filename or number

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Posts
    13

    Run-time Error 52 Bad filename or number

    I am trying to pass a filename from and executable to a dll and I get the error listed in the title. Any thoughts on how to correct this?

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Run-time Error 52 Bad filename or number

    Post the Key code that you are using then we can see to it.??

  3. #3
    Join Date
    Sep 2005
    Posts
    13

    Re: Run-time Error 52 Bad filename or number

    The actuall dll code is rather long, but the calling routine is as follows

    Private Declare Sub ParseBDF Lib _
    "C:\MyDllLibrary\BDF Parser\BDFParserDLL.dll" _
    (ByVal FName As String) ', ByRef Gctr As Integer)
    Sub Main()
    Dim FName As String

    If Command$ <> "" Then
    FName = Command$
    Else
    With Form1.CD1
    .FileName = ""
    .CancelError = True
    .DialogTitle = "Select BDF File"
    .Filter = "Bulk Data File (*.bdf)|*.bdf|All Files (*.*)|*.*"
    .Flags = cdlOFNFileMustExist Or cdlOFNExplorer Or cdlOFNLongNames
    .ShowOpen
    FName = .FileName
    End With
    End If
    MsgBox (FName)
    ParseBDF FName ', Gctr)
    Stop
    End Sub

    The dll starts as follows

    Sub ParseBDF(ByVal FName As String) ', ByRef Gctr As Integer)
    Dim FNum As Integer: FNum = FreeFile
    Dim TStr As String
    Dim tmpstr As String
    Dim tmplen1 As Integer
    Dim tmplen2 As Integer
    Dim comlen1 As Integer
    Dim comlen2 As Integer
    Dim FileType As String * 3
    Dim LineType As String

    Erase Grid
    Gctr = 0: Pctr = 0: Mctr = 0
    Close FNum
    Open FName For Input As FNum

    Line Input #FNum, TStr
    ...
    End Sub

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Run-time Error 52 Bad filename or number

    Ok Looks like the problem is in the dll..
    Code:
    Sub ParseBDF(ByVal FName As String) ', ByRef Gctr As Integer)
    Dim FNum As Integer: FNum = FreeFile
    Dim TStr As String
    Dim tmpstr As String
    Dim tmplen1 As Integer
    Dim tmplen2 As Integer
    Dim comlen1 As Integer
    Dim comlen2 As Integer
    Dim FileType As String * 3
    Dim LineType As String
    
    Erase Grid
    Gctr = 0: Pctr = 0: Mctr = 0
    Close FNum  <----------------------- REMOVE THIS LINE
    Open FName For Input As FNum
    
    Line Input #FNum, TStr
    ...
    CLOSE FNUM   <----------------------- Place it here..
    End Sub
    Freefile will give you the next available free file number .. Closing this file number may be causing your problem..

  5. #5
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Run-time Error 52 Bad filename or number

    By going through this part of the code
    Code:
    Erase Grid
    Gctr = 0: Pctr = 0: Mctr = 0
    Close FNum
    Open FName For Input As FNum
    Line Input #FNum, TStr
    in the DLL part, How can you close anything that has not been opened yet.

    Before closing the File, you need to open it. And your function doesn't open anything prior to Close FNum statement.

    try doing this instead.
    Code:
    Erase Grid
    Gctr = 0: Pctr = 0: Mctr = 0
    Open FName For Input As FNum
    Line Input #FNum, TStr
    ...
    Close FNum

  6. #6
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Talking Re: Run-time Error 52 Bad filename or number

    Quote Originally Posted by vb_the_best
    Before closing the File, you need to open it. And your function doesn't open anything prior to Close FNum statement.

    try doing this instead.
    Quicker on the draw i see...

  7. #7
    Join Date
    Sep 2005
    Posts
    13

    Re: Run-time Error 52 Bad filename or number

    Actually, the close FNum statement is not the culprit. I added a message box after that line to see what FName is, and it comes up ?????????. However, just before calling the dll I have teh same message box and FName is correct. Any thoughts?

  8. #8
    Join Date
    Sep 2005
    Posts
    13

    Re: Run-time Error 52 Bad filename or number

    In the main calling code I changed the line from

    ParseBDF FName

    to

    ParseBDF StrConv(FName, vbUniCode)

    checking FName in the dll gives the correct filename for FName.

    One last question...

    Is there a way to debug or step through dlls at runtime?

  9. #9
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Run-time Error 52 Bad filename or number

    Well i wrote a small program and it seems Close statement in the above posted code will not give this error. There seems to be some other problem.

    Not sure if you can step through the DLL. As your function (in the DLL) is not a big one, you can put simple Message Boxes after each statement and then you will be able to know which statement is actually erroring out.

  10. #10
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Run-time Error 52 Bad filename or number

    Ok instead of trying to explain here just follow this tread also in this forum

    http://www.codeguru.com/forum/showthread.php?t=357076

    I think it should help you...

  11. #11
    Join Date
    Jan 2009
    Posts
    1

    Re: Run-time Error 52 Bad filename or number

    I using the same code provided and can run in Excel 2007.
    But when I run it in Excel 2003 & XP, error encountered.

    compile error:
    Wrong number of argument of invalid property assignment.

    and it highlighted the on: InsertPictureInRange

    The code is as shown below:

    Sub TestInsertPictureInRange()

    InsertPictureInRange "\\semsv003\d\LF Photo\" & Range("MENU!$E$3") & ".jpg", _
    Range("CUTTING!$A77:$Z95")

    End Sub
    Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)

    ' inserts a picture and resizes it to fit the TargetCells range
    Dim p As Object, t As Double, l As Double, w As Double, h As Double

    Sheets("CUTTING").Select

    If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
    If Dir(PictureFileName) = "" Then Exit Sub

    ' import picture
    Set p = ActiveSheet.Pictures.Insert(PictureFileName)

    ' determine positions
    With TargetCells
    t = .Top
    l = .Left
    w = .Offset(0, .Columns.Count).Left - .Left
    h = .Offset(.Rows.Count, 0).Top - .Top
    End With
    ' position picture
    With p
    .Top = t
    .Left = l
    .Width = w
    .Height = h
    End With
    Set p = Nothing

    ' ********************************************************


    End Sub


    Please advice.

    thank you.

  12. #12
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,984

    Re: Run-time Error 52 Bad filename or number

    Please go back and reformat your code. Use CODE TAGS, and also include the connection information that you are using. You need to use 'late-binding' so that you can use any version of a product.

    Code:
    Option Explicit
    ' These are both examples of Late Binding
    
    Public Sub RunAccessMacro(strDB As String, strMacro As String)
    '================================================================
    'for late binding declare it As Object and use CreateObject() function
      Dim AccessDB As Object
      Set AccessDB = CreateObject("Access.Application")
    
        With AccessDB
            .OpenCurrentDatabase strDB
            .DoCmd.RunMacro strMacro, 1
            '.Visible = True    'you decide
            .CloseCurrentDatabase
        End With
        Set AccessDB = Nothing
    
    End Sub
    
    Public Sub ExcelMacro()
      Dim excl As Object
      Dim wrbk As Object
    
        Set excl = CreateObject("Excel.Application")
        excl.DisplayAlerts = False
        Set wrbk = excl.Workbooks.Open("myfile", , True, , "mypassword")
        excl.Run "MacroName", "arg1", "arg2"
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  13. #13
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Run-time Error 52 Bad filename or number

    Quote Originally Posted by dglienna View Post
    Please go back and reformat your code. Use CODE TAGS, and also include the connection information that you are using.
    AND ...

    this thread is over 3 years old, and is unrelated to your problem...

    Please start your own thread with appropriate title in future..

    Gremmy..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  14. #14
    Join Date
    Oct 2012
    Posts
    1

    Angry Re: Run-time Error 52 Bad filename or number

    have the same problem with the following piece of code help plz i have over 1000 lines code working already aand really need this bit to work but cant work it out :

    Code:
    frm_winnings.lblnumbers_matched = ("you have matched " & result & "numbers")
    
        Select Case result
    
            Case result < 3
            
                Open "N:\Computing\Advanced Higher Computing\Software Development\winnings.txt" For Input As #1
                
                winnings(1) = Input(LOF(6), 1)
                
                Close #1
                
            Case result = 3
            
                Open "N:\Computing\Advanced Higher Computing\Software Development\winnings.txt" For Input As #2
                    
                    winnings(2) = Input(LOF(6), 2)
                
                Close #2
                
            Case result = 4
            
                Open "N:\Computing\Advanced Higher Computing\Software Development\winnings.txt" For Input As #3
                    
                    winnings(3) = Input(LOF(6), 3)
                
                Close #3
                
            Case result = 5
            
                Open "N:\Computing\Advanced Higher Computing\Software Development\winnings.txt" For Input As #4
                    
                    winnings(4) = Input(LOF(6), 4)
                
                Close #4
    
            Case result = 6
            
                Open "N:\Computing\Advanced Higher Computing\Software Development\winnings.txt" For Input As #5
                    
                    winnings(5) = Input(LOF(6), 5)
                
                Close #5
    
            Case result = 7
            
                Open "N:\Computing\Advanced Higher Computing\Software Development\winnings.txt" For Input As #6
                    
                    winnings(6) = Input(LOF(6), 6)
                
                Close #6
    
        End Select
    
    End Sub

  15. #15
    Join Date
    Jul 2005
    Posts
    1,074

    Re: Run-time Error 52 Bad filename or number

    You should use the same file number in the LOF function
    Code:
    '
    winnings(1) = Input(LOF(1), 1)
    '
    winnings(2) = Input(LOF(2), 2)
    '
    'etc
    The LOF function retrieves the size of the file you want to load
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center