code error

ck4794

Registered User.
Local time
Yesterday, 22:55
Joined
Oct 12, 2013
Messages
11
so i've bastardized some code together, as follows:
Code:
Const m_strFieldFileName As String = "FileName" ' The name of the attached file
Const m_strFieldFileType As String = "FileType" ' The attached file's extension
Const m_strFieldFileData As String = "FileData" ' The binary data of the file

Sub AddAttachment(ByRef rstCurrent As DAO.Recordset, ByVal strFieldName As String, ByVal strFilePath As String)
    Const CALLER = "AddAttachment"
    'On Error GoTo AddAttachment_ErrorHandler

    Dim rstChild  As DAO.Recordset2
    Dim fldAttach As DAO.Field2

    If Dir(strFilePath) = "" Then ' the specified file does not exist!
        msgbox "The specified input file does not exist: " & vbCrLf & strFilePath, vbCritical, "File not found"
        Exit Sub
    End If

    Set rstChild = rstCurrent.Fields(strFieldName).Value ' the .Value for a complex field returns the underlying Recordset.
    rstChild.AddNew ' add a new row to the child Recordset
    Set fldAttach = rstChild.Fields(m_strFieldFileData) ' set the DAO.Field2 object to the field that holds the binary data.
    fldAttach.LoadFromFile strFilePath ' store the file's contents in the new row.
    rstChild.Update ' commit the new row.
    rstChild.Close ' close the child Recordset.

    Exit Sub
AddAttachment_ErrorHandler:
    'Check for Run-time error '3820': (occurs if the file with the same name is already attached)
    'You cannot enter that value because it duplicates an existing value in the multi-valued lookup or attachment field.
    'Multi-valued lookup or attachment fields cannot contain duplicate values.
    Debug.Print "Error # " & Err.Number & " in " & CALLER & " : " & Err.Description
    If Err.Number <> 3820 Then
        msgbox Err.Description, VbMsgBoxStyle.vbCritical, "Error # " & Err.Number & " in " & CALLER
        Debug.Assert False ' always stop here when debugging
    Else
        msgbox "File of same name already attached", VbMsgBoxStyle.vbCritical, "Cannot attach file"
    End If
    Exit Sub
End Sub 'AddAttachment

Private Sub Command10_Click()
 Dim fDialog As Office.FileDialog
   Dim varFile As Variant
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
   With fDialog
      ' Allow the user to make multiple selections in the dialog box.
      .AllowMultiSelect = False
          
      ' Set the title of the dialog box.
      .title = "Select a File"

      ' Clear out the current filters, and then add your own.
      .Filters.Clear
      .Filters.Add "All Files", "*.*"

      ' Show the dialog box. If the .Show method returns True, the
      ' user picked at least one file. If the .Show method returns
      ' False, the user clicked Cancel.
      If .Show = True Then
         ' Loop through each file that is selected and then add it to the list box.
      Dim rst As DAO.Recordset
            Const strTable = "tblImages"
            Const strField = "File"
            Set rst = CurrentDb.OpenRecordset(strTable)
            rst.AddNew
            AddAttachment rst, strField, varFile
            rst.Update
            rst.MoveLast
            rst.Close
      Else
         msgbox "You clicked Cancel in the file dialog box."
      End If
   End With
End Sub

The idea is, i click a button on a form (frmuploadimages) bound to (tblimages) which saves an attachment to a field in that table called (file). i'll figure out the other details specific to my situation later (like relating these attachments to something.) I just need it to work first.

anyways...
Set fldAttach = rstChild.Fields(m_strFieldFileData) ' set the DAO.Field2 object to the field that holds the binary data.
this line is giving me an "invalid argument". i'm already in over my head so i have no idea what i'm doing troubleshooting this. any help is much appreciated.
 
I see this has been up for awhile with no replies so I'll take a stab at it, or perhaps give you some new things to ponder.

I have done some DAO work but am not a master at it.

However, this doesn't look right to me.

Has this code ever works and now for some reason is not?
or
is this new development which is being debugged?

AS i look at this, I'm not sure you need to set an object for the field in the rstChild
You can refer to any field in rstchild as
rstField!FieldName

for that matter, I'm not even sure you even need the rstChild?

From what I'm seeing, the valid members of .Fields are ".Append", ".Count", ".Delete", and ".Refresh".

After adding a new record to rstcurrent, have you tried something like:

rstCurrent!strFieldName.LoadFromFile strFilePath
 

Users who are viewing this thread

Back
Top Bottom