genuinegenius
asked on
Storing files as strings
Could someone tell me how I can store a whole file as a VB string?
ASKER
Explain
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is how I do in my applications, it works very well :
record.AddNew
record("Item") = nItem
record("Filename") = sFilename
record("Size") = FileLen(sFilename)
record("Type") = nType
record("LastModified") = Now
Call SaveFileToDB(sFilename, record("OLEModule"))
record.Update
.
Function SaveFileToDB(sSource As String, BynaryField As Field) As Integer
' *** Will save a file to a Binary field
Dim nNumBlocks As Integer
Dim nFile As Integer
Dim nI As Integer
Dim nFileLen As Long
Dim nLeftOver As Long
Dim sFileData As String
Dim Retval As Variant
Dim nBlockSize As Long
On Error GoTo Error_SaveFileToDB
nBlockSize = 32000 ' Set size of chunk.
' *** Open the sSource file.
nFile = FreeFile
Open sSource For Binary Access Read As nFile
' *** Get the length of the file.
nFileLen = LOF(nFile)
If nFileLen = 0 Then
SaveFileToDB = 0
Exit Function
End If
' *** Calculate the number of blocks to read and nLeftOver bytes.
nNumBlocks = nFileLen \ nBlockSize
nLeftOver = nFileLen Mod nBlockSize
' *** Read the nLeftOver data, writing it to the table.
sFileData = String$(nLeftOver, 32)
Get nFile, , sFileData
BynaryField.AppendChunk (sFileData)
' *** Read the remaining blocks of data, writing them to the table.
sFileData = String$(nBlockSize, Chr$(32))
For nI = 1 To nNumBlocks
Get nFile, , sFileData
BynaryField.AppendChunk (sFileData)
Next
Close #nFile
SaveFileToDB = nFileLen
SaveFileToDB = 0
Exit Function
Error_SaveFileToDB:
SaveFileToDB = -Err
Exit Function
End Function
record.AddNew
record("Item") = nItem
record("Filename") = sFilename
record("Size") = FileLen(sFilename)
record("Type") = nType
record("LastModified") = Now
Call SaveFileToDB(sFilename, record("OLEModule"))
record.Update
.
Function SaveFileToDB(sSource As String, BynaryField As Field) As Integer
' *** Will save a file to a Binary field
Dim nNumBlocks As Integer
Dim nFile As Integer
Dim nI As Integer
Dim nFileLen As Long
Dim nLeftOver As Long
Dim sFileData As String
Dim Retval As Variant
Dim nBlockSize As Long
On Error GoTo Error_SaveFileToDB
nBlockSize = 32000 ' Set size of chunk.
' *** Open the sSource file.
nFile = FreeFile
Open sSource For Binary Access Read As nFile
' *** Get the length of the file.
nFileLen = LOF(nFile)
If nFileLen = 0 Then
SaveFileToDB = 0
Exit Function
End If
' *** Calculate the number of blocks to read and nLeftOver bytes.
nNumBlocks = nFileLen \ nBlockSize
nLeftOver = nFileLen Mod nBlockSize
' *** Read the nLeftOver data, writing it to the table.
sFileData = String$(nLeftOver, 32)
Get nFile, , sFileData
BynaryField.AppendChunk (sFileData)
' *** Read the remaining blocks of data, writing them to the table.
sFileData = String$(nBlockSize, Chr$(32))
For nI = 1 To nNumBlocks
Get nFile, , sFileData
BynaryField.AppendChunk (sFileData)
Next
Close #nFile
SaveFileToDB = nFileLen
SaveFileToDB = 0
Exit Function
Error_SaveFileToDB:
SaveFileToDB = -Err
Exit Function
End Function
ASKER
Cheers
more comments welcome
more comments welcome
Did you use my solution?
ASKER
Not yet, but I'll try it
Read the contents and store it in a resource file.
Then you can call them as needed.