Solved

Storing files as strings

Posted on 1998-12-09
7
165 Views
Last Modified: 2010-05-03
Could someone tell me how I can store a whole file as a VB string?
0
Comment
Question by:genuinegenius
7 Comments
 
LVL 4

Expert Comment

by:idcanada
ID: 1449094

Read the contents and store it in a resource file.
Then you can call them as needed.
0
 

Author Comment

by:genuinegenius
ID: 1449095
Explain
0
 

Accepted Solution

by:
dbiener earned 30 total points
ID: 1449096
Use filelen to get size of file.
Open file for binary read.
Get the entire length of the file into a string var.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:waty
ID: 1449097
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

0
 

Author Comment

by:genuinegenius
ID: 1449098
Cheers

more comments welcome
0
 
LVL 14

Expert Comment

by:waty
ID: 1449099
Did you use my solution?
0
 

Author Comment

by:genuinegenius
ID: 1449100
Not yet, but I'll try it
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now