Documentation component by D.Glodt (c)2004

Appendix A: QDataBase


QDataBase Component

QdataBase is a component to create and manipulate data base files.

The data base manipulated by this component are not standard with other data base.

the limit of fields number and records is limited at the maxi value of 24 bits word.


QDataBase Properties

Field

Type

R/W

Default






FileName

STRING

R

File name of data base with path.

RecordSize

LONG

R

Record size in octet from data base.

RecordCount

LONG

R

Record count from data base.

FieldCount

LONG

R

Field count from a record.

Error

INTEGER

L

Error data base , his number must be compare with the following constantes to identify error en if the methods return false.

ErrDbCreate: Pb of file creation.

ErrDbOpen: Pb open file

ErrDbFile: File not exist

ErrDbIndex: Index record out of bound

ErrDbRecordEmpty: Record empty or record format empty.

ErrDbRecordSize: Size record is not the same that data base format.

QDataBase Methods

Method

Type

Description

Params






GetFieldName

Function(index&) as string

return the field name by his index

1

GetFieldIndex

Function(name$) as long

return the index field by his name

1

GetFieldSize

Function(index&) as long

return the field size in octet by his index.

1

IsOpen

Function as boolean

return the open state of a data base.

0

Create

Function(FileName$,RecordFormat$) as boolean

Create a dat base file and return true if the file is create.

FileName is the fiel name to create, RecordFormat is the record format from records of data base including the name and the size of each field.

2



Format: <FieldName:FieldSize>,<FieldName:FieldSize >,...........


Example:

text= »NAME:10,CITY:20 »


Open

Function(FileName$) as boolean

Open a data base file and return True.

FileName is the file name to open.

1

Close

SUB

Close data base.

0

AddRecord

Function(record$) as boolean

Add a record to the data base and return True.

Record is all fields of record with the same size that of data base format.

1

AddRecordUdt

Function(record&) as boolean

Add a record to the data base by udt and return True.

Record is a UDT type of variant size but always the same size that data base format with string fields only.

1



Example:

Type Customer

name as string*10

city as string*20

End Type


Dim myCustomer as Customer

myCustomer.name= »Peter »

myCustomer.city= »Paris »


dbase.AddRecordUdt( myCustomer)


SaveRecord

Function(index&,record$) as boolean

Save a record by his index in data base and return True.

Record is all fields of record with the same size that the data base format.

2

SaveRecordUdt

Function(index&,record&) as boolean

Save a record of UDT type by his index in data base and return True.

Record is a UDT type of variant size but always the same size that data base format with string fields only.

2

GetRecord

Function(index&,byref record$) as boolean

Put in record parameter the record l'enregistrement identify by his index.

2

GetRecordUdt

Function(index&,record&) as boolean

Put in the record parameter of UDT type the record identify by his index.

The variable UDT must be the same size of data base format.

2

DelRecord

Function(index&) as boolean

Remove a record from data base by his index

1

SearchByRecord

Function(name$) as boolean

Return True if the string of parameter name has be found in all record of the data base..

The records index of record including the string will be returned by event OnSearch.

1

SearchByField

Function(name$,fieldIndex&) as boolean

Return True if the string of parameter name has be found in the field identify by his index of all the records of data base.

The records index including the string in their fields will be returned by event OnSearch.

2

SearchByRequest

Function(operator$,value$,bNum&,fieldIndex&) as boolean

Returne True if there is records fitted in with a request.

The request is made by a operator and a value on a field identify by his index.

4



Operator list is the following:

< , > ,<= , >= , = , <>

Value parameter is the string to check or numeric value if bNum parameter is at True.

The records index fitted in with the request will be returned by event OnSearch.

Example:

dbase.SearchByRequest( « > », »2000 »,1)



QDataBase Evenements

Event

Type

Occurs when...

Params






OnSearch

(index&)

On using Search methods

1


QDataBase Examples
 
$typecheck on

$include "rapidq.inc"
$include "Object\QDataBase.inc"

declare sub show
Declare Sub mnFileClick
Declare Sub mnOpenClick
Declare Sub mnSaveClick
Declare Sub mnCloseClick
Declare Sub mnCreateClick
Declare Sub mnRequestClick
Declare Sub mnExitClick
Declare Sub BtOkClick
Declare Sub ShowError
Declare Sub ClearCell
Declare Sub Search(index as long)

Type TClient
  Name as string*15
  Compagny as string*20
  City as string*20
  Postal as string*10
  Country as string*20
End Type

dim dbase as QDataBase
    dbase.OnSearch=Search
