Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Runtime Error '5' Please assist

Status
Not open for further replies.

smash81

Programmer
Mar 12, 2001
88
KE
Hi Experts,
I'm using a common dialog control to get a filename from the user.
The code and all other properties for the control work fine except fot the FileName property.
When I select dlgopen.FileName from the drop down menu, VB changes it to dlgopen.filename. Clearly, there is a problem here. As a result, when I create an EXE and run it, I get a runtime error on that line and the application terminates.
Please advise on what I need to do to get around this problem.
Thank you.
 
Could you please post the code you are using in relation to the Dialog box?

Cheers

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Private Sub cmdopenfile_Click()
On Error GoTo errorhandler
filename2 = " "
dlgopen.Filter = "Swift Files (*.dat)|*.dat| Swift Files (*.out)|*.out"


dlgopen.Flags = _
cdlOFNFileMustExist + _
cdlOFNHideReadOnly + _
cdlOFNLongNames + _
cdlOFNExplorer
dlgopen.CancelError = True


dlgopen.ShowOpen

If Err.Number = cdlCancel Then
' The user canceled.

Exit Sub
ElseIf Err.Number <> 0 Then

' Unknown error.
MsgBox "Error " & Format$(Err.Number) & _
" selecting file." & vbCrLf & _
Err.Description

Exit Sub
Else



filename2=dlgopen.filename

End If









errorhandler:
If Err.Number = cdlCancel Then
filename2 = " "

ElseIf Err.Number <> 0 Then
filename2 = " "
MsgBox "An error has occured. Please contact the developer", vbCritical, "Error"

End If

End Sub
 
>Clearly, there is a problem here. As a result

No, not really, since VB isn't actually case-sensitive. What you are seeing is just a quirk of the IDE (which I can explain if you really, really want). And your runtime error therefore is not related to the capitals (or lack of them). Honestly.
 
I'm not able to recreate your problem from my machine unfortunately.

Only thing I did notice was that the line in question was the only line that hadn't been formatted by the IDE (e.g. there have been no spaces added before and after the = sign), unlike the rest of the code.

Sorry I couldn't have been more help

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Stongm,
I would appreciate the explanation on IDE. Hopefully, it will help me resolve the problem.
 
Well, let's demonstrate the quirk. You'll need a form with a commondialog on it.

Now in the IDE enter the following code:

Private Sub Form_Load()
CommonDialog1.FileName = ""
End Sub

Now add the following line:

Private Sub Form_Load()
Dim filename as string
CommonDialog1.FileName = ""
End Sub

which will actually result in

Private Sub Form_Load()
Dim filename as string
CommonDialog1.filename = ""
End Sub

Change the dimmed filename back to FileName and the commondialog property will also magically change back. This is because VB uses pretty simple method of ensuring variable, method and proprty names all match in your code
 
strongm,
Thank you very much for the response.
What is the solution for this problem. Why does it give me a runtime error?
 
What is the run time error message?

I do notice a couple of things about your code.

- You don't have an "Exit Sub" before the "ErrorHandler:" label so you are falling into the error handling code even when there was no error. As you have structured it that should not be a problem but it's usually not good practice.

- You should be able to replace this
Code:
dlgopen.ShowOpen

If Err.Number = cdlCancel Then
    ' The user canceled.
ElseIf Err.Number <> 0 Then
    ' Unknown error.
    MsgBox "Error " & Format$(Err.Number) & _
           " selecting file." & vbCrLf & _
           Err.Description
Else
    filename2 = dlgopen.Filename
End If
with this
Code:
dlgopen.ShowOpen
filename2 = dlgopen.Filename
Exit Sub
The stuff I eliminated is error handling code and you already have an "On Error" statement that will redirect control to the error handler if an error is raised.

- It may also be useful to upgrade your error handler to
Code:
errorhandler:
filename2 = " "
If Err.Number <> cdlCancel Then
    MsgBox "An error has occured. Please contact the developer" & _
           Err.Number & " - " & Err.Description, vbCritical, "Error"
End If
 
>Why does it give me a runtime error?

As I pointed out before, it has nothing to do with capitalisation. Now, as to what is actually causing the problem ... well, it is difficult to tell.

A few of questions:

1) You do have Option Explicit set, don't you?
2) Given that you have an error handler in your routine, how are you determining that the code is breaking on the line that you suggest and with the error code that you suggest (my point being that the code you have shown us may not be the code that gave the diagnosis; we need to see the original code)
3) And we may need to see more code, since it is clear that you are using public variables - and there may be some issues with this that are causing the problem
 
Hi,
I have just the same problem "runtime error 5" when opening a commomdialog (showopen). This díd not occure in the IDE only running the EXE.
In my appliction it turned out to be a problem with the form.SetFocus. As I want my form to keep always the focus, I used the LostFocus-event to call the form.SetFocus. In the case of an open CommomDialog this fails and causes the runtime error 5.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top