Display error messages

ds_8805

Registered User.
Local time
Today, 04:07
Joined
Jan 21, 2010
Messages
70
Hi, I have search form. When I click on a button for a particular productID that I had searched earlier, the folder containing more details abt the product will open up. The code is such:


Private Sub DisplayDrawing_Click()
DisplayDrawing.HyperlinkAddress = "d:\Autocad\" & Combo31.Value
End Sub

however I would like it to display an error msg(in a msgbox) if the folder for a specific product id cannot be found. How do i check whether the link is valid and display this error msg?

Thanks for ur help :)
 
You can use the Dir-function to test if a file exist.

Code:
If Dir("c:\folder1\somefile.txt") <> "" Then
   ' The file exitst, do your thing
Else
   MsgBox " The file dosen't exist"
End If

JR
 
Hey thanks for ur reply. I finally managed to get smthg. This is my updated code:

Private Sub DisplayDrawing_Click()
Dim fso
Dim folder As String
folder = "d:\Autocad\" & Combo31.Value
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(folder) Then
'MsgBox folder & " is a valid folder/path."
DisplayDrawing.HyperlinkAddress = "d:\Autocad\" & Combo31.Value
Else
MsgBox "File does not exist"
DisplayDrawing.HyperlinkAddress = " "
End If


End Sub
 
Glad you got it working, but I have some comments.


Always declare variabels as SOMETHING, without it it will default as Variant which in somecases can give you suprises. A naturally choise here is:

Dim fso As Object

Also it is vital that you release any declared object's , recordsets from memory when you are done with them. Indenting code and use an errorhandler is not a bad idea, use it.

Code:
Private Sub DisplayDrawing_Click()
Dim fso As [COLOR=red]Object[/COLOR]
Dim folder As String
   On Error GoTo DisplayDrawing_Click_Error
 
folder = "d:\Autocad\" & Combo31.value
 
Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FolderExists(folder) Then
        'MsgBox folder & " is a valid folder/path."
        DisplayDrawing.HyperlinkAddress = "d:\Autocad\" & Combo31.value
    Else
        MsgBox "File does not exist"
        DisplayDrawing.HyperlinkAddress = " "
End If
[COLOR=red][/COLOR] 
[COLOR=red]Set fso = Nothing[/COLOR]  'Release fso object from memory
   On Error GoTo 0
   Exit Sub
 
DisplayDrawing_Click_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure DisplayDrawing_Click of VBA Document Form_fTest"
 
End Sub

JR
 
Hey sorry for the late reply. Thanks I get a clearer picture now. It really helped! :D
 

Users who are viewing this thread

Back
Top Bottom