Question
I am currently made a button to save content of the listbox to a access database and a button to retrieve the content of the
I am currently made a button to save content of the listbox to a access database and a button to retrieve the content of the table into the listbox.
The button to save the table is btnSaveTbl
The button to retrieve the cotton is btnRetrieve
The listbox is named 1st operations
I wanted to get it looked over I am having issues of both buttons erroring out.
May someone look over the code? And fix it?
Thanks
Calculator.vb
Imports System.Data.OleDb
Public Class Calculator
Dim conn As New OleDbConnection
Dim command As New OleDbCommand
Dim dr As OleDbDataReader
Dim sql As String
Private Sub Calculator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\TimothySwaney\source epos\Assignment one\Assignment one\Calculator.accdb"
End Sub
Dim decNum1 As Decimal
Dim decNum2 As Decimal
Dim decNum3 As Decimal
Dim count As Integer = 0
Dim strOperation As String ' Indicates the last operation (+,-,* or /) that was used. Appended to file when 'Save' is clicked
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Decimal.TryParse(txtNum1.Text, decNum1)
Decimal.TryParse(txtNum2.Text, decNum2)
'Declares the obj to call from the MathOp class then uses the add subprocedure
Dim objCalc As New MathOp(decNum1, decNum2)
decNum3 = objCalc.Add()
txtNum3.Text = decNum1 & " + " & decNum2 & " = " & decNum3
strOperation = "+"
End Sub
Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
Decimal.TryParse(txtNum1.Text, decNum1)
Decimal.TryParse(txtNum2.Text, decNum2)
'Declares the obj to call from the MathOp class then uses the add subprocedure
Dim objCalc As New MathOp(decNum1, decNum2)
decNum3 = objCalc.Subtract()
txtNum3.Text = decNum1 & " - " & decNum2 & " = " & decNum3
strOperation = "-"
End Sub
Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
Decimal.TryParse(txtNum1.Text, decNum1)
Decimal.TryParse(txtNum2.Text, decNum2)
' 'Declares the obj to call from the MathOp class then uses the add subprocedure
Dim objCalc As New MathOp2(decNum1, decNum2)
decNum3 = objCalc.Multiply()
txtNum3.Text = decNum1 & " X " & decNum2 & " = " & decNum3
strOperation = "*"
End Sub
Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
Decimal.TryParse(txtNum1.Text, decNum1)
Decimal.TryParse(txtNum2.Text, decNum2)
' divide num 1 by num 2 and display results
Try
Dim objCalc As New MathOp2(decNum1, decNum2)
decNum3 = objCalc.Divide()
Catch ex As DivideByZeroException When decNum2 = 0
MessageBox.Show("You Can't divide by 0. Please re-enter another value.")
txtNum2.Focus()
strOperation = String.Empty
Return
End Try
txtNum3.Text = decNum1 & " / " & decNum2 & " = " & decNum3
strOperation = "/"
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
lstOperations.Items.Clear()
My.Computer.FileSystem.WriteAllText("operations.txt", "", False)
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
'Declared an array to hold each part of the operation. Last week I only had it displaying the answer.
Dim strOutput(3) As String
strOutput(0) = CStr(decNum1)
strOutput(1) = CStr(decNum2)
strOutput(2) = CStr(decNum3)
If (count < 10) Then
Dim outFile As IO.StreamWriter
outFile = IO.File.AppendText("operations.txt")
outFile.WriteLine(strOutput(0) & strOperation & strOutput(1) & "=" & strOutput(2))
outFile.Close()
count = count + 1
End If
End Sub
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim inFile As IO.StreamReader
Dim strOperation As String
lstOperations.Items.Clear()
If IO.File.Exists("operations.txt") Then
inFile = IO.File.OpenText("operations.txt")
Do Until inFile.Peek = -1
strOperation = inFile.ReadLine
lstOperations.Items.Add(strOperation)
Loop
inFile.Close()
Else
MessageBox.Show("Please Save to Display.")
End If
End Sub
Private Sub input_validation(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles txtNum1.KeyPress, txtNum2.KeyPress
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
Private Sub lstOperations_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstOperations.SelectedIndexChanged
'Allows you to select a previous entry.
If lstOperations.SelectedIndex = -1 Then Return
Dim strline As String = lstOperations.SelectedItem
'Array stores the different section of the string to split up.
Dim strsection As String() = strline.Split(New [Char]() {"+"c, "-"c, "*"c, "/"c, "="c})
If strsection.Length >= 3 Then
txtNum1.Text = strsection(0)
txtNum2.Text = strsection(1)
txtNum3.Clear()
End If
End Sub
Private Sub btnSaveTbl_Click(sender As Object, e As EventArgs) Handles btnSaveTbl.Click
Dim conn As New OleDbConnection()
Dim ds As New DataSet
Dim dt As New OleDbCommand
Dim cmd As New OleDbCommand
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\TimothySwaney\source epos\Assignment one\Assignment one\Calculator.accdb"
conn.Open()
cmd = New OleDbCommand(" insert values(" & txtNum1.Text & "," & txtNum2.Text & ")", conn)
cmd.ExecuteNonQuery()
MessageBox.Show("DATA SAVED")
End Sub
Private Sub btnRetrieve_Click(sender As Object, e As EventArgs) Handles btnRetrieve.Click
Try
Command = New OleDbCommand("select * from number", conn)
conn.Open()
dr = command.ExecuteReader()
While dr.Read()
sql = dr("NO1").ToString() + " " + dr("NO2").ToString()
lstOperations.Items.Add(sql)
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Close()
End Try
End Sub
End Class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started