Tipp 0160 Speichern mittels PropertyBag
Autor/Einsender:
Datum:
  Michael Werner
20.02.2005 (Update)
Entwicklungsumgebung:   VB 6
Dieser Tipp zeigt, wie man mittels des PropertyBag-Objektes Eingaben in TextBoxen, CheckBoxen usw. ziemlich einfach speichern und wieder auslesen kann.
 
Option Explicit

Private m_strFileName As String

Private t0 As String, t1 As String, t2 As String
Private t3 As String, t4 As String
Private c1 As String

Private Sub Form_Load()
  t0 = "Nachname"
  t1 = "Vorname"
  t2 = "Ort"
  t3 = "Straße"
  t4 = "eMail"
  c1 = chkBox.Caption

  m_strFileName = GetDATFileName
  Call GetFormData
End Sub

Private Function GetDATFileName() As String
  Const cFileName As String = "pb.dat"
  Dim strPath As String

  strPath = App.Path
  If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
  GetDATFileName = strPath & cFileName
End Function

Private Sub GetFormData()
  Dim FN    As Integer
  Dim pb    As PropertyBag
  Dim varPB As Variant

  On Error Resume Next
  If Len(Dir(m_strFileName, vbNormal)) <> 0 Then
    FN = FreeFile()
    Open m_strFileName For Binary As #FN
    Get #FN, , varPB
    Close #FN

    Set pb = New PropertyBag
    pb.Contents = varPB

    txtFeld(0).Text = pb.ReadProperty(t0, "Nachname")
    txtFeld(1).Text = pb.ReadProperty(t1, "Vorname")
    txtFeld(2).Text = pb.ReadProperty(t2, "Wohnort")
    txtFeld(3).Text = pb.ReadProperty(t3, "Straße")
    txtFeld(4).Text = pb.ReadProperty(t4, "eMail")
    chkBox.Value = pb.ReadProperty(c1, "1")
  End If
  On Error GoTo 0
End Sub

Private Sub SaveFormData()
  Dim FN  As Integer
  Dim pb  As PropertyBag

  Set pb = New PropertyBag

  pb.WriteProperty t0, txtFeld(0).Text
  pb.WriteProperty t1, txtFeld(1).Text
  pb.WriteProperty t2, txtFeld(2).Text
  pb.WriteProperty t3, txtFeld(3).Text
  pb.WriteProperty t4, txtFeld(4).Text
  pb.WriteProperty c1, chkBox.Value

  FN = FreeFile()
  Open m_strFileName For Binary As #FN
  Put #FN, , pb.Contents
  Close #FN
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Call SaveFormData
End Sub
 
Weitere Links zum Thema
Programm-Einstellungen speichern (INI-Datei)
Programm-Einstellungen speichern (SaveSetting)

Windows-Version
95
98/SE
ME
NT
2000
XP
Vista
Win 7
VB-Version
VBA 5
VBA 6
VB 4/16
VB 4/32
VB 5
VB 6


Download  (3 kB) Downloads bisher: [ 1751 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Projekte | Tutorials | API-Referenz | VB-/VBA-Tipps | Komponenten | Bücherecke | VB/VBA-Forum | VB.Net-Forum | DirectX-Forum | Foren-Archiv | DirectX | VB.Net-Tipps | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Sonntag, 28. August 2011