473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Access, OLE & VB.NET

I couldn't decide which newsgroup to post this in so I thought I would start
here.

I can create an "OLE Object" field manually in Microsoft Access and insert
an Excel spreadsheet or a Word document, etc. into the field.

I would like to be able to programmatically do the same thing. Basicaly I
want to be able to add and retrieve and reconstitute Excel spreadsheets and
Word documents, etc. I don't need to be able to programmatically create the
Access database and I'm pretty sure that I can do that with other code I
have already written.

Can someone point me in the right direction? I have already looked at the
following link and while I have a little experience with C++ it would be
much more preferable to write the app in VB.NET.

http://www.codeguru.com/Cpp/data/mfc...icle.php/c1123

Thanks.

Stan

Stan Smith
ACT! Certified Consultant
ADS Programming Services
2320 Highland Avenue South
Suite 290
Birmingham, AL 35205
205-222-1661
www.adsprogramming.com
ssmith_at_adsprogramming.com

Mar 15 '06 #1
2 2940
This may help - it's VB6 code but should give you an idea. I need to
upgrade it to VB.NET too actually.

It's a function I use for inserting image files into Access - should
work for any binary file. I found a few examples on ms.com and elsewhere
but none of them seemed to work that well. I rewrote the below function
and it seems to work ok.

HTH

'This function updates the first row in a recordset with the passed 'file.
'The recordset should contain the primary key of the record as well as
'the BLOB field otherwise you get a "missing key" error.

Private Const BlockSize = 32000

Function fcnLoadBlobIntoDatabase(SourceFile As String, _
r As Recordset, sField As String) As Boolean

Dim NumBlocks As Integer, F As Integer, i As Integer
Dim FileLength As Long, LeftOver As Long
Dim FileData() As Byte

On Error GoTo Err_ReadBLOB

'open the source file for access
F = FreeFile
Open SourceFile For Binary Access Read As F

' Get the length of the file.
FileLength = LOF(F)
If FileLength = 0 Then Exit Function

' Calculate the number of blocks to read and leftover bytes.
LeftOver = FileLength Mod BlockSize
NumBlocks = (FileLength - LeftOver) \ BlockSize

'Now load read the file in blocks and load into FileData
Do While i <= NumBlocks
'size FileData as per blocksize of the same size
'as the remaining data
If i < NumBlocks Then
ReDim FileData(BlockSize - 1)
Else
ReDim FileData(LeftOver - 1)
End If

'Read the data from the file into FileData
Get F, , FileData

'append to our BLOB field
r(sField).AppendChunk (FileData)
'increment our block counter
i = i + 1
Loop

' Update the record - we're done
r.Update

fcnLoadBlobIntoDatabase = True

Err_ReadBLOB:
r.close
On Error Resume Next
Close F
End Function

And for getting the BLOB out again.

Function fcnGetBlobFromDatabase(r As Recordset, sField As String,
DestinationFile As String) As Boolean

Dim NumBlocks As Integer, F As Integer, i As Integer
Dim FileLength As Long, LeftOver As Long
Dim FileData() As Byte

On Error GoTo Err_WriteBLOB

' Get the size of the field.
FileLength = r(sField).ActualSize
If FileLength = 0 Then Exit Function

' Calculate number of blocks to write and leftover bytes.
LeftOver = FileLength Mod BlockSize
NumBlocks = (FileLength - LeftOver) / BlockSize

' Remove any existing destination file.
F = FreeFile
Open DestinationFile For Output As F
Close F

' Open the destination file.
Open DestinationFile For Binary As F

'Loop through the blob
Do
FileData() = r(sField).GetChunk(BlockSize)
Put F, , FileData()
Loop Until UBound(FileData) < BlockSize - 1

fcnGetBlobFromDatabase = True

Err_WriteBLOB:
On Error Resume Next
Close F

End Function
Stan Smith wrote:
I couldn't decide which newsgroup to post this in so I thought I would start
here.

I can create an "OLE Object" field manually in Microsoft Access and insert
an Excel spreadsheet or a Word document, etc. into the field.

I would like to be able to programmatically do the same thing. Basicaly I
want to be able to add and retrieve and reconstitute Excel spreadsheets and
Word documents, etc. I don't need to be able to programmatically create the
Access database and I'm pretty sure that I can do that with other code I
have already written.

Can someone point me in the right direction? I have already looked at the
following link and while I have a little experience with C++ it would be
much more preferable to write the app in VB.NET.

http://www.codeguru.com/Cpp/data/mfc...icle.php/c1123

Thanks.

Stan

Stan Smith
ACT! Certified Consultant
ADS Programming Services
2320 Highland Avenue South
Suite 290
Birmingham, AL 35205
205-222-1661
www.adsprogramming.com
ssmith_at_adsprogramming.com

Mar 16 '06 #2
Gman,

Thanks. I'll give it a try and see what happens.

Stan

Stan Smith
ACT! Certified Consultant
ADS Programming Services
2320 Highland Avenue South
Suite 290
Birmingham, AL 35205
205-222-1661
www.adsprogramming.com
ssmith_at_adsprogramming.com

See what 40 Crystal Reports can do for ACT! 6 at
www.crystalclearreports.com
"Gman" <nah> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
This may help - it's VB6 code but should give you an idea. I need to
upgrade it to VB.NET too actually.

