Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

saving varients to a file

Posted on 2000-04-07
12
140 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 28

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 28

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
 
LVL 28

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 28

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 28

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

856 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