home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Power Pack / Visual_Basic4_Power_Pack.bin / vb4files / blobtest / oletest.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-11-20  |  5.6 KB  |  181 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   3780
  5.    ClientLeft      =   1155
  6.    ClientTop       =   1725
  7.    ClientWidth     =   6255
  8.    Height          =   4185
  9.    Left            =   1095
  10.    LinkTopic       =   "Form1"
  11.    LockControls    =   -1  'True
  12.    ScaleHeight     =   3780
  13.    ScaleWidth      =   6255
  14.    Top             =   1380
  15.    Width           =   6375
  16.    Begin VB.CommandButton cmdPut 
  17.       Caption         =   "Put Object"
  18.       Height          =   495
  19.       Left            =   4800
  20.       TabIndex        =   5
  21.       Top             =   960
  22.       Width           =   1215
  23.    End
  24.    Begin VB.CommandButton cmdGet 
  25.       Caption         =   "Get Object"
  26.       Height          =   495
  27.       Left            =   4800
  28.       TabIndex        =   4
  29.       Top             =   300
  30.       Width           =   1215
  31.    End
  32.    Begin VB.CommandButton Command3 
  33.       Caption         =   "Edit"
  34.       Height          =   495
  35.       Left            =   3060
  36.       TabIndex        =   3
  37.       Top             =   960
  38.       Width           =   1215
  39.    End
  40.    Begin VB.CommandButton Command2 
  41.       Caption         =   "Update"
  42.       Height          =   495
  43.       Left            =   1680
  44.       TabIndex        =   2
  45.       Top             =   960
  46.       Width           =   1215
  47.    End
  48.    Begin VB.CommandButton Command1 
  49.       Caption         =   "Add"
  50.       Height          =   495
  51.       Left            =   300
  52.       TabIndex        =   1
  53.       Top             =   960
  54.       Width           =   1215
  55.    End
  56.    Begin VB.TextBox txtName 
  57.       DataField       =   "Name"
  58.       DataSource      =   "Data1"
  59.       Height          =   315
  60.       Left            =   720
  61.       TabIndex        =   0
  62.       Text            =   "C:\TEST.TXT"
  63.       Top             =   300
  64.       Width           =   3555
  65.    End
  66.    Begin VB.Data Data1 
  67.       Caption         =   "Data1"
  68.       Connect         =   "Access"
  69.       DatabaseName    =   "C:\BLOBTEST.MDB"
  70.       Exclusive       =   0   'False
  71.       Height          =   270
  72.       Left            =   300
  73.       Options         =   0
  74.       ReadOnly        =   0   'False
  75.       RecordsetType   =   1  'Dynaset
  76.       RecordSource    =   "Table1"
  77.       Top             =   1680
  78.       Width           =   5715
  79.    End
  80.    Begin VB.Label Label3 
  81.       Caption         =   "File:"
  82.       Height          =   315
  83.       Left            =   300
  84.       TabIndex        =   8
  85.       Top             =   300
  86.       Width           =   375
  87.    End
  88.    Begin VB.Label Label2 
  89.       Caption         =   "A helpful code snippet for VB4 programmers --- Frank Font 1996"
  90.       Height          =   255
  91.       Left            =   240
  92.       TabIndex        =   7
  93.       Top             =   3360
  94.       Width           =   4695
  95.    End
  96.    Begin VB.Label Label1 
  97.       Caption         =   $"OLETEST.frx":0000
  98.       Height          =   1035
  99.       Left            =   240
  100.       TabIndex        =   6
  101.       Top             =   2160
  102.       Width           =   5775
  103.    End
  104. Attribute VB_Name = "Form1"
  105. Attribute VB_Creatable = False
  106. Attribute VB_Exposed = False
  107. Option Explicit
  108. '----------------------------------------------------------------------
  109. 'Pass in the name of the file that you want to store in the specified
  110. 'Access 2.0 OLE OBJECT field.  This function will return the size of
  111. 'the file.
  112. '----------------------------------------------------------------------
  113. Public Function CopyOleFromFile(FileName As String, OleField As Field) As Long
  114.   Const BUFFER_SIZE = 4096
  115.   Dim Handle As Integer
  116.   Dim Buffer As String * 4096
  117.   Dim BytesNeeded As Long
  118.   Dim Buffers As Long
  119.   Dim Remainder As Integer
  120.   Dim i As Long
  121.   BytesNeeded = FileLen(FileName)
  122.   Buffers = BytesNeeded \ BUFFER_SIZE
  123.   Remainder = BytesNeeded Mod BUFFER_SIZE
  124.   'Copy the object chunk by chunk.
  125.   Handle = FreeFile
  126.   Open FileName For Binary Access Read As #Handle
  127.   If BytesNeeded > BUFFER_SIZE Then
  128.     For i = 0 To Buffers - 1
  129.       Get #Handle, , Buffer
  130.       OleField.AppendChunk Buffer
  131.     Next
  132.   End If
  133.   Get #Handle, , Buffer
  134.   OleField.AppendChunk LeftB(Buffer, Remainder)
  135.   Close #Handle
  136.   CopyOleFromFile = BytesNeeded
  137. End Function
  138. '----------------------------------------------------------------------
  139. 'Pass in the name of the file you want to create and the Access 2.0
  140. 'OLE OBJECT field that contains it.  This function will return the size
  141. 'of the file.
  142. '----------------------------------------------------------------------
  143. Public Function CopyOleToFile(FileName As String, OleField As Field) As Long
  144.   Const BUFFER_SIZE = 8192
  145.   Dim Handle As Integer
  146.   Dim Buffer As String
  147.   Dim BytesNeeded As Long
  148.   Dim Buffers As Long
  149.   Dim Remainder As Long
  150.   Dim i As Long
  151.   BytesNeeded = OleField.FieldSize()
  152.   Buffers = BytesNeeded \ BUFFER_SIZE
  153.   Remainder = BytesNeeded Mod BUFFER_SIZE
  154.   'Copy the object chunk by chunk.
  155.   Handle = FreeFile
  156.   Open FileName For Binary As #Handle
  157.   For i = 0 To Buffers - 1
  158.     Buffer = OleField.GetChunk(i * BUFFER_SIZE, BUFFER_SIZE)
  159.     Put #Handle, , Buffer
  160.   Next
  161.   Buffer = OleField.GetChunk(Buffers * BUFFER_SIZE, Remainder)
  162.   Put #Handle, , Buffer
  163.   Close #Handle
  164.   CopyOleToFile = BytesNeeded
  165. End Function
  166. Private Sub cmdGet_Click()
  167. MsgBox CopyOleFromFile(txtName, Data1.Recordset("Blob")), , "Done"
  168. End Sub
  169. Private Sub cmdPut_Click()
  170. MsgBox CopyOleToFile(txtName, Data1.Recordset![Blob]), , "Done"
  171. End Sub
  172. Private Sub Command1_Click()
  173. Data1.Recordset.AddNew
  174. End Sub
  175. Private Sub Command2_Click()
  176. Data1.Recordset.Update
  177. End Sub
  178. Private Sub Command3_Click()
  179. Data1.Recordset.Edit
  180. End Sub
  181.