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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving and reading variables to/from a file

Status
Not open for further replies.

OrangeCrush

Technical User
Dec 15, 2010
4
US
Hello,

I'm attempting to implement a "save file" of sorts for a number of Extra basic macros. Ideally, i'd like it to ask a series of questions, then save the user responses to a text file so it remembers the answers the next time the macro runs.

The point I'm stumped at is how to code it to actually read the values from a file to set multiple variables. Is anyone familiar with how I might do this?

Thanks!
 



Hi,

Have you looked at Extra HELP? Check out the Open command.

What code have you tried?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I have. However the help file doesn't have any examples for the input mode, so I have no idea how to pass the values in a text file to variables.
 


ExtraHELP said:
Open (statement) Example
<Close> <Copy> <Print>

This example uses the Open statement to open a file for output.

TestString$ = "The quick brown fox" ' Create test string.
For I% = 1 To 5
FNum% = FreeFile ' Determine next file number.
FName$ = "FILEIOX" + LTrim$(Str$(FNum%)) + ".DAT"
Open FName$ For Output As FNum% ' Open file.
Print #I%, TestString$ ' Write string to file.
Next I%
Close ' Close all files.
Msg$ = "Several test files have been created on your disk. "
Msg$ = Msg$ + "Choose OK to remove the test files."

MsgBox Msg$
Kill "FILEIOX?.DAT" ' Remove test file from disk.

Copyright 1996 - 1999, Attachmate Corporation. All Rights Reserved.
Obviously this example opens a file for OUTPUT.

Maybe TRY opening your file for INPUT?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Here's the test macro I'm trying to get to read and write to/from a text file. I've got the "write" bit working, but I don't know how to read from the file once it's been opened. "Read" is obviously not the correct command:

Code:
Sub Main()

dim Somevariable1 as string
dim Somevariable2 as string
dim Somevariable3 as string


if dir$ ("c:\temp\savedvariables.txt") <> "savedvariables.txt" then
    Somevariable1 = inputbox ("Somevariable1")
    Somevariable2 = inputbox ("Somevariable2")
    Somevariable3 = inputbox ("Somevariable3")
    Open "c:\temp\savedvariables.txt" For Append Access Write Lock Read Write as #1
         Write #1, Somevariable1, Somevariable2, Somevariable3
    Close #1
Else
    
    Open "c:\temp\savedvariables.txt" For Input as #1
        Read #1, Somevariable1, Somevariable2, Somevariable3
    Close #1
    msgbox Somevariable1 & Somevariable2 & Somevariable3
End If

End Sub
 
I figured it out. The command to pass values from a file to variables is "Input." I was almost there in the above example. Here's the working code:

Code:
Sub Main()

dim Somevariable1 as string
dim Somevariable2 as string
dim Somevariable3 as string


if dir$ ("c:\temp\savedvariables.txt") <> "savedvariables.txt" then
    Somevariable1 = inputbox ("Somevariable1")
    Somevariable2 = inputbox ("Somevariable2")
    Somevariable3 = inputbox ("Somevariable3")
    Open "c:\temp\savedvariables.txt" For Append Access Write Lock Read Write as #1
         Write #1, Somevariable1, Somevariable2, Somevariable3
    Close #1
Else
    
    Open "c:\temp\savedvariables.txt" For Input as #1
        Input #1, Somevariable1, Somevariable2, Somevariable3
    Close #1
    msgbox Somevariable1 & Somevariable2 & Somevariable3
End If

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top