'Listing 28.6 --- Baza klientw (cz w module kodu)

Option Base 1

Type NameRec
  FName As String * 15
  LNAme As String * 25
  Address As String * 30
  City As String * 15
  State As String * 2
  Zip As String * 10
End Type

Public Index As Integer
Global Const MaxRecords As Integer = 25
Public Reclen As Integer
Public Contacts(MaxRecords) As NameRec


'Listing 28.6 --- Baza klientw (cz w formularzu)

' Przepisz zawarto pl z formularza do struktury
Sub Get_Data()
  Contacts(Index).FName = CnctForm.FirstName
  Contacts(Index).LNAme = CnctForm.LastName
  Contacts(Index).Address = CnctForm.Address
  Contacts(Index).City = CnctForm.City
  Contacts(Index).State = CnctForm.State
  Contacts(Index).Zip = CnctForm.Zip
End Sub

' Czy pola biecej struktury
Sub Clear_Record()
  Contacts(Index).FName = ""
  Contacts(Index).LNAme = ""
  Contacts(Index).Address = ""
  Contacts(Index).City = ""
  Contacts(Index).State = ""
  Contacts(Index).Zip = ""
End Sub

' Czy pola formularza
Sub Clear_Form()
  CnctForm.FirstName = ""
  CnctForm.LastName = ""
  CnctForm.Address = ""
  CnctForm.City = ""
  CnctForm.State = ""
  CnctForm.Zip = ""
End Sub

' Czy biecy rekord
Private Sub Command1_Click()
  Clear_Record
  CLear_Form
End Sub

' Zapisz wszystkie rekordy
Private Sub Command2_Click()
  Cnumber = FreeFile
  Open "Contacts.Dat" For Random As #Cnumber Len = RecLen
  For X = 1 To MaxRecords
    Put #Cnumber, X, Contacts(X)
  Next X
  Close #Cnumber
  MsgBox "Zapisano wszystkie rekordy" , , "Zapisz wszystko"
  Get_Data
  Update_Form
End Sub

' Odczytaj wszystkie rekordy
Private Sub Command3_Click()
  Cnumber = FreeFile
  Open "Contacts.Dat" For Random As #Cnumber Len = RecLen
  For X = 1 To MaxRecords
    Get #Cnumber, X, Contacts(X)
  Next X
  Close #Cnumber
  Index = 1
  Update_Form 
  MsgBox "Odczytano wszystkie rekordy" , , "Odczytaj wszystko"
End Sub

' Nastpny rekord
Private Sub Command4_Click()
  CnctForm.RecordNum = Index
  If Index < MaxRecords Then
    Get_Data
    Index = Index + 1
    Update_Form
  End If
End Sub

' Poprzedni rekord
Private Sub Command5_Click()
  CnctForm.RecordNum = Index
  If Index > 1 Then
    Get_Data
    Index = Index - 1
    Update_Form
  End If
End Sub

' Zapisz biecy rekord
Private Sub Command6_Click()
  Cnumber = FreeFile
  Open "Contacts.Dat" For Random As #Cnumber Len = RecLen
  Put #Cnumber, Index, Contacts(Index)
  Close #Cnumber
  MsgBox "Zapisano rekord nr " & Index, , "Zapisz rekord"
End Sub

' Aktualizacja formularza na podstawie biecego rekordu
Sub Update_Form()
  CnctForm.FirstName = Contacts(Index).FName
  CnctForm.LastName = Contacts(Index).LNAme
  CnctForm.Address = Contacts(Index).Address
  CnctForm.City = Contacts(Index).City
  CnctForm.State = Contacts(Index).State
  CnctForm.Zip = Contacts(Index).Zip
  CnctForm.RecordNum = Index
End Sub

' Nadanie wartoci pocztkowych
Private Sub Form_Load()
  Index = 1
  CnctForm.RecordNum = Index
  Reclen = Len(Contacts(1))
End Sub