It's a function I use for inserting image files into Access - should work
for any binary file. I found a few examples on ms.com and elsewhere but
none of them seemed to work that well. I rewrote the below function and it
seems to work ok.

HTH

'This function updates the first row in a recordset with the passed 'file.
'The recordset should contain the primary key of the record as well as
'the BLOB field otherwise you get a "missing key" error.

Private Const BlockSize = 32000

Function fcnLoadBlobIntoDatabase(SourceFile As String, _
r As Recordset, sField As String) As Boolean

Dim NumBlocks As Integer, F As Integer, i As Integer
Dim FileLength As Long, LeftOver As Long
Dim FileData() As Byte

On Error GoTo Err_ReadBLOB

'open the source file for access
F = FreeFile
Open SourceFile For Binary Access Read As F

' Get the length of the file.
FileLength = LOF(F)
If FileLength = 0 Then Exit Function

' Calculate the number of blocks to read and leftover bytes.
LeftOver = FileLength Mod BlockSize
NumBlocks = (FileLength - LeftOver) \ BlockSize

'Now load read the file in blocks and load into FileData
Do While i <= NumBlocks
'size FileData as per blocksize of the same size
'as the remaining data
If i < NumBlocks Then
ReDim FileData(BlockSize - 1)
Else
ReDim FileData(LeftOver - 1)
End If

'Read the data from the file into FileData
Get F, , FileData

'append to our BLOB field
r(sField).AppendChunk (FileData)
'increment our block counter
i = i + 1
Loop

' Update the record - we're done
r.Update

fcnLoadBlobIntoDatabase = True

Err_ReadBLOB:
r.close
On Error Resume Next
Close F
End Function

And for getting the BLOB out again.

Function fcnGetBlobFromDatabase(r As Recordset, sField As String,
DestinationFile As String) As Boolean

Dim NumBlocks As Integer, F As Integer, i As Integer
Dim FileLength As Long, LeftOver As Long
Dim FileData() As Byte

On Error GoTo Err_WriteBLOB

' Get the size of the field.
FileLength = r(sField).ActualSize
If FileLength = 0 Then Exit Function

' Calculate number of blocks to write and leftover bytes.
LeftOver = FileLength Mod BlockSize
NumBlocks = (FileLength - LeftOver) / BlockSize

' Remove any existing destination file.
F = FreeFile
Open DestinationFile For Output As F
Close F

' Open the destination file.
Open DestinationFile For Binary As F

'Loop through the blob
Do
FileData() = r(sField).GetChunk(BlockSize)
Put F, , FileData()
Loop Until UBound(FileData) < BlockSize - 1

fcnGetBlobFromDatabase = True

Err_WriteBLOB:
On Error Resume Next
Close F

End Function
Stan Smith wrote:
I couldn't decide which newsgroup to post this in so I thought I would
start here.

I can create an "OLE Object" field manually in Microsoft Access and
insert an Excel spreadsheet or a Word document, etc. into the field.

I would like to be able to programmatically do the same thing. Basicaly
I want to be able to add and retrieve and reconstitute Excel spreadsheets
and Word documents, etc. I don't need to be able to programmatically
create the Access database and I'm pretty sure that I can do that with
other code I have already written.

Can someone point me in the right direction? I have already looked at
the following link and while I have a little experience with C++ it would
be much more preferable to write the app in VB.NET.

http://www.codeguru.com/Cpp/data/mfc...icle.php/c1123

Thanks.

Stan

Stan Smith
ACT! Certified Consultant
ADS Programming Services
2320 Highland Avenue South
Suite 290
Birmingham, AL 35205
205-222-1661
www.adsprogramming.com
ssmith_at_adsprogramming.com


Mar 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: CJM | last post by:
I have a query which produces different results in the Access query builder and in an ASP page (via ADO) An example of the query is: ----------------------------------------------------------...
2
by: cisco | last post by:
Could anyone point me in the direction of a document that describes the difference between JET and Access? I can't seem to find anything. Another question i have is i have this application (.net)...
0
by: TheCoder | last post by:
I am making a D-base with web conectivity for my class project. I have everything working but the subit button sends the data to the correct fields but afterwards it wants to reproduce new blank...
4
by: RichB | last post by:
Hello, The .NET application I have just installed at a client web site is throwing a strange error. At first the applications works without any problems, but after 10 mins or so Access denied...
12
by: PMA | last post by:
Hi all, I am porting a web application from IE 6.0 to FireFox 1.5. I have solved almost all compatibility issues (quite a lot but not too bad) except two of them : 1) Clipboard access thru'...
2
by: freegnu | last post by:
how to declare a friend function that can access two class it will look like the following class A { private: int i; public: A(){} ~A(){} friend void call(A &a, B &b);
1
by: hkhella | last post by:
I receive several flat files daily that need to be improted into my Access Database. Each file record starts with a specific letter or group of letters, the following lines or rows in the flat file...
4
by: Suzette | last post by:
I have an excel file that automatically updates when opened, pulling information from an Access database into into various pivot tables. I created a macro in Access to perform various necessary...
1
by: Webstorm | last post by:
Hi, I hope someone can help me sort this out a bit, Im completely lost. Here is the page I am working on: http://www.knzbusinessbrokers.com/default.asp I have 3 search critera that I need to...
1
by: Brit | last post by:
I have an ASP file that retrieves names from an Access database for 4 different categories of membership, which the visitor to the page selects (corporate, institutional, regular, or student). The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.