2
votes

I am attempting to 'save' the context of a text box in vb6 to a *.ini file, so that it can be used in a later part of the program. (i.e. the user would enter something into the text box, then later in the program, a label would appear with the user-entered, saved information).

I used the following code which I copied from the source of someone else's program, however it hasn't worked:

Dim fsys As New FileSystemObject 
Dim outstream As TextStream
Dim write1 As String
Dim val1 As String
val1 = Text1.Text
inisettings = App.Path & "\Variables.ini"
Set outstream = fsys.OpenTextFile(inisettings, ForWriting, False, TristateFalse)
outstream.WriteLine (val1)
Set outstream = Nothing

This is the result:

Error message

Does anyone have any way to save data for later?

3
@Danh, if an answer solves your problem, mark it as the solution.Herb
@Herb not my questionDanh
@Doofitator if an answer solves your problem, mark it as the solution.MarkJ
Next time please indicate the line number that the error is happening on.vbguyny

3 Answers

4
votes

FileSystemObject lives inside an external library, to use it click Project then References and tick Microsoft Scripting Runtime.

You don't actually need to do any of that, the code below uses VB's built-in functionality to write a file.

Dim hF As Integer
hF = FreeFile()

Open App.Path & "\Variables.ini" For Output As #hF
Print #hF, val1
Close #hF
2
votes

You must declare TristateFalse and give it a value like 0, 1 or 2.

You can take a look at this link: https://msdn.microsoft.com/en-us/subscriptions/bxw6edd3(v=vs.84).aspx

1
votes

The reason why you are getting this error is because you don't have a reference to the Microsoft Scripting Runtime library. Follow the below instructions while in your VB6 project:

  1. From the top menu, click on Project > References.
  2. From the list, check the item entitled "Microsoft Scripting Runtime".
  3. Click OK.

This will resolve your immediate error however your code still has some other issues. First off, you forgot to declare the variable inisettings. I am going to assume that you will want to always overwrite the entire file each time you update the INI file so you want to use the method CreateTextFile instead of OpenTextFile.

Dim fsys As New FileSystemObject 
Dim outstream As TextStream
Dim write1 As String
Dim val1 As String
Dim inisettings As String

val1 = Text1.Text
inisettings = App.Path & "\Variables.ini"
Set outstream = fsys.CreateTextFile(inisettings, True, False)
Call outstream.WriteLine(val1)
Set outstream = Nothing

Good luck!