Thursday, October 28, 2010

In VB 2008, how would I go about saving the input a user entered?

I made this program in VB 2008. The program is pretty simple. The user inputs values and clicks the button to output the sum. How do I save those values and the sum so that when the user exits the program and then starts the program, the values are still there?In VB 2008, how would I go about saving the input a user entered?
Private Sub LoadValues()

Dim iFileNum As Short



iFileNum = FreeFile()

FileOpen(iFileNum, My.Application.Info.DirectoryPath %26amp; ';\SumValues.txt';, OpenMode.Input)



'Get Value1

Input(iFileNum, Value1)

'Get Value2

Input(iFileNum, Value2)

'Get Sum

Input(iFileNum, Sum)



FileClose(iFileNum)

End Sub



Private Sub SaveValues()

Dim iFileNum As Short



iFileNum = FreeFile()

FileOpen(iFileNum, My.Application.Info.DirectoryPath %26amp; ';\SumValues.txt';, OpenMode.Output)



'Save Value1

WriteLine(iFileNum, Value1)

'Save Value2

WriteLine(iFileNum, Value2)

'Save Sum

WriteLine(iFileNum, Sum)



FileClose(iFileNum)



End Sub



This saves values in a text file %26amp; reloads them. You need to:

Call LoadValues in your Form_Load, then load the variables (Value1, Value2 %26amp; Sum) into your text boxes.

Load textbox values into the variables and then call SaveValues when you do the calculation or when you close the program.

You'll need to Dim the variables as global to whatever type you want (Integer, Double, etc.)In VB 2008, how would I go about saving the input a user entered?
That error means that you are reading past the end of the file. If you write 3 values and then read 3 values only, it should work. I copied this from a working program so should work as listed. If you can't find it, go to my website at www.Cybertrek.info and email me the .vb file for your form.

Report Abuse


One more thing. The first time you run this, you need to comment out the call to the LoadValues routine since you have not created the text file yet (created in SaveValue). If you make an installer, be sure to include a copy of SumValues.txt or check if file exists before loading.

Report Abuse

No comments:

Post a Comment