Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

saving varients to a file

Posted on 2000-04-07
12
Medium Priority
?
144 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 400 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

670 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