Question
HELP!! Please look at my code and the instructions. I am having a hard time saving and retrieving a table from access. My listbox is
HELP!!
Please look at my code and the instructions.
I am having a hard time saving and retrieving a table from access. My listbox is named 1stoperations, button is named btnretrieve, and the button to save the table is btnSaveTbl. SO please read.
You are going to save and retrieve the operations developed from the prior assignment in Ms. Access database, create the necessary table to save and retrieved operations Details - Create a table to save the operations - Create the necessary fields for the table - Create a button to save the content of the listbox to the database - Create a button to retrieve the content of the table into the listbox
Note* the table in the database should not exceed 10 records, when you do another save the table should be cleared and replaced with the new operations when select a line from the list box, the user should be able to modify the values/operations and save it to the listbox/database.
Calculator.VB
Imports System.Data.OleDb
Public Class Calculator
'con object
Dim con As New OleDbConnection
'command object
Dim command As New OleDbCommand
'dr object
Dim dr As OleDbDataReader
'sql string
Dim sql As String
'form load event
Private Sub Calculator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'setting connection string
'change this parameters
con.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
'using try catch block to handle the exception
Try
'command object is used to send the query to select the data from department table and con object
command = New OleDbCommand("select * from calculations", con)
'open the connection
con.Open()
'execute the command with ExecuteReader() method
dr = Command.ExecuteReader()
'read records one by one
While dr.Read()
'concatinate the values of NO1 and No2
sql = dr("NO1").ToString() + " " + dr("NO2").ToString() + " " + dr("ADD").ToString()
'add the Items in the listbox
lstOperations.Items.Add(Sql)
End While
Catch ex As Exception
'showing exception in messagebox
MessageBox.Show(ex.Message)
Finally
'close the connection
con.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