Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2006
    Posts
    9

    Unanswered: VB-Office crash on Get/Step operations

    Hi!

    I don't understand what is going on:
    I'm writting a macro in Excel to read data from a .btr file, so I created a class called clssFicheroBtr where I have written the necessary code to operate with the .btr file. I'm working under PSQL7.

    What it happens is that once the file is Opened correctly (with 0 BTRCALL operation), when any Get/Step (12, 33, ...) operation is called by BTRCALL, inmediatelly the Excel crashes without any sweet message like "Hope you have enjoyed the dedicated time..." or something like that. I have tried with Access too, but it happens exactly the same. I know it works, because I get success with other test .btr files, and I'm sure I'm using the correct record size, and almost sure I'm using the correct parametrers, but I don't know what is going on.
    Does anybody know what happens? Thanks a lot.

    The VB-code I'm using is this one:

    PHP Code:
    DefInt A-Z

    Const DEFKeyBufLen 255

    Private Declare Function BTRCALL Lib "wbtrv32.dll" (ByVal OPByVal Pb$, Db As AnyDL As IntegerKb As AnyByVal KlByVal Kn) As Integer

    Public NombreFichero As String
    Public RutaFichero As String
    Public FicheroAbierto As Boolean
    Public ClavePpal As Integer
    Public Marcadores As clssMarcador
     
    Dim iclNregistros 
    As Long
    Dim iclNumCampos 
    As Integer
    Dim iclCampos
    () As clssCampos

    Dim KeyBufLen 
    As Integer
    Dim PosBlk
    $
    Dim DataBuf As String
    Dim DBLen 
    As Integer
    Dim KeyNum 
    As Integer

    Property Get LongReg
    () As Integer
        Dim I 
    As Integer
        
        LongReg 
    0
        
        
    For 0 To NumCampos 1
            LongReg 
    LongReg iclCampos(I).LongitudCampo
        Next
    End Property

    Property Get NumCampos
    () As Integer
        NumCampos 
    iclNumCampos
    End Property

    Property Let NumCampos
    (NC As Integer)
        
    Dim i As Integer
        
        
    If iclNumCampos <> 0 Then
            MsgBox 
    "Error en la propiedad NumCampos de la clase clssFicheroBtr:" Chr(10) + _
                
    "No se puede cambiar el número de campos una vez ya asignados"vbExclamation"Error"
        
    Else
            
    ReDim iclCampos(NC 1)
            For 
    0 To NC 1
                Set iclCampos
    (i) = New clssCampos
            Next
            iclNumCampos 
    NC
        End 
    If
    End Property

    Public Function AbreFichero() As Boolean
        Dim I 
    As Integer
        Dim Stat 
    As Integer
        
        AbreFichero 
    True
        
        
    If Not FicheroAbierto Then
            DataBuf 
    Space(LongReg)
            
    DBLen Len(DataBuf)
            
    KeyBufLen DEFKeyBufLen
            KeyBuffer
    $ = RutaFichero "\\" NombreFichero
            KeyNum 
    BtrOpenMode.Read_Only
            
            Stat 
    BTRCALL(Btr.AbrirPosBlk$, DataBufDBLenByVal KeyBuffer$, KeyBufLenKeyNum)
        
    End If

    Fin:
        If 
    Stat <> 0 Then
            Beep
            ErrBtr Stat
    "Error en el método AbreFichero de la clase FicheroBtr.", , vbCritical"Error"
            
    AbreFichero False
            FicheroAbierto 
    False
        
    Else
            
    FicheroAbierto True
            StepFirst
        End 
    If
    End Function

    Public 
    Sub StepFirst()
        
    Dim Stat As Integer
        
        DataBuf 
    Space(LongReg)
        
    DBLen LongReg
        KeyBufLen 
    DEFKeyBufLen
        KeyBuffer
    $ = Space(KeyBufLen)
        
    KeyNum 0
        Stat 
    BTRCALL(Btr.StepFirstPosBlk$, DataBufDBLenKeyBuffer$, KeyBufLenKeyNum)
        
        If 
    Stat <> 0 Then
            ErrBtr Stat
    "Error en el método StepFirst de la clase FicheroBtr.", , vbExclamation"Error"
        
    Else: Buf_Mem
        End 
    If
    End Sub

    Public Sub MoveFirst()
        
    Dim Stat As Integer
        
        DataBuf 
    Space(LongReg)
        
    DBLen LongReg
        KeyBufLen 
    DEFKeyBufLen
        KeyBuffer
    $ = Space(KeyBufLen)
        
    KeyNum ClavePpal
        Stat 
    BTRCALL(Btr.GetFirstPosBlk$, DataBufDBLenByVal KeyBuffer$, KeyBufLenKeyNum)
        
        If 
    Stat <> 0 Then
            ErrBtr Stat
    "Error en el método MoveFirst de la clase FicheroBtr.", , vbExclamation"Error"
        
    Else: Buf_Mem
        End 
    If
    End Sub


    Private Sub Class_Initialize()
        
    iclNumCampos 0
        FicheroAbierto 
    False
        KeyBufLen 
    DEFKeyBufLen 'Por defecto la longitud de clave está a 255
        ClavePpal = 0 '
    Por defecto se utiliza el índice 0
        PosBlk
    $ = Space$(128)
    End Sub 
    Last edited by miqmago; 08-17-06 at 15:00.

  2. #2
    Join Date
    Dec 2001
    Posts
    1,109
    Provided Answers: 4
    I've never used the Btrieve API from Excel (or Access or Word) but have done it through VB. A few suggestions:
    1. Before calling the BTRCALL function that crashes, display a message box with all of the parameters (except the PosBlk). Make sure they are correct.
    2. Hard code values for the parameters (rather than rely on functions or properties). Display the values before calling BTRCALL.
    If hard coding the values prevents the crash, the problem is your values.

    Usually a crash when calling the Btrieve API is caused by an uninitialized or incorrectly sized variable.
    Mirtheil Software
    Certified Pervasive Developer
    Certified Pervasive Technician
    Custom Btrieve/VB development
    http://www.mirtheil.com
    I do not answer questions by email. Please post on the forum.

  3. #3
    Join Date
    Aug 2006
    Posts
    9
    I have realised that it is because the data buffer in BTRCALL was a string, and when I declare the data buffer as a user defined type with the structure of the record it works fine.

    PHP Code:
    Type RecordStructure
        Field1 
    as DataInField1
        Field2 
    as DataInField2
        
    ...
    End Type

    Dim DataBuf 
    as RecordStructure
    ...
    Sub ...
    ... 
    Stat BTRCALL(BGETFIRSTPosBlk$, DataBufDBLenKB$, KBLenKNum
    Is there any special reason why BTRCALL crashes when a string is used as data buffer?

    As I would like to read different tables with different record structure, but all fields with char data, is there any way to avoid this crash?
    I mean, my idea was to read all the record in the data buffer and then parse it to the field structure of the record, considering the predefined length of each field in the different tables.

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •