To allow portability of an Access database, I want to force the user to select its folder if it's copied to another computer. I have run into a stumbling block, though, in trying to test for the folder's path.
In the code below, the if
statement block works when not commented out, but the while
statement above it does not. I get:
Run-time error '5': Invalid procedure call or argument.
I've looked at Tools > References
, and the appropriate pieces seem to be in place. I've tried fd.SelectedItems.Count = 0
, but that doesn't prevent an unwanted string from being passed.
Private Sub btn_CorrectPath_Click()
Dim sHostName As String, strSQL As String, sFolder As String
Dim rs As Recordset, db As Database, fd As FileDialog
Dim intResult As Integer
Set db = CurrentDb
' Get Host Name / Get Computer Name
sHostName = Environ$("computername")
Set rs = CurrentDb.OpenRecordset("SELECT * FROM t_ComputerInfo")
If rs!ComputerName <> sHostName Then
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
fd.AllowMultiSelect = False
fd.Title = "Select database folder"
intResult = fd.Show
While intResult = False
intResult = fd.Show
While fd.SelectedItems(1) = vbNullString 'folder path was not selected
intResult = fd.Show
Wend
Wend
sFolder = fd.SelectedItems(1)
strSQL = "UPDATE t_ComputerInfo SET [t_ComputerInfo].[ComputerName] = '" & sHostName & _
& " [t_ComputerInfo].[DBPath] = '" & sFolder & "' WHERE [t_ComputerInfo].[ID] = 1"
CurrentDb.Execute strSQL, dbFailOnError
' If fd.Show = True Then 'Action button was pressed
' MsgBox ("Directory was given. fd.SelectedItems(1)= " & fd.SelectedItems(1))
' If fd.SelectedItems(1) <> vbNullString Then
' sFolder = fd.SelectedItems(1)
' strSQL = "UPDATE t_ComputerInfo SET [t_ComputerInfo].[ComputerName] = '" & sHostName & _
"', [t_ComputerInfo].[DBPath] = '" & sFolder & "' WHERE [t_ComputerInfo].[ID] = 1"
' MsgBox ("SQL statement = " & vbCrLf & strSQL)
' CurrentDb.Execute strSQL, dbFailOnError
' End If
' Else 'Cancel button was pressed
' sFolder = fd.SelectedItems(1)
' MsgBox ("The location of the database is required and will be requested later. fd.SelectedItems(1)= " & sFolder)
' End If
Set fd = Nothing
End If
db.Close
End Sub