Userform double click on listbox Error handling for invalid use of null

dellehurley

Board Regular
Joined
Sep 26, 2009
Messages
171
Office Version
  1. 365
Platform
  1. Windows
Hi
I am having trouble with this code below, the if isnull section. The code is to open the associated file after double clicking on the row. The issue arises when you miss double clicking the filename's row (generally by hitting the header row) an error "invalid use of null" displays. I simply wanted a message box to pop up saying that it was invalid and to try again or for nothing to happen you try again.
With the code below the message box opens after every doubleclick, regardless of if it is valid or not.

VBA Code:
Private Sub lstDatabase_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim folderName As String
Dim FileName As String
Dim answer As VbMsgBoxResult
folderName = ThisWorkbook.Path & "\"

FileName = Me.lstDatabase.Value
    If IsNull(FileName) = False Then
        MsgBox "Invalid!" & vbCrLf & "Try Again", vbOKOnly
    End If

On Error GoTo NoFile
    answer = MsgBox("Would you like to open" & vbCrLf & FileName & "?", vbOKCancel, "Multi Media Database")
        If answer = vbYes Then
                    ThisWorkbook.FollowHyperlink folderName & FileName
                    Exit Sub
        End If
NoFile:
MsgBox "File " & FileName & vbCrLf & " is not found in Folder.", vbOKOnly + vbCritical, "File Not Found"
End Sub
NB. The "one error goto" is working correctly and is seperate from the Null issue.
Thanks for your help.
Dannielle
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
How avout
VBA Code:
Private Sub lstDatabase_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim folderName As String
Dim FileName As String
Dim answer As VbMsgBoxResult
folderName = ThisWorkbook.Path & "\"

FileName = Me.lstDatabase.Value
    If IsNull(FileName) = False Then
        MsgBox "Invalid!" & vbCrLf & "Try Again", vbOKOnly
        Exit Sub
    End If

On Error GoTo NoFile
    answer = MsgBox("Would you like to open" & vbCrLf & FileName & "?", vbOKCancel, "Multi Media Database")
        If answer = vbYes Then
                    ThisWorkbook.FollowHyperlink folderName & FileName
                    Exit Sub
        End If
Exit Sub
NoFile:
MsgBox "File " & FileName & vbCrLf & " is not found in Folder.", vbOKOnly + vbCritical, "File Not Found"
End Sub
 
Upvote 0
How avout
VBA Code:
Private Sub lstDatabase_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim folderName As String
Dim FileName As String
Dim answer As VbMsgBoxResult
folderName = ThisWorkbook.Path & "\"

FileName = Me.lstDatabase.Value
    If IsNull(FileName) = False Then
        MsgBox "Invalid!" & vbCrLf & "Try Again", vbOKOnly
        Exit Sub
    End If

On Error GoTo NoFile
    answer = MsgBox("Would you like to open" & vbCrLf & FileName & "?", vbOKCancel, "Multi Media Database")
        If answer = vbYes Then
                    ThisWorkbook.FollowHyperlink folderName & FileName
                    Exit Sub
        End If
Exit Sub
NoFile:
MsgBox "File " & FileName & vbCrLf & " is not found in Folder.", vbOKOnly + vbCritical, "File Not Found"
End Sub
Thanks for your help. Unfortunately this caused every double click to come up with MsgBox "Invalid!" & vbCrLf & "Try Again", vbOKOnly. FileName = Me.lstDatabase.Text did the job.
Dannielle
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top