' (c) Fernando Perez Rios - Santander (SPAIN) - Enero 2006
' FormRecord.bas = Ver registros en un formulario
' View records in forms
' Voir enregistrements avec formulaires
' sans finir encore!!!

$INCLUDE "qDataBase_v1.inc"
CONST cr = CHR$(13)
CONST nChamps = 8

TYPE Record
   Dossier  AS STRING * 8
   Cni      AS STRING * 12
   Nom      AS STRING * 50
   Domicile AS STRING * 50
   Postal   AS STRING * 5
   Localite AS STRING * 50
   Compte   AS STRING * 20
   Montant  AS DOUBLE
END TYPE

DIM db AS qDatabase
DIM rs AS qRecordset
DIM reg AS Record

' -------------------------------
' Functions API
DECLARE FUNCTION SetFocus _
        Lib "user32.dll" _
        Alias "SetFocus" _
       (hwnd AS LONG) _
             AS LONG
DECLARE FUNCTION MensajeBox _
        Lib "user32" _
        Alias "MessageBoxA" _
       (hWND      AS LONG, _
        lpText    AS STRING, _
        lpCaption AS STRING, _
        wType     AS LONG) _
                  AS LONG

' -------------------------------
DECLARE SUB Lanza
DECLARE SUB DrawLine
DECLARE SUB UDT2form
DECLARE SUB form2UDT
DECLARE SUB CloseDB
DECLARE SUB btnOnClick(Button AS qButton)
DECLARE SUB Modified
DECLARE SUB goto_Rec(Key AS BYTE)
DECLARE SUB viewButtons(Mode AS BOOLEAN)
DECLARE SUB btnCancel_OnClick
DECLARE SUB btnSave_OnClick

' -------------------------------
DIM WebFont AS QFont
    WebFont.Name = "Webdings"
    WebFont.AddStyles(0) ' fsBold = 0
    WebFont.Size = 10

DIM PeqFont AS QFont
    PeqFont.Name = "Arial"
    PeqFont.Size = 7
 
DIM Comic AS QFont
    Comic.Name = "Comic"
	 Comic.Size = 10   

CREATE Form AS qForm
    Width = 350
    Height = 230
    Center
    Caption = "Access & Rapid-Q"
    BorderStyle = 3    ' bsDialog = 3
    Center
    OnPaint = DrawLine
    OnShow = Lanza
    OnClose = CloseDB

    CREATE RecNum AS QEdit
       Top = 165
       Left = 72
       Width = 85
       Height = 20
       Font = PeqFont
       Color = rgb(255,255,195)
       OnKeyPress = goTo_Rec
    END CREATE

    CREATE btnExit AS QButton
'      Caption = "Salir"
      Caption = "Exit"
      Top = 162
      Left = 265
      Width = 54
      OnClick = CloseDB
   END CREATE

   CREATE btnAdd AS QButton
'      Caption = "Grabar"
      Caption = "Save"
      Top = 162
      Left = 230
      Width = 54
      Visible = False
      OnClick = btnSave_OnClick
   END CREATE

   CREATE btnCancel AS QButton
'      Caption = "Cancelar"
      Caption = "Cancel"
      Top = 162
      Left = 285
      Width = 54
      Visible = False
      OnClick = btnCancel_OnClick
   END CREATE

END CREATE

' ------------------------------------------------------------------------
DIM txt(1 TO nChamps) AS QEdit
DIM lbl(1 TO nChamps) AS QLabel
DIM i AS INTEGER

FOR i = 1 TO nChamps
   lbl(i).Parent = Form
   txt(i).Parent = Form
   lbl(i).Height = 20
   txt(i).Height = 20
   lbl(i).Font = Comic
   
   READ lbl(i).Caption
   READ lbl(i).Left
   READ lbl(i).Top
   READ lbl(i).Width
   READ txt(i).Left
   READ txt(i).Top
   READ txt(i).Width

NEXT i

' Caption, lblLeft, lblTop, lblWidth, txtLeft, txtTop, txtWidth
DATA "Dossier",   12,   8, 55,  75,   6,  84
DATA "CNI",      180,   8, 55, 210,   6, 108
DATA "Nom",       12,  38, 55,  75,  36, 242
DATA "Domicile",  12,  68, 55,  75,  66, 242
DATA "Localité",  12,  98, 55,  75,  96,  45
DATA "-",        125,  98, 55, 134,  96, 183
DATA "Compte",    12, 128, 55,  75, 126,  95
DATA "Montant",  178, 128, 55, 235, 126,  82

' =====================================================
DIM btn(1 TO 5) AS QButton

FOR i = 1 TO 6
    btn(i).Parent = Form
    btn(i).Top = 165
    btn(i).Width = 20
    btn(i).Height = 20
    btn(i).Font = WebFont
    btn(i).Cursor = -21 ' CONST crHandPoint = -21
    btn(i).OnClick = btnOnClick
    READ btn(i).Caption
    READ btn(i).Left
    READ btn(i).Tag
NEXT i

DATA "r",  10, 1
DATA "9",  30, 2
DATA "7",  50, 3
DATA "8", 160, 4
DATA ":", 180, 5
DATA "=", 200, 6

Form.ShowModal

END

' -------------------------------------------------------------------------
SUB Lanza
' Abrir Base de Datos
   db.OpenDatabase("Test.mdb")
' Abrir Tabla
   rs.OpenRecordset("Sample")
