• We've upgraded our forums. Please post any issues/requests in this thread.

Visual Basic

Joined
Dec 22, 2007
Messages
184 (0.05/day)
Likes
7
Location
Central Nebraska
System Name CORSAIR
Processor AMD Phenom 1090T x6 @3.2
Motherboard Gigabyte Ga-78LMT-S2P
Cooling High efficiency dust cooling
Memory 8GB GSkill DDR3 1333
Video Card(s) Sapphire Radeon HD3870 512MB GDDR4 PCI-e toxic
Storage Seagate SV35.3 ST3250310SV 250GB SATAII(Windows 7 Pro x64), 320GB Samsung SATAII(Storage and SuSE)
Display(s) Dell 20" LCD, Dell 17" LCD
Power Supply Antec Cool Blue 650W Modular
Software Windows 7 Professional SP1, Visual Studio 2010 Ultimate
#1
Me and a buddy have an assignment to write a program in visual basic that allows you to calculate interest, etc. on a loan. We've been sitting for hours tryign to figure out streamwriter because the program must also be able to save the input to the text boxes used to calculate the loan info to a text file.
If anyone could tell us the error of our ways, it would be much appreciated

Imports System.IO
Public Class Console_MAIN

Private Sub Console_MAIN_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
'closes the program
Me.Close()
End Sub

Private Sub Button_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Exit.Click
'closes the program
Me.Close()
End Sub

Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
AboutBox1.Show()
End Sub

Private Sub Button_UpdateSchedule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_UpdateSchedule.Click
Dim VAR_LoanAmount As Decimal
Dim VAR_LoanRate As String
Dim VAR_LoanPeriod As Decimal
Dim LoanInfo As StreamWriter

VAR_LoanAmount = TextBox_LoanAmount.Text
VAR_LoanRate = ComboBox_AnnualRate.Text
VAR_LoanPeriod = TextBox_Years.Text
LoanInfo = New IO.StreamWriter("loaninfo.txt", True)

Try
LoanInfo.WriteLine(Now())
LoanInfo.Write(VAR_LoanAmount, VAR_LoanRate, VAR_LoanPeriod)
LoanInfo.WriteLine()
LoanInfo.WriteLine()
Catch
MessageBox.Show("Error writing file")
End Try
End Sub
End Class

This has resulted in a blank text file, and if you click the button again, it crashes the program. Please help!
 
Joined
Jul 26, 2010
Messages
1,655 (0.61/day)
Likes
729
Location
Philly
System Name Primary Rig
Processor Phenom II X4 B50 @ 3.7GHz
Motherboard Biostar TA790GX 128M
Cooling Sunbeam CR-CCTF 120mm , 6x120mm, MOS-C1
Memory 2x2GB Kingston HyperX 1066 @ 800 4-4-4-12
Video Card(s) Sapphire HD 5830 800/1000 @ 885/1225
Storage 320GB, 400GB, 500GB, 1.5TB
Display(s) Hannspree HF259
Case CM 690
Power Supply OCZ 850W
Benchmark Scores 3Dmark06: 18545/5219 CPU Mark 7.0: 3911.2 Cinebench R10: 11826/3359 x264 HD 2.0: 75.6/23.9
#2
It's been a couple years since I did anything in VB but I'd try a couple things.

You'll definitely want to add this line below your last "WriteLine()" statement:
Code:
LoanInfo.[url="http://msdn.microsoft.com/en-us/library/system.io.streamwriter.flush(VS.80).aspx"]Flush[/url]()
And/Or you can try this:
http://msdn.microsoft.com/en-us/library/hxwfzt61(v=VS.90).aspx

Either way about it you should remember to close the file
 
Last edited:
Joined
Jan 9, 2010
Messages
481 (0.17/day)
Likes
117
Location
Kansas
System Name Late 2013 rMBP 13'' w/ 250 GB SSD
Display(s) Dell P2416D @ 2560x1440 & Dell U2211H @ 1920x1080
Audio Device(s) NuForce uDAC-2 w/ Klipsch Promedia 2.1 & Sennheiser HD595
Mouse Logitech G400 @ 1600 DPI
Keyboard Razr Black Widow
Software OS X
#3
shuggans, I believe you meant to say the program must also be able to save the input to from the text boxes used to calculate the loan info and write to a text file


I would avoid using the IO.StreamWriter class and use the StreamWriter class like below. Make sure you have Imports System.IO at the top of all of your code.

try this -

Code:
    Public Sub WriteSerials()
        Using Writer As New StreamWriter(selectedSystem + ".txt", False)
            For k = 0 To lsbDisplay.Items.Count - 1
                Writer.Write(systemSerials(k).systemSerial + ",")
                Writer.Write(systemSerials(k).boughtFrom + ",")
                Writer.Write(systemSerials(k).tradeDate + ",")
                Writer.Write(systemSerials(k).employee + ",")
                Writer.WriteLine()
            Next
            Writer.Write(systemSerial + ",")
            Writer.Write(boughtFrom + ",")
            Writer.Write(Now.Date + ",")
            Writer.Write(employee + ",")
        End Using
        '  Array.Clear(systemSerials, 0, systemSerials.Length)
    End Sub