const maxSeq=50
dim indexsearch as integer
dim client as TClient
dim OpenDial as QOpenDialog
    OpenDial.Filter="Db file(*.db)|*.db"
    OpenDial.caption="Open database"
dim CreateDial as QSaveDialog
    CreateDial.Filter="Db file(*.db)|*.db"
    CreateDial.caption="Create database"


CREATE Form2 AS QFORM
  BorderStyle=bsDialog
  Caption="Request"
  Width=316
  Height=153
  Center
  CREATE Label1 AS QLABEL
    Caption="Value:"
    Left=26
    Top=16
  END CREATE
  CREATE Label2 AS QLABEL
    Caption="Operator:"
    Left=10
    Top=48
    Width=46
  END CREATE
  CREATE Label3 AS QLABEL
    Caption="Field:"
    Left=115
    Top=48
    Width=46
  END CREATE
  CREATE EdValue AS QEDIT
    Left=63
    Top=12
    Width=233
  END CREATE
  CREATE CbOp AS QCOMBOBOX
    Left=64
    Top=45
    Width=40
    AddItems "=","<",">","<=",">=","<>"
    ItemIndex=0
  END CREATE
  CREATE CbField AS QCOMBOBOX
    Left=145
    Top=45
    Width=80
    AddItems "Name","Compagny","City","Postal","Country"
    ItemIndex=0
  END CREATE
  CREATE CheckNum AS QCHECKBOX
    Caption="Numeric"
    Left=240
    Top=48
    Width=81
  END CREATE
  CREATE BtOk AS QBUTTON
    Caption="&Ok"
    Kind=bkOK  
    Left=136
    Top=90
    Default=true
    OnClick=BtOkClick
  END CREATE
  CREATE BtCancel AS QBUTTON
    Caption="&Cancel"
    Kind=bkCancel  
    Left=224
    Top=90
  END CREATE
END CREATE
    
CREATE Form AS QFORM
  Caption="DataBase"
  Width=730
  Height=454
  Center
  OnShow=Show
  Create MainMenu As QMAINMENU
    Create File As QMenuItem
      Caption="&File"
      OnClick=mnFileClick
      Create mnOpen As QMenuItem
        Caption="&Open"
        OnClick=mnOpenClick
      End Create
      Create mnSave As QMenuItem
        Caption="&Save"
        OnClick=mnSaveClick
      End Create
      Create mnClose As QMenuItem
        Caption="&Close"
        OnClick=mnCloseClick
      End Create
      Create mnCreate As QMenuItem
        Caption="C&reate"
        OnClick=mnCreateClick
      End Create
      Create sep1 As QMenuItem
        Caption="-"
      End Create
      Create mnRequest As QMenuItem
        Caption="&Request"
        OnClick=mnRequestClick
      End Create
      Create sep2 As QMenuItem
        Caption="-"
      End Create
      Create mnExit As QMenuItem
        Caption="&Exit"
        OnClick=mnExitClick
      End Create
    End Create
  End Create
  CREATE StringGrid1 AS QSTRINGGRID
    Align=AlClient
    RowCount=maxSeq    
    ColCount=6
    Color=&HAAFFFF
    DefaultColWidth=100
    DefaultRowHeight=20
    ColWidths(0)=50
    Cell(0,0)="Record"
    Cell(1,0)=space$(8)+"Name"
    Cell(2,0)=space$(8)+"Company"
    Cell(3,0)=space$(12)+"City"
    Cell(4,0)=space$(8)+"Postal code"
    Cell(5,0)=space$(8)+"Country"
    AddOptions goEditing,goColSizing
  END CREATE
END CREATE

Form.ShowModal

'============================================ 
' Evenement apparition fenetre 
'============================================ 
Sub Show
  dim i as integer
  for i=1 to maxSeq
    StringGrid1.Cell(0,i)=str$(i)
  next i
End Sub

'============================================ 
' Evenement menu ouvrir 
'============================================ 
Sub mnOpenClick
  dim i as integer
  dim filename as string
  dim Error as integer

  if OpenDial.Execute() then
    if dbase.open(OpenDial.filename) then
      for i=1 to dbase.RecordCount
        if dbase.GetRecordUdt(i,client)=true then
          StringGrid1.Cell(1,i)=client.name
          StringGrid1.Cell(2,i)=client.Compagny
          StringGrid1.Cell(3,i)=client.City
          StringGrid1.Cell(4,i)=client.Postal
          StringGrid1.Cell(5,i)=client.Country
        else
          Error=True
          Exit For
        end if
      next i
      if Error=true then ShowError 
    else
      ShowError
    end if
  end if