' número campos   --> rs.fieldCount
' nombre campos   --> rs.fieldName(i)
' tipo campo      --> rs.fielTypeName(i)
' total registros --> rs.recordCount
' registro actual --> rs.idRec
' ultimo registro --> rs.EOF = True
' primer registro --> rs.BOF = True

' Obtener TODOS los registros:
   rs.getRecord(ptrUDT(reg))
' Mover al último registro:
   rs.moveLast(ptrUDT(reg))
' Volcar UDT a formulario:   
   UDT2Form

END SUB

' ------------------------------------------------------------------
SUB btnOnClick(Button AS qButton)
DIM i AS INTEGER 

   Modified
   
   SELECT CASE Button.Tag
      CASE 1
         IF MensajeBox(Form.Handle, "¿Borrar registro?", _
                      "Precaución", 4+16) = 7 THEN EXIT SUB
         rs.del(ptrUDT(reg)) 
      CASE 2  ' FIRST
         rs.moveFirst(ptrUDT(reg))
      CASE 3  ' PRIOR
         rs.movePrevious(ptrUDT(reg))
      CASE 4  ' NEXT
         rs.moveNext(ptrUDT(reg))
      CASE 5  ' LAST
         rs.moveLast(ptrUDT(reg))
      CASE 6  ' NEW
         ViewButtons(True)
         FOR i = 1 TO rs.fieldCount : txt(i).Text = " " : NEXT i
         SetFocus(txt(1).Handle) 
   END SELECT

   UDT2form

END SUB

' ---------------------------------------------------------------
SUB Modified
DIM i AS INTEGER
DIM isModified AS INTEGER
DIM nRec AS LONG
 
   isModified = False
   FOR i = 1 TO nChamps
      IF txt(i).Modified THEN
         isModified = True 
         txt(i).Modified = False
      END IF   
   NEXT i
   IF IsModified = False THEN EXIT SUB

   IF MensajeBox(Form.Handle, "¿Actualizar registro?", _
	             "Datos modificados", 4+32) = 7 THEN EXIT SUB

   nRec = rs.idRec
   form2UDT
   rs.Edit(ptrUDT(reg))
   rs.moveTo(ptrUDT(reg), nRec)

END SUB

' ------------------------------------------------------------------
SUB goTo_Rec(Key AS BYTE)

   IF Key <> 13 THEN EXIT SUB

   DIM nRec AS LONG : nRec = VAL(RecNum.Text)
   
   IF (nRec > 0) AND (nRec < rs.recordCount) THEN
      rs.moveTo(ptrUDT(reg), nRec)
   ELSE   
      ShowMessage "Registro inexistente"
   END IF   

   UDT2form

END SUB

' ---------------------------------------------------------------
SUB ViewButtons(Mode AS BOOLEAN)
    btnExit.Visible   = IIF(Mode = False, True, False)
    btnAdd.Visible    = IIF(Mode = False, False, True)
    btnCancel.Visible = IIF(Mode = False, False, True)
END SUB

' ------------------------------------------------------------------
SUB btnSave_OnClick
   ViewButtons(False)
   form2UDT
   rs.Add(ptrUDT(reg))
   rs.moveLast(ptrUDT(reg))
   UDT2Form
END SUB

' ------------------------------------------------------------------
SUB btnCancel_OnClick
DIM i AS INTEGER
   ViewButtons(False)
   FOR i = 1 TO nChamps : txt(i).Modified = False : NEXT i
   UDT2Form
END SUB

' ------------------------------------------------------------------
SUB UDT2form
   txt(1).Text = reg.Dossier
   txt(2).Text = reg.Cni       
   txt(3).Text = reg.Nom    
   txt(4).Text = reg.Domicile 
   txt(5).Text = reg.Postal    
   txt(6).Text = reg.Localite 
   txt(7).Text = reg.Compte    
   txt(8).Text = STR$(reg.Montant)
   recNum.Text = SPACE$(2) & STR$(rs.idRec) & " de " & STR$(rs.recordCount)   

   btn(2).Enabled = IIF(rs.idRec = 1, False, True)  ' FIRST
   btn(3).Enabled = IIF(rs.idRec = 1, False, True)  ' PRIOR
   btn(4).Enabled = IIF(rs.idRec = rs.recordCount, False, True)  ' NEXT
   btn(5).Enabled = IIF(rs.idRec = rs.recordCount, False , True) ' LAST

 ''  btn(2).Enabled = IIF(rs.BOF, False, True)  ' FIRST
 ''  btn(3).Enabled = IIF(rs.BOF, False, True)  ' PRIOR
 ''  btn(4).Enabled = IIF(rs.EOF, False, True)  ' NEXT
 ''  btn(5).Enabled = IIF(rs.EOF, False , True) ' LAST



END SUB

' ------------------------------------------------------------------
SUB form2UDT
   reg.Dossier  = txt(1).Text 
   reg.Cni      = txt(2).Text        
   reg.Nom      = txt(3).Text     
   reg.Domicile = txt(4).Text 
   reg.Postal   = txt(5).Text    
   reg.Localite = txt(6).Text  
   reg.Compte   = txt(7).Text     
   reg.Montant  = VAL(txt(8).Text)
END SUB

' ------------------------------------------------------------------
SUB CloseDB
   db.Close
   Application.Terminate
END SUB

' ------------------------------------------------------------------
SUB DrawLine
    Form.Line ( 0, 153, 343, 153, &HFF0000)
    Form.Line ( 0, 154, 343, 154, &H0000FF)
END SUB
