Wednesday, November 29, 2006

connecting btrieve database with vb6

you have an old version of btrieve and you want to read data from it?? well, you'r luck if you read this message in the beginning of your search... i lost 3 days of intensive searching.. and finaly found it. and here i share it with you...
well, open vb program and bla bla bla ...
now you should define some consts for easy work:
i suggest you to create a separate modul for this code (that what i did)
DefInt A-Z
Global Const BOPEN = 0
Global Const BCLOSE = 1
Global Const BINSERT = 2
Global Const BUPDATE = 3
Global Const BDELETE = 4
Global Const BGETEQUAL = 5
Global Const BGETNEXT = 6
Global Const BGETGREATEROREQUAL = 9
Global Const BGETFIRST = 12
Global Const BCREATE = 14
Global Const BSTAT = 15
Global Const BSTOP = 25
Global Const BVERSION = 26
Global Const BRESET = 28
Global Const KEY_BUF_LEN = 255

Rem Key FlagsGlobal Const DUP = 1
Global Const MODIFIABLE = 2
Global Const BIN = 4
Global Const NUL = 8
Global Const SEGMENT = 16
Global Const SEQ = 32
Global Const DEC = 64
Global Const SUP = 128
Rem Key Types
Global Const EXTTYPE = 256
Global Const MANUAL = 512
Global Const BSTRING = 0
Global Const BINTEGER = 1
Global Const BFLOAT = 2
Global Const BDATE = 3
Global Const BTIME = 4
Global Const BDECIMAL = 5
Global Const BNUMERIC = 8
Global Const BZSTRING = 11
Global Const BAUTOINC = 15

' now declare the function BTRCALL with do all the I\O with the database
Private Declare Function BTRCALL Lib "wbtrv32.dll" (ByVal OP, ByVal Pb$, Db As Any, DL As Long, Kb As Any, ByVal Kl, ByVal Kn) As Integer

' define some data types for working with the database files
Type typ_byte4 f1(1 To 4) As ByteEnd TypeRem ***************************************************************************
Rem Btrieve Structures
Type KeySpec
KeyPos As Integer
KeyLen As Integer
KeyFlags As Integer
KeyTot As typ_byte4
KeyType As String * 1
Reserved As String * 5
End Type

Type FileSpec
RecLen As Integer
PageSize As Integer
IndxCnt As Integer
NotUsed As String * 4
FileFlags As Integer
Reserved As String * 2
Allocation As Integer
KeyBuf(0 To 1) As KeySpec
End Type

Type StatFileSpecs
RecLen As Integer
PageSize As Integer
IndexTot As Integer
RecTot As typ_byte4
FileFlags As Integer
Reserved As String * 2
UnusedPages As Integer
KeyBuf(0 To 1) As KeySpec
End Type

Type RecordBuffer
' here you should define the structure of the file that you are reading.. in this example i have 3 'fields, one is an id (int) description and another float type
'change this to your data structure
profID As Integer
profDesc As String * 28
profPrice As Double
End Type

Type VersionBuf
Major As Integer
Minor As Integer
Engine As String * 1
End Type

Type typ_PosBlk
f1(1 To 128) As Byte
End Type

' defining variables with the types we created
Global FileBuf As FileSpec
Global DataBuf As RecordBuffer
Global StatFileBuffer As StatFileSpecs
Global BufLen As Long
Global DBLen As Integer

Rem*******Added to Open multiple files
Public iMaxRuns As Integer
Public bFilesCreated As Boolean

Sub PrintLB(Item As String)
' function for printing data on screen
Dim fTxtFileName As String
BtrFrm32.List1.AddItem Item
Rem Added to write info to file
fTxtFileName = App.Path & "\" & App.EXEName & ".log"
Open fTxtFileName For Append As #1
Print #1, Item Close #1
Rem *************
End Sub

sub readDate()
PrintLB ("Btrieve Sample Test Started")
PrintLB ("")
Rem Local variables needed for conversion from byte to long.
Dim loc_RecTot As Long
Dim h_field1 As String
Dim h_field2 As String
Dim h_field3 As String
Dim h_field4 As String
Dim h_total As String
'Rem **************************
FileName$ = "XFACE.BTR"
PosBlk$ = Space$(128)KeyBuffer$ = Space$(KEY_BUF_LEN)
RemRem ***************** Btrieve Create *********************Rem
Rem ************* SET UP FILE SPECS
FileBuf.RecLen = 34
FileBuf.PageSize = 1024
FileBuf.IndxCnt = 2
FileBuf.FileFlags = 0
''Rem ************* SET UP KEY SPECS
FileBuf.KeyBuf(0).KeyPos = 1
FileBuf.KeyBuf(0).KeyLen = 8
FileBuf.KeyBuf(0).KeyFlags = EXTTYPE + MODIFIABLE
FileBuf.KeyBuf(0).KeyType = Chr$(BFLOAT)
FileBuf.KeyBuf(1).KeyPos = 9
FileBuf.KeyBuf(1).KeyLen = 26
FileBuf.KeyBuf(1).KeyFlags = EXTTYPE + MODIFIABLE + DUP
FileBuf.KeyBuf(1).KeyType = Chr$(BSTRING)

'Open File
KeyBufLen = KEY_BUF_LEN
KeyBuffer$ = txtFileName.txt
BufLen = Len(DataBuf)KeyNum = 0
Status = BTRCALL(BOPEN, PosBlk$, DataBuf, BufLen, ByVal KeyBuffer$, KeyBufLen, KeyNum)
If Status <> 0 Then
Msg$ = "Error Opening file! " + Str$(Status)
PrintLB (Msg$)
GoTo Fini
Else
Msg$ = "File Opened Successfully!" PrintLB (Msg$)
End If

BufLen = Len(DataBuf)
KeyBuffer$ = Space$(255)
KeyBufLen = KEY_BUF_LEN
'Status = BTRCALL(BGETFIRST, PosBlk$, DataBuf, BufLen, ByVal KeyBuffer$, KeyBufLen, 0)
If Status <> 0 Then
Msg$ = "Error on BGETFIRST. " + Str$(Status)
PrintLB (Msg$)
Else
Msg$ = "BGETFIRST okay for : " + DataBuf.profDesc PrintLB (Msg$)
End If
'Get Next Record
BufLen = Len(DataBuf)KeyBuffer$ = Space$(KEY_BUF_LEN)
KeyBufLen = KEY_BUF_LEN
Status = BTRCALL(BGETNEXT, PosBlk$, DataBuf, BufLen, ByVal KeyBuffer$, KeyBufLen, 0)
If Status <> 0 Then
Msg$ = "Error on BGETNEXT. " + Str$(Status)
PrintLB (Msg$)
Else
Msg$ = "BGETFIRST okay for : " + DataBuf.profDesc
PrintLB (Msg$)
End If


end sub

No comments: