Public Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Public Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Public Function GetTempFilePath() As String
Dim sPath As String, sTempFile As String
Dim Result As Long, sTempPrefix As String
'Put what you want your temp file prefix to be
sTempPrefix = "AppName"
'initialize the string and get the temp path
sPath = Space(255)
Result = GetTempPath(255, sPath)
'strip Whitespace & null char off of path
sPath = Trim(sPath)
sPath = Mid(sPath, 1, Len(sPath) - 1)
'Get the full path & file name
sTempFile = Space(255)
Result = GetTempFileName(sPath, sTempPrefix, 0&, sTempFile)
'Strip Whitespace & null char off of temp filename
sTempFile = Trim(sTempFile)
sTempFile = Mid(sTempFile, 1, Len(sTempFile) - 1)
GetTempFilePath = sTempFile
End Function
Public Function SaveFileToDB(ByVal sFileName As String, ByRef rstRecordset As ADODB.Recordset) As Boolean
On Local Error GoTo ProcError
'Initialize Stream Object
Dim sStream As New ADODB.Stream
sStream.Type = adTypeBinary
rstRecordset.AddNew
sStream.Open
sStream.LoadFromFile sFileName
rstRecordset.Fields("MyFile").Value = sStream.Read
'update the database
rstRecordset.Update
sStream.Close
'clear the TempFile
Kill sFileName
SaveFileToDB = True
ProcExit:
Exit Function
ProcError:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly + vbCritical, "Error!"
SaveFileToDB = False
GoTo ProcExit
End Function
Public Function GetFileFromDB(ByVal rstRecordset As ADODB.Recordset) As String
On Local Error GoTo ProcError
Dim sStream As New ADODB.Stream
'GetTempFilenmae
sTempFileName = GetTempFilePath()
'Save data to file
sStream.Type = adTypeBinary
sStream.Open
sStream.Write rstRecordset.Fields("MyFile").Value
sStream.SaveToFile sTempFileName, adSaveCreateOverWrite
sStream.Close
GetFileFromDB = sTempFileName
ProcExit:
Exit Function
ProcError:
'MsgBox Err.Number & ": " & Err.Description, vbOKOnly + vbCritical, "Error!"
MsgBox Err.Number & ": " & Err.Description
GoTo ProcExit
End Function