THANKS SO MUCH for everyone’s responses… I really DO appreciate the help. Because there were three responses, I attempted to digest them all, and I have listed the results below:
___________________
ItsMe’s Response: I added the line Me.fldNotesLink=strFilePath to my first code. This solution partially worked. The command button does let me select a file, but when I subsequently click on my hyperlink field (fldNotesLink), nothing happens… it doesn’t open the file like a hyperlink should. HOWEVER, when I edit the field, I can see that the filepath was indeed inserted in the “Text to Display” portion, just NOT the “Address” portion that it needs to execute the hyperlink.
So, I guess the final question is: how do I get the strFilePath variable to transfer to the “Address” part of the hyperlink field instead of the “Text to Display” part? Here’s what I have so far:
Code:
Private Sub cmdPickLink_Click()
Dim strFilePath As String
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Locate a file to Import"
.ButtonName = "Import"
.Filters.Clear
.InitialFileName = "\\DM-SERVER\DM-Data\Inventory\Items\picPickFile.JPG"
.InitialView = msoFileDialogViewThumbnail
If .Show = 0 Then
'There is a problem
Exit Sub
End If
'Save the first file selected
strFilePath = Trim(.SelectedItems(1))
End With
strTable = "tblItems"
Me.fldNotesLink = strFilePath
End Sub
__________________________________________________ _
Atuhacek and June 7 responses:
If I understand y’all’s responses correctly, the returned file path in my first code was stored in a string variable called strFilePath, but in order to be compatible with the hyperlink format, I need for it to be in the “variant” format.
Therefore, I tried modifying my code as follows:
1. Line 3: I changed strFilePath to VARIANT (instead of String).
2. Line 19: I used the CStr to convert it to a string.
3. Line 20: Then I used the procedure that June7 gave me to assign it to a variable (that I want to eventually assign to my text box):
Obviously I didn’t understand because when I clicked my command button, I got a compile error at Line 19 (CStr) with the following code:
Code:
Private Sub cmdPickFile_Click()
Dim strTable As String
Dim strFilePath As Variant
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Locate a file to Import"
.ButtonName = "Import"
.Filters.Clear
.InitialFileName = "\\DM-SERVER\DM-Data\Inventory\Items\picPickFile.JPG"
.InitialView = msoFileDialogViewThumbnail
If .Show = 0 Then
'There is a problem
Exit Sub
End If
'Save the first file selected
strFilePath = Trim(.SelectedItems(1))
End With
strTable = "tblItems"
CStr(.SelectedItems(1))
Dim varFile As Variant
If .Show = -1 Then
For Each varFile In .SelectedItems
strFilePath = Trim(.SelectedItems(1))
Next
End If
End Sub
______________________________
I then tried the code that Atuhacek gave me, and I changed “Table1” to my “tblItems”, and “XX” to “fldNotesLink” (my hyperlink field) to match my situation.
BUT I’m not sure how to enter this code into the command button. If I choose the Event Procedure option on the “On Click” event, it inserts the code between a Private Sub and End Sub commands, then it gives me a compile error: End Sub Expected. If I delete the beginning Private Sub and ending End Sub commands, then nothing happens.
So, I guess my question is: Do I use the Event Procedure to enter this code, or should I be doing something different?
Code:
Function cmdPickFile() As Variant
Dim var As String
Dim objDialog As Object
Dim rst As Object
Set rst = CurrentDb.OpenRecordset("Table1")
Set objDialog = Application.FileDialog(3)
With objDialog
.AllowMultiSelect = False
.Show
If .SelectedItems.Count = 0 Then
MsgBox "No file selected."
Else
var = .SelectedItems(1)
End If
End With
Set objDialog = Nothing
With rst
.AddNew
!xx = fGetBaseFileName(var) & "#" & var
.Update
End With
rst.Close
cmdPickFile = var
End Function
Public Function fGetBaseFileName(strFilePath As String) As String
'This Function accepts the Absolute Path to a File and returns the Base File
'Name (File Name without the Extension)
'Make absolutely sure that it is a valid Path/Filename
If Dir$(strFilePath) = "" Then Exit Function
Dim strFileName As String
Dim strBaseFileName As String
strFileName = Right$(strFilePath, Len(strFilePath) - InStrRev(strFilePath, "\"))
strBaseFileName = Left$(strFileName, InStr(strFileName, ".") - 1)
fGetBaseFileName = strBaseFileName
End Function