VB4.0/SQL server - GetChunk/AppendChunk behave oddly 
Author Message
 VB4.0/SQL server - GetChunk/AppendChunk behave oddly

Question 1:  The documentation for GetChunk says, "If numbytes is
greater than the number of bytes in the field, the actual number of
bytes in the field is returned."  However, the code below seems to
show otherwise.  If CHUNK_SIZE, which is used as the numbytes
parameter to GetChunk is modified, lower numbers seem to work.  Does
anyone have any idea why?

Question 2: AppendChunk is failing with a block of binary data that
contains an ASCII null.  The method will only append those bytes up
until the NULL.  

Are these behaviors by design, and if so, are there a workarounds?

Here is the code:

VERSION 4.00
Begin VB.Form memotest
   Caption         =   "memotest"
   ClientHeight    =   2070
   ClientLeft      =   2130
   ClientTop       =   1935
   ClientWidth     =   5520
   Height          =   2475
   Left            =   2070
   LinkTopic       =   "Form1"
   ScaleHeight     =   2070
   ScaleWidth      =   5520
   Top             =   1590
   Width           =   5640
   Begin VB.CommandButton BtnQuit
      Caption         =   "Quit"
      Height          =   615
      Left            =   3000
      TabIndex        =   3
      Top             =   1080
      Width           =   2295
   End
   Begin VB.CommandButton BtnJet30
      Caption         =   "DAO 3.0 JET"
      Height          =   615
      Left            =   480
      TabIndex        =   2
      Top             =   1080
      Width           =   2295
   End
   Begin VB.CommandButton BtnJet31
      Caption         =   "DAO 3.1 JET"
      Height          =   615
      Left            =   3000
      TabIndex        =   1
      Top             =   300
      Width           =   2295
   End
   Begin VB.CommandButton BtnODBCDirect
      Caption         =   "DAO 3.1 ODBCDirect"
      Height          =   615
      Left            =   480
      TabIndex        =   0
      Top             =   300
      Width           =   2295
   End
End
Attribute VB_Name = "memotest"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
Option Explicit

Const sFileName As String = "c:\windows\bubbles.bmp"
'Const sFileName As String = "c:\autoexec.bat"

Const CHUNK_SIZE = 4096
Const sTmpFile As String = "c:\windows\temp\bubtemp.bmp"
Const sDB As String = "c:\windows\temp\test.mdb"

#If DAO_3_1 Then
    Dim Conn As Connection
    Dim WS As Workspace
#End If

Dim DB As Database

Function MemoToFile(TheField As Field, lSize As Long) As Long
    Dim iHandle As Integer
    Dim sBlock As String
    Dim lLoop As Long

    iHandle = FreeFile

    Open sTmpFile For Binary Access Write As #iHandle

    lLoop = 0
    Do
        sBlock = Format$(TheField.GetChunk(lLoop * CHUNK_SIZE,
CHUNK_SIZE))
        'MsgBox Format$(Len(sBlock))
        If sBlock <> "" Then
            Put iHandle, , sBlock
        Else
            Exit Do
        End If
        lLoop = lLoop + 1
    Loop

    lSize = LOF(iHandle)
    Close #iHandle

End Function
Function FileToMemo(TheField As Field, lSize As Long) As Long
    Dim iHandle As Integer
    Dim lFileSize As Long
    Dim lNumBlock As Long           ' # of blocks to write
    Dim lLastBlock As Long          ' uneven part left over after
other blocks
    Dim lLoop As Long
    Dim sBlock As String

    iHandle = FreeFile

    Open sFileName For Binary Access Read As #iHandle
    lFileSize = LOF(iHandle)
    lSize = lFileSize

    lNumBlock = lFileSize \ CHUNK_SIZE
    lLastBlock = lFileSize Mod CHUNK_SIZE

    sBlock = Space$(CHUNK_SIZE)

    For lLoop = 1 To lNumBlock
        Get #iHandle, , sBlock
        TheField.AppendChunk (sBlock)
    Next

    If lLastBlock > 0 Then
        sBlock = Space$(lLastBlock)
        Get #iHandle, , sBlock
        TheField.AppendChunk (sBlock)
    End If

    Close #iHandle

End Function

