If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Pervasive.SQL > VB-Office crash on Get/Step operations

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-17-06, 12:45
miqmago miqmago is offline
Registered User
 
Join Date: Aug 2006
Posts: 9
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 14:00.
Reply With Quote
  #2 (permalink)  
Old 08-17-06, 14:48
mirtheil mirtheil is offline
Registered User
 
Join Date: Dec 2001
Posts: 1,026
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.
Reply With Quote
  #3 (permalink)  
Old 08-18-06, 06:13
miqmago miqmago is offline
Registered User
 
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On