End Sub

'============================================ 
' Effacement contenu cellule 
'============================================ 
Sub ClearCell
  dim i as integer
  dim j as integer
  
  for i=1 to StringGrid1.RowCount-1
    for j=1 to StringGrid1.ColCount-1
      StringGrid1.Cell(j,i)=""
    next j
  next i
End Sub

'============================================ 
' Evenement menu fermer 
'============================================ 
Sub mnCloseClick
  ClearCell
  dbase.close
End Sub

'============================================ 
' Evenement menu créer 
'============================================ 
Sub mnCreateClick
  dim format as string
  dim i as integer
  dim filename as string
  dim Error as integer

  format="NAME:15,COMPAGNY:20,CITY:20,POSTAL:10,COUNTRY:20"
  if CreateDial.Execute() then
    filename=CreateDial.filename
    if instr(LCASE$(FileName),".db")=0 then FileName=FileName+".db"
    if dbase.create(CreateDial.filename,format)=true then
      for i=1 to StringGrid1.RowCount-1
        client.name=StringGrid1.Cell(1,i)
        client.Compagny=StringGrid1.Cell(2,i)
        client.City=StringGrid1.Cell(3,i)
        client.Postal=StringGrid1.Cell(4,i)
        client.Country=StringGrid1.Cell(5,i)
        if dbase.AddRecordUdt(client)=False then
          Error=True
          Exit For
        end if
      next i
      if Error=True then ShowError 
    else
      ShowError
    end if
  end if
End Sub

'============================================ 
' Evenement menu sauver 
'============================================ 
Sub mnSaveClick
  dim i as integer
  dim filename as string
  dim Error as integer

  if CreateDial.Execute() then
    filename=CreateDial.filename
    if instr(LCASE$(FileName),".db")=0 then FileName=FileName+".db"
    if dbase.IsOpen then
      for i=1 to StringGrid1.RowCount-1
        client.name=StringGrid1.Cell(1,i)
        client.Compagny=StringGrid1.Cell(2,i)
        client.City=StringGrid1.Cell(3,i)
        client.Postal=StringGrid1.Cell(4,i)
        client.Country=StringGrid1.Cell(5,i)
        if dbase.SaveRecordUdt(i,client)=False then
          Error=True
          Exit For
        end if
      next i
      if Error=True then ShowError 
    end if
  end if
End Sub

'============================================ 
' Affichage erreur 
'============================================ 
Sub ShowError
  defstr text=""

  Select case dbase.error
  case ErrDbCreate
    text="Error file create"
  case ErrDbOpen
    text="Error file open"
  case ErrDbFile
    text="Error file not exist"
  case ErrDbIndex
    text="Error index out of bound"
  case ErrDbRecordEmpty
    text="Error record empty"
  case ErrDbRecordSize
    text="Error record size"
  End Select
  MessageBox(text,"Error",16)
End Sub

'============================================ 
' Evenement menu fichier 
'============================================ 
Sub mnFileClick
  if dbase.IsOpen then
    mnSave.enabled=true
    mnRequest.enabled=true
  else
    mnSave.enabled=false
    mnRequest.enabled=false
  end if
End Sub

'============================================ 
' Evenement menu request 
'============================================ 
Sub mnRequestClick
  form2.showmodal
End Sub

'============================================ 
' Evenement bouton ok 
'============================================ 
Sub BtOkClick
  dim operator as string
  dim index as long
  
  if EdValue.text="" then exit sub
  ClearCell
  indexsearch=0
  operator=CbOp.Item(CbOp.ItemIndex)
  index=dbase.GetFieldIndex(CbField.Item(CbField.ItemIndex))
  dbase.SearchByRequest(operator,EdValue.text,CheckNum.checked,index)
End Sub

'============================================ 
' Evenement OnSearch 
'============================================ 
Sub Search(index as long)
  if dbase.IsOpen()=true then
    if dbase.GetRecordUdt(index,client)=true then
      indexsearch++
      StringGrid1.Cell(1,indexsearch)=client.name
      StringGrid1.Cell(2,indexsearch)=client.Compagny
      StringGrid1.Cell(3,indexsearch)=client.City
      StringGrid1.Cell(4,indexsearch)=client.Postal
      StringGrid1.Cell(5,indexsearch)=client.Country
    end if
  end if
End Sub

'============================================ 
' Evenement menu quitter 
'============================================ 
Sub mnExitClick
  application.terminate
End Sub