?
Solved

How save a number(decimal) in a file?

Posted on 2003-03-15
9
Medium Priority
?
173 Views
Last Modified: 2010-05-01
Hi,

I have the following problem:

I try to save a variable (single) to a file an my harddrive.
I have tried different methods and nothing worked.

so: for example:  I have an variable (dim getal as single).
The user can input (using the textbox (text1.text)) an decimal number.

for example:   getal=text1.text

I have tried to save this decimal using 2 methods:
the first thing i tried was with the:

         OPEN ****** FOR OUTPUT AS 1
         PRINT #1, ******
         CLOSE #1

The second thing i tried was:
         OPEN "**********" FOR RANDOM AS 1 LEN = 6
         PUT 1, 1, getal
         CLOSE 1

these two codes didn't save the numeric data how it should be.
when i tried the second thing: I entered the number 12.6
afterwards when i opened the file again, it was saved as 126 instead of 12.6

Can someone help me with this??  I'm a beginner in VB.
I try to make a simple cashregister.  So I should be able save variables containing decimals and close the program.  Then start the program again and get the proper variable from a saved file.
It should also be able to make calculations with that variable after it is opened again.

Thanx in advance


0
Comment
Question by:vincentregent
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 4

Expert Comment

by:iozturk
ID: 8144625
Try something like this

OPEN "**********" FOR RANDOM AS 1 LEN = 6
MyStr = Format(getal, "##,##0.00")    
PUT 1, 1, mystr
CLOSE 1
0
 
LVL 4

Accepted Solution

by:
iozturk earned 405 total points
ID: 8144645
It is better to save your data in a database. It would give you filexibility. when updating, searching your data if you have much data.
0
 
LVL 3

Expert Comment

by:PhiliP
ID: 8144785
Option Explicit
'==========================================================
'Demo for vincentregent at expertXchange
'16 maart 2002 by PhiliP
'Explains using the filesystemobject
'Explains why decimal are converted to whole numbers
'Start a vbProject(Standard EXE)
'Add a textbox
'Add a commandbutton
'Add this code to the form1 codewindow
'Safe your project and run
'==========================================================
Private Sub Command1_Click()
'Since the Open method is quite old Microsoft advices you to use the
'FileSystemObject
'This needs a reference set to Microsoft Scripting Runtime (scrun.dll)
'<Menu><Project><References>
Dim fso As Scripting.FileSystemObject
Dim fMap As Scripting.Folder
Dim fFile As Scripting.File
Dim fStream As Scripting.TextStream
Dim strFileName As String

Dim sngGetal As Single


On Error GoTo errHandler
'We dont want non numbers, so check and raise error if not a number
If Not IsNumeric(Text1.Text) Then
    'Remember always start your own errornumbers from vbObjectError and add at least 1000
    'Number above 0 are reserved for Microsoft
    Err.Raise vbObjectError + 1200, "Form1", "Not a number"
End If

'Lets create a file in the applicationpath
'First a name
strFileName = App.Path & "\Cashregister.txt"
'Now Create the scripting object
Set fso = New Scripting.FileSystemObject
'Let's clean up before creating
If fso.FileExists(strFileName) Then
    fso.DeleteFile strFileName, True
End If

'Now create the file
Call fso.CreateTextFile(strFileName, True)
'Get a reference to the file
Set fFile = fso.GetFile(strFileName)
'Open the text stream for writing
Set fStream = fFile.OpenAsTextStream(ForAppending, TristateFalse)

'Convert text1 to the strong type

sngGetal = CSng(Text1.Text)
'This is the part where it's getting messy
'The number is formatted using the local settings for numbers
'My regional setting are dutch so the decimal operator = , (12,45)
'The tristate grouping = . (1.000,23)
'So if using . as decimal seperator it's discarded when you write

'Write the values you like
fStream.WriteLine sngGetal
'Close you Stream
fStream.Close

'Clean up your open objects and release the alocated memory
Set fFile = Nothing
Set fso = Nothing

'Now let's open the file in a rich text box
'To execute this you need a second form and add the Microsoft RichTextBox control to your toolbox
'Add form2
'set form1 to be the startup form
'Get the rtf
'<Menu><Project><Components><Microsoft Rich Textbox Control>

Form2.RichTextBox1.LoadFile strFileName, rtfText
Form2.Show vbModal

Exit Sub

errHandler:

    MsgBox "An error has occured: " & Err.Description, vbOKOnly

End Sub

'HAve fun, PhiliP

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.

 

Expert Comment

by:junkeira
ID: 8145180
Get started with this:

in a form, command1 writes the number and command2 reads it. (number is a variable)

Private Sub Command1_Click()
    Open "C:\temp\test.txt" For Output As 1
    Print #1, Text1.Text
    Close
End Sub

Private Sub Command2_Click()
    Open "C:\temp\test.txt" For Input As 1
    Line Input #1, Number
    MsgBox Number
End Sub
0
 

Author Comment

by:vincentregent
ID: 8152569
Hi Jozturk,

Finally, I did it without using a database, because otherwise, I would have to change a lot of codes.  But I'm sure this is the best advice.  I ll consider using databases in the future to solve these kinds of problems.

Also thanks to everyone who answered my question.

So, I finally solved the problem thanks to you all!

Greetings,

Vincent
0
 
LVL 3

Expert Comment

by:PhiliP
ID: 8156032
OK it's a good advice, but it isn't the answer to your question. Please judge more carefully.
0
 

Expert Comment

by:junkeira
ID: 8156172
I agree with PhiliP
0
 
LVL 3

Expert Comment

by:PhiliP
ID: 8156251
OK it's a good advice, but it isn't the answer to your question. Please judge more carefully.
0
 
LVL 3

Expert Comment

by:PhiliP
ID: 8156258
sorry for that last refresh
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

621 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