This project took a backseat with other things I had going on, but I did eventually find a solution and would like to post a solution for anyone that comes across this in the future.
I wanted to be able to attach multiple pictures to a single main table record, so I created a new table to hold the file paths (along with their associated ID from the main table) and established a one-to-many relationship between the main table and new table. I also put the image and its control (the file path) inside of a subform within my main form so that I could scroll through multiple images while keeping my main form consistent.
Step 1: Create a module
For the very new users to Access, this is done by clicking the "Create" ribbon and then the "Module" button.
In the module, copy in the entirety of the code found here: http://access.mvps.org/access/api/api0001.htm
Be sure to name the rename the module to something you can later call. I used "basCommDialog".
Step 2: Call the file picker and record the existing file path
Create a command button to launch the file picker. Within the Click event, copy the code below:
Code:
' Call file picker from basCommDialog standard module
Dim strFilter As String
Dim strPath As String
' Format file picker
strFilter = ahtAddFilterItem(strFilter, _
"All Pictures(*.emf; *.wmf; *.jpg; *.jpeg; *.jfif; *.jpe; *.png; *.bmp; *.dib; *.rle; *.gif; *.emz; *.wmz; *.pcz; *.tif; *.tiff; *.cgm; *.eps; *.pct; *.pict; *.wpg) ", _
"*.emf; *.wmf; *.jpg; *.jpeg; *.jfif; *.jpe; *.png; *.bmp; *.dib; *.rle; *.gif; *.emz; *.wmz; *.pcz; *.tif; *.tiff; *.cgm; *.eps; *.pct; *.pict; *.wpg")
strFilter = ahtAddFilterItem(strFilter, _
"All Files (*.*)", _
"*.*")
' Save existing file path and name
strPath = ahtCommonFileOpenSave(Filter:=strFilter, _
OpenFile:=True, _
DialogTitle:="Select a file", _
Flags:=ahtOFN_HIDEREADONLY)
There will be more code included before the End Sub; continue reading.
Step 3: Give the file a new name and set the file path
Now, I am very new to Access and programming, and I was looking to find a way to ensure that every file I saved to a shared folder would have a unique name. You can absolutely modify this section so that the file name is different (my code might be clunky; I honestly don't know). I decided to record the current time and date as a numerical string and use that as the new file name. (This is still within the Click event).
Code:
' Note current time and date
Dim curTimeDate As String
curTimeDate = Now
' Format time and date as numerical string
'Start by breaking it into numerical values
Dim monthTimeDate As String
Dim dayTimeDate As String
Dim yearTimeDate As String
Dim hourTimeDate As String
Dim minTimeDate As String
Dim secTimeDate As String
monthTimeDate = Format(curTimeDate, "mm")
dayTimeDate = Format(curTimeDate, "dd")
yearTimeDate = Format(curTimeDate, "yyyy")
hourTimeDate = Format(curTimeDate, "hh")
minTimeDate = Format(curTimeDate, "nn")
secTimeDate = Format(curTimeDate, "ss")
' Combine numerical values into one long string of numbers
Dim numTimeDate As String
numTimeDate = [monthTimeDate] & [dayTimeDate] & [yearTimeDate] & "_" & [hourTimeDate] & [minTimeDate] & [secTimeDate]
' Set new file path
Dim strNewPath As String
strNewPath = "H:\Folder\Subfolder\" & numTimeDate & ".jpg"
The folder used in the file path needs to already exist and be accessible.
Step 4: Copy the existing file to the new file path and name
(This is still within the Click event)
Code:
' Copy file to new file path
' FileCopy OLD PATH & NAME, NEW PATH & NAME
FileCopy strPath, strNewPath
Step 5: Create a new record in the table holding the file paths
I had to temporarily turn off the warnings in this code so that I would stop getting a message asking if I was sure I wanted to create a new record in a table (This isn't a requirement, but I thought it aided in the user experience). After the new file path is saved in the table, I launched a form with a simple message that said "Image saved!" This could also be done with a text box or skipped entirely. (This is still within the Click event)
Code:
' Create new record in t_ImagePaths table
' and record MainFormID and new file path
' Turn off Warnings
On Error GoTo ErrorHandler
DoCmd.SetWarnings False
Dim NewPic As String
NewPic = "INSERT INTO t_ImagePaths(MainTableID, ImagePath)" _
& "VALUES(Forms!f_MainForm.MainFormID, '" & strNewPath & "' );"
DoCmd.RunSQL NewPic
' Launch message box form f_ImageSaved
DoCmd.OpenForm "f_ImageSaved", acNormal, "", "", , acDialog
' Turn warnings back on
ExitHandler:
DoCmd.SetWarnings True
Exit Sub
ErrorHandler:
MsgBox Err.Number & Chr(9) & Err.Description
Resume ExitHandler
Moving Forward
I am still working on a way to reload the main form to include the now added images. But I hope this helps someone who comes across it in the future! This has been a huge learning experience for me. Good luck!