|
|
Хранение файлов в поле MEMO таблицы БД (DAO)
Пример:
Private Sub TestAll()
Dim x As Long
Dim str As String
On Error GoTo TestAll_Err
str = CurrentProject.Path & "\Shablon\"
esPutFilesToTable str, "aflTemplates", "tplName", "tplBody", "*.dbf"
Exit Sub
str = CurrentProject.Path & "\Shablon\COPY2\"
If Dir(str, vbDirectory) = "" Then PrepareFolders str
x = esOutPutFilesFromTable(str, "aflTemplates", "tplName", "tplBody")
If x = 0 Then MsgBox "OK!", vbInformation, "Вывод файла"
TestAll_Bye:
Exit Sub
TestAll_Err:
MsgBox "Ошибка " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
"в процедуре: TestAll", vbCritical, "Error in FOMS Reports v030-03"
Resume TestAll_Bye
End Sub
Public Function esOutPutOneFileFromTable(strFilePath As String, _
strTableName As String, _
strFieldForFileName As String, _
strFieldForFileBody As String, _
strFileName As String)
Dim daoRst As DAO.Recordset
Dim str As String
Dim v As Variant
Dim i As Long
Dim x As Long
On Error GoTo esOutPutOneFileFromTableERR
If Dir(strFilePath) <> "" Then Kill strFilePath
DoEvents
str = "SELECT * FROM " & strTableName & " WHERE " & strFieldForFileName & " =
Set daoRst = CurrentDb.OpenRecordset(str, dbOpenSnapshot)
If daoRst.EOF = True Then GoTo esOutPutOneFileFromTableExit
v = daoRst.Fields(strFieldForFileBody)
Reset
Open strFilePath For Output As #1
Print #1, v;
Close #1
esOutPutOneFileFromTableExit:
On Error Resume Next
daoRst.Close
Set daoRst = Nothing
Close #1
DoEvents
Exit Function
esOutPutOneFileFromTableERR:
esOutPutOneFileFromTable = Err.Number
MsgBox "Error " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
"in procedure esOutPutOneFileFromTable of Module modData", vbCritical, "Error!"
Err.Clear
Resume esOutPutOneFileFromTableExit
End Function
Private Sub esPutFilesToTable(strStoragePath As String, _
strTableName As String, _
strFieldForFileName As String, _
strFieldForFileBody As String, _
Optional strExt As String = "*.*")
Dim Msg As String, Style As Integer
Dim strFileName As String
Dim strFilePath As String
Dim varVal As Variant
Dim daoRst As DAO.Recordset
Dim i As Long
Dim lngFileLen As Long
On Error GoTo esPutFilesToTableERR
If Mid(strStoragePath, Len(strStoragePath), 1) = "\" Then
strStoragePath = Mid(strStoragePath, 1, Len(strStoragePath) - 1)
End If
If Dir(strStoragePath, vbDirectory) = "" Then
MsgBox "Указанный путь к файлам " & vbCrLf & _
strStoragePath & vbCrLf & _
"не существует!!!", vbCritical
Exit Sub
End If
Msg = "Имеющиеся данные из таблицы =" & strTableName & "= будут удалены..." & vbCrLf & _
"Вы уверены ???"
Style = vbYesNo + vbExclamation + vbDefaultButton1
If MsgBox(Msg, Style, "Предупреждение") = vbNo Then Exit Sub
DoCmd.SetWarnings False
CurrentDb.Execute "DELETE * FROM " & strTableName
Set daoRst = CurrentDb.OpenRecordset(strTableName, dbOpenDynaset)
strFileName = Dir(strStoragePath & "\" & strExt)
With daoRst
Do While strFileName <> ""
strFilePath = strStoragePath & "\" & strFileName
lngFileLen = FileLen(strFilePath)
Reset
Open strFilePath For Binary Access Read Lock Read As #1
varVal = Input(lngFileLen, #1)
Close #1
.AddNew
.Fields(strFieldForFileName) = strFileName
.Fields(strFieldForFileBody) = varVal
.Update
strFileName = Dir
varVal = Null
i = i + 1
Loop
End With
daoRst.Close
Set daoRst = Nothing
MsgBox "В таблицу принято - " & i & " файлов"
Exit Sub
esPutFilesToTableERR:
MsgBox "Произошла ошибка №" & Err.Number & vbCrLf & Err.Description
Err.Clear
End Sub
Public Function esOutPutFilesFromTable(strStoragePath As String, _
strTableName As String, _
strFieldForFileName As String, _
strFieldForFileBody As String) As Long
Dim strFileName As String
Dim strFilePath As String
Dim daoRst As DAO.Recordset
Dim i As Long
Dim x As Long
On Error GoTo esOutPutFilesFromTableERR
If Mid(strStoragePath, Len(strStoragePath), 1) = "\" Then
strStoragePath = Mid(strStoragePath, 1, Len(strStoragePath) - 1)
End If
If Dir(strStoragePath, vbDirectory) = "" Then
MsgBox "Указанный путь к файлам " & vbCrLf & _
strStoragePath & vbCrLf & _
"не существует!!!", vbCritical
Exit Function
End If
Set daoRst = CurrentDb.OpenRecordset(strTableName, dbOpenSnapshot)
If daoRst.EOF = True Then GoTo esOutPutFilesFromTableExit
With daoRst
.MoveLast
.MoveFirst
x = .RecordCount
For i = 1 To x
strFileName = .Fields(strFieldForFileName)
strFilePath = strStoragePath & "\" & strFileName
Reset
Open strFilePath For Output As #1
Print #1, .Fields(strFieldForFileBody);
Close #1
If i < x Then .MoveNext
Next i
End With
esOutPutFilesFromTableExit:
On Error Resume Next
daoRst.Close
Set daoRst = Nothing
MsgBox "Из таблицы скопировано - " & x & " файлов"
Exit Function
esOutPutFilesFromTableERR:
esOutPutFilesFromTable = Err.Number
MsgBox "Произошла ошибка №" & Err.Number & vbCrLf & Err.Description
Err.Clear
End Function
|
|