Saving & Retreiving VB6

Hi there everyone,

I have been trying to use the Commondialog control to save information from a textbox and retrieve it at some point but the problem is, I can't seem to retrieve more than the variables a,b,c,d which were originally programmed to save as "Name", "LastName", "Phone" and "Coutry". Can someone please advice me on how to save more than just these four properties? Code included below. Thanks:-

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", txtName.Text
pb.WriteProperty "Payroll Number", txtPayroll.Text
pb.WriteProperty "Address", txtAddress.Text
pb.WriteProperty "Department", cboSelect.Text
pb.WriteProperty "Claim", txtClaim.Text
pb.WriteProperty "Hours Wrk", Form2.txthrsWorked.Text
pb.WriteProperty "Hourly Rate", Form2.txthrrate.Text
pb.WriteProperty "Total", Form2.txttotal.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

' If user pressed cancel button exit sub.
errhandler:
Exit Sub
End Sub


Private Sub Open_Click()
Dim varTemp As Variant
Dim byteArr() As Byte
' This is necesary to convert from Variant to Byte.
' If you dont 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("Payroll Number")
c = pb.ReadProperty("Address")
d = pb.ReadProperty("Department")
e = pb.ReadProperty("Claim")
f = pb.ReadProperty("Hours Wrk")
g = pb.ReadProperty("Hourly Rate")
h = pb.ReadProperty("Total")

' Now put the value of the variables in textboxes
txtName.Text = a
txtPayroll.Text = b
txtAddress.Text = c
cboSelect.Text = d
txtClaim.Text = e
Form2.txthrsWorked.Text = f
Form2.txthrrate.Text = g
Form2.txttotal.Text = h
errhandler:
Exit Sub
End Sub