This is code from a program I wrote but I know this method works. When you create your Streamwriter object put the location of your txt file you want to write to.

Basically try this -

Code:
Using Writer As New Streamwriter("C:\nameoftext.txt", False)

End Using
Or try this -

Code:
Using Writer As New Streamwriter("C:\nameoftext.txt", True)

End Using
 
Last edited:

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
20,906 (6.25/day)
Likes
10,000
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#4
Code:
    Private Sub Button_UpdateSchedule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_UpdateSchedule.Click
        Dim VAR_LoanAmount As Decimal
        Dim VAR_LoanRate As String
        Dim VAR_LoanPeriod As Decimal
        Dim LoanInfo As StreamWriter

        VAR_LoanAmount = TextBox_LoanAmount.Text
        VAR_LoanRate = ComboBox_AnnualRate.Text
        VAR_LoanPeriod = TextBox_Years.Text
        LoanInfo = New IO.StreamWriter("loaninfo.txt", True)

        Try
            LoanInfo.WriteLine(Now())
            LoanInfo.Write(VAR_LoanAmount, VAR_LoanRate, VAR_LoanPeriod)
            LoanInfo.WriteLine()
            LoanInfo.WriteLine()
        Catch
            MessageBox.Show("Error writing file")
        End Try
    End Sub
There's a few problems here. First, a StreamWriter handles only strings so it doesn't make sense to convert everything to a different type and then back to string again.

Second, as streetfighter 2 pointed out, you need to Flush() it (commits the data in the buffer to the hard drive).

Thirdly, after you are done with a file, you must Close() it (ending the stream).

Fourthly, there is no StreamWriter.Write() method that accepts Write(decimal, string, decimal) for arguments.

Code with those changes implemented:
Code:
    Private Sub Button_UpdateSchedule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_UpdateSchedule.Click
        Dim LoanInfo As StreamWriter = New IO.StreamWriter("loaninfo.txt", True) ' Declare and instaniate on one line
        Try
            LoanInfo.WriteLine(Now())
            LoanInfo.WriteLine(TextBox_LoanAmount.Text)
            LoanInfo.WriteLine(VAR_LoanRate.Text)
            LoanInfo.WriteLine(VAR_LoanPeriod.Text)
            LoanInfo.Flush() ' Commit changes to file.
        Catch
            MessageBox.Show("Error writing file")
        End Try
        LoanInfo.Close() ' Free the file stream.
    End Sub


I would avoid using the IO.StreamWriter class and use the StreamWriter class like below. Make sure you have Imports System.IO at the top of all of your code.
They are the same thing. How you reference is programmer preference. I tend to do a lot of importing while others may not.


Basically try this -

Code:
Using Writer As New Streamwriter("C:\nameoftext.txt", False)

End Using
Or try this -

Code:
Using Writer As New Streamwriter("C:\nameoftext.txt", True)

End Using
The boolean value at the end is for appending (true/false). Default is false (zero-lengths the file when the stream is opened).
 
Last edited:
Joined
Dec 22, 2007
Messages
184 (0.05/day)
Likes
7
Location
Central Nebraska
System Name CORSAIR
Processor AMD Phenom 1090T x6 @3.2
Motherboard Gigabyte Ga-78LMT-S2P
Cooling High efficiency dust cooling
Memory 8GB GSkill DDR3 1333
Video Card(s) Sapphire Radeon HD3870 512MB GDDR4 PCI-e toxic
Storage Seagate SV35.3 ST3250310SV 250GB SATAII(Windows 7 Pro x64), 320GB Samsung SATAII(Storage and SuSE)
Display(s) Dell 20" LCD, Dell 17" LCD
Power Supply Antec Cool Blue 650W Modular
Software Windows 7 Professional SP1, Visual Studio 2010 Ultimate
#5
There's a few problems here. First, a StreamWriter handles only strings so it doesn't make sense to convert everything to a different type and then back to string again.

Second, as streetfighter 2 pointed out, you need to Flush() it (commits the data in the buffer to the hard drive).

Thirdly, after you are done with a file, you must Close() it (ending the stream).

Fourthly, there is no StreamWriter.Write() method that accepts Write(decimal, string, decimal) for arguments.

Code with those changes implemented:
Code:
    Private Sub Button_UpdateSchedule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_UpdateSchedule.Click
        Dim LoanInfo As StreamWriter = New IO.StreamWriter("loaninfo.txt", True) ' Declare and instaniate on one line
        Try
            LoanInfo.WriteLine(Now())
            LoanInfo.WriteLine(TextBox_LoanAmount.Text)
            LoanInfo.WriteLine(VAR_LoanRate.Text)
            LoanInfo.WriteLine(VAR_LoanPeriod.Text)
            LoanInfo.Flush() ' Commit changes to file.
        Catch
            MessageBox.Show("Error writing file")
        End Try
        LoanInfo.Close() ' Free the file stream.
    End Sub



They are the same thing. How you reference is programmer preference. I tend to do a lot of importing while others may not.



The boolean value at the end is for appending (true/false). Default is false (zero-lengths the file when the stream is opened).
This worked great! Thanks so much!