Solved

saving varients to a file

Posted on 2000-04-07
12
135 Views
Last Modified: 2010-05-02
Hello,
I have to save my project's variants to a file, so whenever I will open the file I will be able to see the variant's values ,
In other words I want to be able to write to a file.
Thank you for helping me…
                                         chen.
0
Comment
Question by:chen_meir
12 Comments
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 2695104
to read the file use somthing like this
----------------------------------------

Dim fh As Integer
fh = FreeFile()

    Open MyFilePathAndName For Input As #fh
        Line Input #fh, Value1
        Line Input #fh, Value2
        Line Input #fh, Value3
        Line Input #fh, Value4
        Line Input #fh, Value5
    Close #fh


to Write the file use
----------------------------------------
Dim fh As Integer
fh = FreeFile()
   
 Open MyFilePathAndName For Output As #fh
        Print #fh, Value1
        Print #fh, Value2
        Print #fh, Value3
        Print #fh, Value4
        Print #fh, Value5
    Close #fh

Good luck
0
 
LVL 27

Expert Comment

by:Ark
ID: 2695191
Hi
Above sample is for strings
If you don't know format of variables, it's better use "Write" statement:

Dim n As Integer
n = FreeFile
     
 Open "c:\test.txt" For Output As #
      Write #n, "This is a string"
'output:  "This is a string"
      Write #n, 12345
'output: 12345
      Write #n, Now
'output #2000-04-08 10:40:30"
      Write #n, True
'output #True#
      Write #n, Null
'output #Null#
    Close #n

when you'll read this file using Input statement, all datas will read correctly.
Cheers
0
 

Expert Comment

by:john_price
ID: 2695195
Why not use the registry?
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2695248
In both examples above.. each field is written as a record.. multiple writes and multiple reads to get. To put/get all 6 fields in one record:

<----- Code Begin ----->

Dim MyVar1 As String
Dim MyVar2 As Integer
Dim MyVar3 As Variant
Dim MyVar4 As Long
Dim MyVar5 As Boolean
Dim MyVar6 As Date

MyVar1 = "String"
MyVar2 = 99
MyVar3 = "Variant"
MyVar4 = -12345
MyVar5 = True
MyVar6 = Now

Dim intFile As Integer
intFile = FreeFile
     
' WRITE the File

Open "c:\MyPath\MyFile.txt" For Output As #intFile
Write #intFile, MyVar1; MyVar2; MyVar3; MyVar4; MyVar5; MyVar6
Close #intFile

MyVar1 = ""
MyVar2 = 0
MyVar3 = ""
MyVar4 = 0
MyVar5 = False
MyVar6 = #12/31/1999#

' READ The File
Open "c:\MyPath\MyFile.txt" For Input As #intFile
Input #intFile, MyVar1, MyVar2, MyVar3, MyVar4, MyVar5, MyVar6
Close #intFile

MsgBox (MyVar1 & " " & _
   MyVar2 & " " & _
   MyVar3 & " " & _
   MyVar4 & " " & _
   MyVar5 & " " & _
   MyVar6)

<----- Code End ----->
0
 
LVL 27

Expert Comment

by:Ark
ID: 2695282
:-))))
Private Type MyVars
  MyVar1 As String
  MyVar2 As Integer
  MyVar3 As Variant
  MyVar4 As Long
  MyVar5 As Boolean
  MyVar6 As Date
End Type
Dim MV As MyVars


Private Sub Command1_Click()
MV.MyVar1 = "String"
MV.MyVar2 = 99
MV.MyVar3 = "Variant"
MV.MyVar4 = -12345
MV.MyVar5 = True
MV.MyVar6 = Now

Open "c:\testfile" For Random As #1 Len = Len(MV)
Put #1, , MV
Close #1
End Sub