Function TestTheField(sType As String) As Long
    Dim lFileSize As Long
    Dim lResult As Long
    Dim sBuf As String
    Dim TheCursor As Recordset

    #If DAO_3_1 Then
        If sType = "ODBC" Then
            Conn.Execute ("DELETE FROM pub_info WHERE pub_id =
'9900'")
            Set TheCursor = Conn.OpenRecordset("SELECT * FROM
pub_info", dbOpenDynaset, 0, dbOptimistic)
        Else
            DB.Execute ("DELETE FROM pub_info WHERE pub_id = '9900'")
            Set TheCursor = DB.OpenRecordset("SELECT * FROM pub_info",
dbOpenDynaset, 0, dbOptimistic)
        End If
    #Else
    #End If
    TheCursor.AddNew
    'This is to avoid any problems with foreign constraints on the
table
    'Make sure you insert this into the table
    'FROM the i/SQL windows type INSERT INTO PUBLISHERS (pub_id)
VALUES ('9900')
    TheCursor("Pub_id") = "9900"
    lResult = FileToMemo(TheCursor("pr_info"), lFileSize)
    TheCursor.Update
    TheCursor.Close
    sBuf = "record added" & vbCrLf & "FILE: " & sFileName
    sBuf = sBuf & vbCrLf & "File Size: " & Format$(lFileSize)
    MsgBox sBuf

    #If DAO_3_1 Then
        If sType = "ODBC" Then
            Set TheCursor = Conn.OpenRecordset("SELECT * FROM pub_info
WHERE pub_id = '9900'", dbOpenDynaset, 0, dbReadOnly)
        Else
            Set TheCursor = DB.OpenRecordset("SELECT * FROM pub_info
WHERE pub_id = '9900'", dbOpenDynaset, 0, dbReadOnly)
        End If
    #Else
    #End If
    lResult = MemoToFile(TheCursor("pr_info"), lFileSize)
    TheCursor.Close
    sBuf = "file extracted" & vbCrLf & "FILE: " & sTmpFile
    sBuf = sBuf & vbCrLf & "File Size: " & Format$(lFileSize)
    MsgBox sBuf

End Function

Private Sub BtnJet31_Click()
    Dim lResult As Long

    #If DAO_3_1 Then

        DBEngine.DefaultType = dbUseJet

          'Set this value to point to your system.mdw file
          DBEngine.SystemDB = "h:\meteor\bin\System.MDW"

        'Fill in the username and password sections as per your setup
          Set WS = DBEngine.CreateWorkspace("temp", (your user name), (your
password))

        Set DB = Workspaces(0).OpenDatabase(sDB)
        lResult = TestTheField("JET31")
        Set Conn = Nothing
        Set WS = Nothing

    #End If

End Sub

Private Sub BtnODBCDirect_Click()
    Dim lResult As Long

    #If DAO_3_1 Then

        DBEngine.DefaultType = dbUseODBC

        'Fill in the username and password sections as per your setup
        Set WS = DBEngine.CreateWorkspace("temp", (your user name),
(your password))

        Set Conn = Workspaces(0).OpenConnection("", dbDriverPrompt,
False, "ODBC;")

        lResult = TestTheField("ODBC")
        Set Conn = Nothing
        Set WS = Nothing

    #End If

End Sub

Private Sub BtnQuit_Click()
    Unload Me
End Sub

Private Sub Form_Load()
    #If DAO_3_1 Then
        BtnODBCDirect.Enabled = True
        BtnJet31.Enabled = True
        BtnJet30.Enabled = False
    #Else
        BtnODBCDirect.Enabled = False
        BtnJet31.Enabled = False
        BtnJet30.Enabled = True
    #End If

End Sub



Fri, 04 Dec 1998 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. VB4.0/SQL server - GetChunk/AppendChunk behave oddly

2. GetChunk and AppendChunk - works with ODBC/SQL Server?

3. Converted Database Behaves Oddly

4. Word 97/2000 Selection.InsertFile behaves oddly

5. Borderstyle Fixed Dialog - behaves oddly with msgboxes

6. PeekMessage() behaves oddly in DLL under VB 3.0

7. Desktop behaves oddly when Shelling EXPLORER or WINFILE

8. Desktop behaves oddly when Shelling EXPLORER or WINFILE

9. Weird problem with GetChunk/AppendChunk and BLOBs

10. DAO getchunk/appendchunk problems in Access 97 DB

11. DCOM AppendChunk getChunk

12. Using GetChunk and AppendChunk methods.

 

 
Powered by phpBB® Forum Software