Solved

saving varients to a file

Posted on 2000-04-07
12
141 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
[X]
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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

726 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