Private Sub Command2_Click()
Open "c:\testfile" For Random As #1 Len = Len(MV)
Get #1, , MV
Close #1
MsgBox MV.MyVar1 & MV.MyVar2 & MV.MyVar3 & MV.MyVar4 & MV.MyVar5 & MV.MyVar6
End Sub
'vb.hlp - rulezzz :-)
Cheers
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2695301
Ark:
Random access requires a FIXED record layout / length. The String we can fix, but the Variant is definitely a no no.. <smile>.

Change your Data Type to Read..

Private Type MyVars
  MyVar1 As String * 80
  MyVar2 As Integer
  MyVar3 As Variant <-- Can't use for random
  MyVar4 As Long
  MyVar5 As Boolean
  MyVar6 As Date
End Type

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 27

Expert Comment

by:Ark
ID: 2695325
Nope
Wsh2, did you try code?
Strings don't need be fixed length - VB put 2 bytes before string - it's length (of course, you can't read this file as text). About variants -
MV.MyVar3 = "Variant" -works OK
MV.MyVar3 = 1234567 -works OK
MV.MyVar3 = True -works OK
MV.MyVar3 = Now -works OK
MV.MyVar3 = Null -works OK
MV.MyVar3 = Picture1.Picture - still works OK (write handle)
MV.MyVar3 = Array (1,2,3,4,5,6) :-(

Cheers
0
 
LVL 27

Expert Comment

by:Ark
ID: 2695343
Wsh2,
Sorry, now agree - strings can be only up to 10 chars length. But we can save for binary.

Private Type MyVars
  MyVar1 As String
  MyVar2 As Integer
  MyVar3 As Variant
  MyVar4 As Long
  MyVar5 As Boolean
  MyVar6 As Date
End Type
Dim MV As MyVars


Private Sub Command1_Click()
'works!!
MV.MyVar1 = "1234567890123456789"
MV.MyVar2 = 99
'works!!
MV.MyVar3 = Array("zero", "one", "two", "three", "four")
MV.MyVar4 = -12345
MV.MyVar5 = True
MV.MyVar6 = Now

Open "c:\testfile" For Binary As #1
Put #1, , MV
Close #1
End Sub

Private Sub Command2_Click()
Open "c:\testfile" For Binary As #1
Get #1, , MV
Close #1
MsgBox MV.MyVar1 & MV.MyVar2 & MV.MyVar3(2) & MV.MyVar4 & MV.MyVar5 & MV.MyVar6
End Sub

Cheers
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2695354
Ark:
Here is the problem..

Open "c:\testfile" For Random As #1 Len = Len(MV)
Get #1, , MV

When you close the program and then start it again, when you go to open the file, how will the program open statement know the length of MV? It is going to use the empty MV type size, and due to that, a data underrun could easily take place.

Now, I could be wrong, maybe it will size properly on the fly. But I wouldn't want to bet the house on that.
As such, I never use variant data types for Disk I/O.. so in that regard, I hardly qualify as an expert.. <smile>.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2695356
Ark:
Same problem applies for Binary, we don't know the size of MV before we read it.. <sigh>.
0
 
LVL 27

Accepted Solution

by:
Ark earned 100 total points
ID: 2695379
Private Sub Command2_Click()
MV.MyVar1 = 0
MV.MyVar2 = 0
MV.MyVar3 = 0
MV.MyVar4 = 0
MV.MyVar5 = 0
MV.MyVar6 = 0

Open "c:\testfile" For Binary As #1
Get #1, , MV
Close #1
MsgBox MV.MyVar1 & MV.MyVar2 & MV.MyVar3(2) & MV.MyVar4 & MV.MyVar5 & MV.MyVar6
End Sub

works OK. We don't need to know size of MV, only structure. Put statement place variable description (and size) for every variable in structure into file before this variable. Get statement read this description and fill variables properly.
Cheers
0
 

Author Comment

by:chen_meir
ID: 2696198
ark ,
thank you for the answers...
i will try your code and i hope it will work.
:-)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now