Comments

  • [b][red]This message was edited by DarQ at 2002-10-29 4:34:48[/red][/b][hr]
    Easy without propertybag. Please note that your textboxes should be an array before trying this code. Does not include comdialog

    [code]
    private sub Save(strPath as string)
    dim i as integer
    dim intFile as integer

    intFile = Freefile 'Get the first free file pointer
    open strPath for output as #intFile ' Open the file for writing
    for i = 0 to ubound(txtTextbox) 'loop from textbox 0 to the last one
    Print #intFile,txtTextbox(i)'Write value of textbox(i)
    next i
    close #intFile
    end sub
    [/code]

    Heee!! Cool huh??? Less lines = more pleasure! Let open the file and restore the textbox values

    [code]
    private sub Open(strPAth as string)
    dim i as integer
    dim intFile as integer
    dim strBuffer as string

    intfile = freefile
    open strpath for input as #intFile
    do while not EOF(intFile) ' loop until end of file reached
    line input #intFile,strbuffer ' Read the next line from the file
    txtTextbox(i) = strbuffer ' Put the read line in textbox(i)
    i=i+1 ' hmm, what could this be??
    loop
    Close #intfile
    end sub
    [/code]

    We can get rid of the i=i+1 very easy but then i need to modify the save and open subs.

    good luck






    : Hi there everyone,
    :
    : I have been trying to use the Commondialog control to save information from a textbox and retrieve it at some point but the problem is, I can't seem to retrieve more than the variables a,b,c,d which were originally programmed to save as "Name", "LastName", "Phone" and "Coutry". Can someone please advice me on how to save more than just these four properties? Code included below. Thanks:-
    :
    : 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", txtName.Text
    : pb.WriteProperty "Payroll Number", txtPayroll.Text
    : pb.WriteProperty "Address", txtAddress.Text
    : pb.WriteProperty "Department", cboSelect.Text
    : pb.WriteProperty "Claim", txtClaim.Text
    : pb.WriteProperty "Hours Wrk", Form2.txthrsWorked.Text
    : pb.WriteProperty "Hourly Rate", Form2.txthrrate.Text
    : pb.WriteProperty "Total", Form2.txttotal.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
    :
    : ' If user pressed cancel button exit sub.
    : errhandler:
    : Exit Sub
    : End Sub
    :
    :
    : Private Sub Open_Click()
    : Dim varTemp As Variant
    : Dim byteArr() As Byte
    : ' This is necesary to convert from Variant to Byte.
    : ' If you dont 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("Payroll Number")
    : c = pb.ReadProperty("Address")
    : d = pb.ReadProperty("Department")
    : e = pb.ReadProperty("Claim")
    : f = pb.ReadProperty("Hours Wrk")
    : g = pb.ReadProperty("Hourly Rate")
    : h = pb.ReadProperty("Total")
    :
    : ' Now put the value of the variables in textboxes
    : txtName.Text = a
    : txtPayroll.Text = b
    : txtAddress.Text = c
    : cboSelect.Text = d
    : txtClaim.Text = e
    : Form2.txthrsWorked.Text = f
    : Form2.txthrrate.Text = g
    : Form2.txttotal.Text = h
    : errhandler:
    : Exit Sub
    : End Sub
    :
    :

    [HR]
    Gadver, ik wot ha'k hjir no wr?
    [HR]

    [GREEN]Anyone cares to translate?[/GREEN]



  • : [b][red]This message was edited by DarQ at 2002-10-29 4:34:48[/red][/b][hr]
    : Easy without propertybag. Please note that your textboxes should be an array before trying this code. Does not include comdialog
    :
    : [code]
    : private sub Save(strPath as string)
    : dim i as integer
    : dim intFile as integer
    :
    : intFile = Freefile 'Get the first free file pointer
    : open strPath for output as #intFile ' Open the file for writing
    : for i = 0 to ubound(txtTextbox) 'loop from textbox 0 to the last one
    : Print #intFile,txtTextbox(i)'Write value of textbox(i)
    : next i
    : close #intFile
    : end sub
    : [/code]
    :
    : Heee!! Cool huh??? Less lines = more pleasure! Let open the file and restore the textbox values
    :
    : [code]
    : private sub Open(strPAth as string)
    : dim i as integer
    : dim intFile as integer
    : dim strBuffer as string
    :
    : intfile = freefile
    : open strpath for input as #intFile
    : do while not EOF(intFile) ' loop until end of file reached
    : line input #intFile,strbuffer ' Read the next line from the file
    : txtTextbox(i) = strbuffer ' Put the read line in textbox(i)
    : i=i+1 ' hmm, what could this be??
    : loop
    : Close #intfile
    : end sub
    : [/code]
    :
    : We can get rid of the i=i+1 very easy but then i need to modify the save and open subs.
    :
    : good luck
    :
    :
    :
    :
    :
    :
    : : Hi there everyone,
    : :
    : : I have been trying to use the Commondialog control to save information from a textbox and retrieve it at some point but the problem is, I can't seem to retrieve more than the variables a,b,c,d which were originally programmed to save as "Name", "LastName", "Phone" and "Coutry". Can someone please advice me on how to save more than just these four properties? Code included below. Thanks:-
    : :
    : : 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", txtName.Text
    : : pb.WriteProperty "Payroll Number", txtPayroll.Text
    : : pb.WriteProperty "Address", txtAddress.Text
    : : pb.WriteProperty "Department", cboSelect.Text
    : : pb.WriteProperty "Claim", txtClaim.Text
    : : pb.WriteProperty "Hours Wrk", Form2.txthrsWorked.Text
    : : pb.WriteProperty "Hourly Rate", Form2.txthrrate.Text
    : : pb.WriteProperty "Total", Form2.txttotal.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
    : :
    : : ' If user pressed cancel button exit sub.
    : : errhandler:
    : : Exit Sub
    : : End Sub
    : :
    : :
    : : Private Sub Open_Click()
    : : Dim varTemp As Variant
    : : Dim byteArr() As Byte
    : : ' This is necesary to convert from Variant to Byte.
    : : ' If you dont 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("Payroll Number")
    : : c = pb.ReadProperty("Address")
    : : d = pb.ReadProperty("Department")
    : : e = pb.ReadProperty("Claim")
    : : f = pb.ReadProperty("Hours Wrk")
    : : g = pb.ReadProperty("Hourly Rate")
    : : h = pb.ReadProperty("Total")
    : :
    : : ' Now put the value of the variables in textboxes
    : : txtName.Text = a
    : : txtPayroll.Text = b
    : : txtAddress.Text = c
    : : cboSelect.Text = d
    : : txtClaim.Text = e
    : : Form2.txthrsWorked.Text = f
    : : Form2.txthrrate.Text = g
    : : Form2.txttotal.Text = h
    : : errhandler:
    : : Exit Sub
    : : End Sub
    : :
    : :
    :
    : [HR]
    : Gadver, ik wot ha'k hjir no wr?
    : [HR]
    :
    : [GREEN]Anyone cares to translate?[/GREEN]
    :
    :
    :
    : Thanks a BILLION, it worked

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories