Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your first problem comes from the fact that you didn't carefully read the code in the link you mention.<br> The <code>AddAttachment</code> subroutine is defined as:</p> <pre><code>AddAttachment(ByRef rstCurrent As DAO.Recordset, _ ByVal strFieldName As String, _ ByVal strFilePath As String) </code></pre> <p>This means that it has 3 mandatory parameters:</p> <ul> <li><p><code>rstCurrent</code> an open recordset for the table where you want to store your file. The file will be added to the recordset current record.</p></li> <li><p><code>strFiledName</code>the name of the attachment field where the file will be saved. Your <code>tblAttach</code> table that you created in Access must have at least one Attachment field (and probably other fields as well for information related to the attachment so you can find it, like a document name, and ID, maybe the original path of the document, etc).</p></li> <li><p><code>strFilePath</code> the absolute path to where the file to be attached is located.</p></li> </ul> <p>Your second problem is to let users select the file they want through a file dialog:</p> <pre><code>Public Function SelectFile() As String Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogOpen) With fd .AllowMultiSelect = False .Title = "Please select file to attach" If .show = True Then SelectFile = .SelectedItems(1) Else Exit Function End If End With Set fd = Nothing End Function </code></pre> <p>Call this function <code>SelectFile()</code> to let the user choose a file. The function will return the full path to the file or an empty string if the operation was cancelled or no file selected.</p> <p>For letting the user select the name and location of the file when they want to save the attachment, the code is similar:</p> <pre><code>Public Function SelectSaveAs(initialName As String) As String Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogSaveAs) With fd .Title = "Save Attachment" .InitialFileName = initialName If .show = True Then SelectSaveAs = .SelectedItems(1) Else Exit Function End If End With End Function </code></pre> <p>Call <code>SelectSaveAs("toto.xls")</code> for instance to suggest a name for the attachment and let the user select where they will save it (and they can change the name as well). The function will return the full path to the file where you will save the attachment.</p> <p>Now, you can put everything together.</p> <p>Say you have created a <code>tblAttach</code> that has a <code>Files</code> field in it.<br> We can rewrite the test in the link you mention as such:</p> <pre><code> Dim dbs As DAO.database Dim rst As DAO.RecordSet ' Ask the user for the file Dim filepath As String filepath = SelectFile() ' Check that the user selected something If Len(filepath) = 0 Then Debug.Assert "No file selected!" Exit Sub End If Set dbs = CurrentDb Set rst = dbs.OpenRecordset("tblAttach") ' Add a new row and an attachment rst.AddNew AddAttachment rst, "Files", filepath rst.Update ' Close the recordset rst.Close Set rst = Nothing Set dbs = Nothing </code></pre> <p>To let the user save back the file, you would do something similar: open the recordset, move to the record that contains the file you want to save, ask the user for the filename, then pass all this information to the <code>SaveAttachment</code> subroutine.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload