Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving Variables To C: 2

Status
Not open for further replies.

bluenoser337

Programmer
Jan 31, 2002
343
CA
I want to save and read 10 variables to/from drive C: What's the simplest code for doing that? Let's call them gVari1, gVari2, etc. THANKS!!!!
 
It may be easier to use the getsetting and savesetting functions and read the variables from your registry. Just requires one line of code to read and one line of code to save. Thanks and Good Luck!

zemp
 
I think I'd like to keep the registry out of this...and besides I might want the values read as a CSV file by some other program. Just wondering how to write values to a file on the HDD.
 
Then just create a text file.

Open Filename.txt For Output Access Write As #1
Print #1, Variable1 & "," & variable2 & "," ...
Close #1

You can add a common dialog control to help in naming and location your file as well. Thanks and Good Luck!

zemp
 
I'm getting them in the file okay with the commas between the variables...but when I input the values with Input #1, Variable1, Variable2, etc it is not getting the data between the commas...instead it is breaking it up if there is a space, etc. Help. :(
 
I don't quite understand what you mean. You should be creating a text file with one line in it. Then when you open the file to read you place the entire line into a variable.

Input #1, strVariable

strVariable should contain "Variable1,variable2,etc.".

Then I would use the split function to separate the variabales into an array.

dim strArray() as string
strArray=split(strvariable,",")'split on the comma

Now you should be able to access the variables by array element number. Thanks and Good Luck!

zemp
 
I'm new to the split function...
I have:
dim strArray() as string
strArray=split(strvariable,",")
There are 10 variables...what code comes next to assign each split value to these 10 variables. I keep getting errors no matter what I try. Do I need dim strArray(10)? Thanks!
 
The split function takes care of the number of array elements.

Post your code and the errors that you are getting. Thanks and Good Luck!

zemp
 
Input #1, FileVAriable
Dim myArray() As String
myArray() = Split(FileVariable,",")
gVariable0 = myarray(0)
gVariable1 = myArray(1) etc etc

Error is: "Subsrcipt out of range" on the myArray(1) line

Thanks!
 
I think I forgot that that the input function uses a comma as a deliminating character. This is what I have.

A text file called test.txt containing "10,20,30,40,50" on one line.

Dim strFile As String, strVariable As String
Dim myArray() As String, i as integer

Redim myArray(0) as String
strFile = App.Path & "\test.txt"
Open strFile For Input As #1
Do While Not EOF(1)
if i > 0 Then Redim Preserve myArray(i) as String
Input #1, strVariable
myArray(i) = strVariable
i = i + 1
Loop

This should put the values into the array properly.
Thanks and Good Luck!

zemp
 
zemp...thanks...got it working! Had to take out the Dim myArray() As String because the first ReDim gave an error.
Thanks again!!
 
zemp: you said It may be easier to use the getsetting and savesetting functions and read the variables from your registry. Just requires one line of code to read and one line of code to save.

How would you do that?
Is using the registry a problem? -- Just trying to help...
[wolf]<--- This is a wolf? We need a new icon.......
mikewolf@tst-us.com
 
From MSDN:

Creating or Saving Application Settings


You can use the SaveSetting statement to save a new value for a registry key stored in your application's registry location. For example, you could add code to the Form_Unload event in the application's main form in order to preserve settings at shutdown, or in the Form_Unload event on an Options dialog box to update user preferences.

Use the following syntax for the SaveSetting statement:

SaveSetting appname, section, key, value

The following code saves new values for the Backup and LastEntry keys in the Startup section of the registry for an application named &quot;RegCust.&quot; This code assumes that the variables strDate and intLastEntry contain the new values.

Private Sub Form_Unload(Cancel As Integer)
SaveSetting &quot;RegCust&quot;, &quot;Startup&quot;, &quot;Backup&quot;, strDate
SaveSetting &quot;RegCust&quot;, &quot;Startup&quot;, &quot;LastEntry&quot;, _
intLastEntry
End Sub

Retrieving Application Settings


You can use the GetSetting and GetAllSettings functions to retrieve registry values stored in your application's registry location. For example, your application can retrieve registry settings to recreate its condition at the time it was closed.

One Setting at a Time
To retrieve a single registry setting, use the following syntax for the GetSetting function:

GetSetting(appname, section, key[, default])

The following code retrieves the value of the LastEntry key in the &quot;RegCust&quot; application's Startup section, and displays the value in the Immediate window.

Private Sub Form_Load()
Dim intLastEntry As Integer
intLastEntry = GetSetting(&quot;RegCust&quot;, &quot;Startup&quot;, _
&quot;LastEntry&quot;, &quot;0&quot;)
Debug.Print intLastEntry
End Sub

Note that you can use the optional parameter, default, to set the value returned by Visual Basic when there is no value listed in the registry for the specified key.



To get what you want, you have to go through the crap, but to get through the crap you have to know what you want... [rockband]
 
As Dazz22 has posted. Thanks and Good Luck!

zemp
 
TY Dazz & Zemp - works great and is SO EASY!!!!!! Stars for both of you.... -- Just trying to help...
[wolf]<--- This is a wolf? We need a new icon.......
mikewolf@tst-us.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top