I have a database that keeps track of water utility repair digs. When the repair dig is complete the crew lead fills out a dig card. I'm scanning the cards (because the cards have hand drawn details of the repair) to pdf. The pdf will be kept in a separate directory and opened into the default pdf viewer with a button click. The code I'm having a problem with opens a file dialog and gets the full unc file path of the pdf and writes it to a field named ViewDigCard. It works accept it always write to the first record. I need a reference to the current record to insure that's where the file path is written. I'm new to Access and don't quite know what I'm doing, but I think the fix is probably easy. Any help would be greatly appreciated. See code below.
Code:
Private Sub btnGetLink_Click()
' Get pdf link
On Error GoTo ErrorHandler
Dim dlg As FileDialog
Dim selectedFile As Variant
Dim currentRecord As Recordset
' Refresh the data set
Me.Recordset.Requery
' Create a file dialog
Set dlg = Application.FileDialog(msoFileDialogFilePicker)
' Set the initial directory to the network directory "DigCards"
dlg.InitialFileName = "\\osdshare\Utilities\Dig Card Database\DigCardImages"
' Set the title and filter
dlg.Title = "Select a file from DigCardsImages Directory"
dlg.Filters.Clear
dlg.Filters.Add "pdf Files", "*.pdf"
' Show the dialog and capture the selected file
If dlg.Show = -1 Then
selectedFile = dlg.SelectedItems(1)
' Write the path to the field "ViewDigCard" in the current record
Set currentRecord = CurrentDb.OpenRecordset("Dig Cards")
currentRecord.Edit
currentRecord("ViewDigCard") = selectedFile
currentRecord.Update
currentRecord.Close
Else
' User canceled the dialog
MsgBox "File selection canceled.", vbInformation, "Information"
End If
' Stop
' Clean up
Set dlg = Nothing
Exit Sub
ErrorHandler:
' Handle any errors here (e.g., display a message box)
MsgBox "Error: " & Err.Description, vbExclamation, "Error"
End Sub