?
Solved

How save a number(decimal) in a file?

Posted on 2003-03-15
9
Medium Priority
?
172 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
[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
  • 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
Technology Partners: 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!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

764 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