This Visual Basic code (immediately below) always gives me errors after I added "redType As String" in the previously empty parenthesis on the first line. Also, before the change "Const strField" was "=" to a fixed term called "Copyright" enclosed in quotes, instead of redType. If it helps, this code (immediately below) inputs code into another Module code (further below).
Public Function TestOpenFirstAttachmentAsT
empFile(re
dType As String)
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Const strTable = "Excel Data"
Const strField = redType
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strTable
)
'rst.MoveNext ' Uncomment this line to go to the 2nd row in the Table.
OpenFirstAttachmentAsTempF
ile rst, strField
rst.Close
End Function
The program above provides values for this formula below. Overall the two formulas are supposed to select an Attachment folder from Access and open the first file only (out of potentially multiple files). Is there a way to make the bottom code open the second file if available in an attachments folder instead of the first file? Thank you very much for your help in advance. I am a relative novice when it comes to this...took one semester of Java programming in college, and I am very new to VB.
Public Function OpenFirstAttachmentAsTempF
ile(ByRef rstCurrent As DAO.Recordset, ByVal strFieldName As String) As String
Dim rstChild As DAO.Recordset2
Dim fldAttach As DAO.Field2
Dim strFilePath As String
Dim strTempDir As String
strTempDir = Environ("Temp") ' Get the Temp directory from the environment variable.
If Right(strTempDir, 1) <> "\" Then strTempDir = strTempDir & "\" ' Make sure the path always ends with a backslash.
Set rstChild = rstCurrent.Fields(strField
Name).Valu
e ' the .Value for a complex field returns the underlying recordset.
strFilePath = strTempDir & rstChild.Fields("FileName"
).Value ' Append the name of the first (and only) attached file to temp dir.
If Dir(strFilePath) <> "" Then ' the file already exists--delete it first.
VBA.SetAttr strFilePath, vbNormal ' remove any file attributes (e.g. read-only) that would block the kill command.
VBA.Kill strFilePath ' delete the file.
End If
Set fldAttach = rstChild.Fields("FileData"
) ' The binary data of the file.
fldAttach.SaveToFile strFilePath
rstChild.Close ' cleanup
VBA.Shell "Explorer.exe " & Chr(34) & strFilePath & Chr(34), vbNormalFocus ' Use Windows Explorer to launch the file.
End Function 'OpenFirstAttachmentAsTemp
File
Start Free Trial