×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Saving and reading variables to/from a file

Saving and reading variables to/from a file

Saving and reading variables to/from a file

(OP)
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!

RE: Saving and reading variables to/from a file




Hi,

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

What code have you tried?

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Saving and reading variables to/from a file

(OP)
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.

RE: Saving and reading variables to/from a file



Quote (ExtraHELP):

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,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: Saving and reading variables to/from a file

(OP)
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

RE: Saving and reading variables to/from a file

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close