CodeItBetter Programming Another VB Programming Blog

How to save data from different textboxes in the same file.

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'Controls - How to save data from different textboxes in the same file.
'This is an example of how to save data from different textboxes in the same file.

'We need 4 textboxes, 3 buttons and a commondialog control. Use the text boxes
'to store some data like name,last name, phone and country. Name the buttons
'open, save and clear.

'Declare some private variables (Used at module level to declare private
'variables and allocate storage space).

'Declare a PropertyBag object.
Private pb As PropertyBag
'This strings will represent the text boxes.
Private a As String
Private b As String
Private c As String
Private d As String
 
'Saving the data... Here we create a property bag to store the data and save the
'data of the textboxes as variant in a file.
Private Sub Save_Click()
    'The data stored will be saved as variant
    Dim varTemp As Variant
    'This is if the user press the cancel button.
    On Error GoTo errhandler
 
    CommonDialog1.ShowSave
 
    'Create new property bag.
    Set pb = New PropertyBag
 
    'We save the text in the property bag and give a name to every string.
    pb.WriteProperty "Name", Text1.Text
    pb.WriteProperty "LastName", Text2.Text
    pb.WriteProperty "Phone", Text3.Text
    pb.WriteProperty "Country", Text4.Text
 
    'We convert the propertybag contents to variant.
    varTemp = pb.Contents
 
    'Now we open a file, save the data and put it a name (With the common 
    'dialog).
    Open CommonDialog1.FileName For Binary As #1
    Put #1, , varTemp
    Close #1
 
errhandler:
    'If user pressed cancel buton exit sub.
    Exit Sub
End Sub
 
'To open the data saved... Here we open the file, we convert the variant data to
'byte and read the data from a property bag, then we store the data in the
'textboxes.
Private Sub Open_Click()
    Dim varTemp As Variant
    Dim byteArr() As Byte
 
    'This is necesary to convert from Variant to Byte. If you don't do that,the
    'data will not be visible in the text boxes.
    On Error GoTo errhandler
 
    CommonDialog1.ShowOpen
 
    Set pb = New PropertyBag
 
    'Open the file and store the data in a variable
    Open CommonDialog1.FileName For Binary As #1
    Get #1, , varTemp
    Close #1
 
    'Convert the variant to byte.
    byteArr = varTemp
    ' Property bag contents as byte.
    pb.Contents = byteArr
 
    'Store the read data to a variable.
    a = pb.ReadProperty("Name")
    b = pb.ReadProperty("LastName")
    c = pb.ReadProperty("Phone")
    d = pb.ReadProperty("Country")
 
    'Now put the value of the variables in  textboxes
    Text1.Text = a
    Text2.Text = b
    Text3.Text = c
    Text4.Text = d
 
errhandler:
    Exit Sub
End Sub
 
'Clear the contents of the text boxes
Private Sub Clear_Click()
    Text1.Text = ""
    Text2.Text = ""
    Text3.Text = ""
    Text4.Text = ""
End Sub
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.