-
September 23rd, 2005, 02:30 PM
#1
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?
-
September 23rd, 2005, 02:37 PM
#2
Re: Run-time Error 52 Bad filename or number
Post the Key code that you are using then we can see to it.??
-
September 23rd, 2005, 02:43 PM
#3
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
-
September 23rd, 2005, 02:53 PM
#4
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..
-
September 23rd, 2005, 02:50 PM
#5
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
-
September 23rd, 2005, 02:56 PM
#6
Re: Run-time Error 52 Bad filename or number
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...
-
September 23rd, 2005, 02:59 PM
#7
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?
-
September 23rd, 2005, 03:04 PM
#8
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?
-
September 23rd, 2005, 03:16 PM
#9
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.
-
September 23rd, 2005, 03:05 PM
#10
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...
-
January 12th, 2009, 09:07 PM
#11
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.
-
January 12th, 2009, 10:22 PM
#12
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
-
January 13th, 2009, 02:21 AM
#13
Re: Run-time Error 52 Bad filename or number
Originally Posted by dglienna
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.
-
October 8th, 2012, 08:13 AM
#14
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
-
October 8th, 2012, 02:17 PM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|