Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Saving List Items to an Access Database

$
0
0

This is the full code as it stands, I have no idea what to do next, or whats wrong. I've spend all day on just trying to get the listbox to populate the database to no avail. The pertinent section is bolded

Option Explicit On
Option Strict On
Option Infer Off



Public Class frmMain



    Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles m3.Click
        'Adds the numbers together and the tag remembers the operator



        If NumOne.Text = ("") Then
            MessageBox.Show("Please enter the first number")
            NumOne.Focus()
        ElseIf NumTwo.Text = ("") Then
            MessageBox.Show("Please enter the second number")
            NumTwo.Focus()
        End If

        Dim m As New MathOp2()
        Dim r As Integer

        Try

            r = CInt(CStr(m.Add(CInt(NumOne.Text), CInt(NumTwo.Text))))

            Result.Text = CStr(r)

            ListBox1.Tag = "+"

        Catch ex As Exception

        End Try




    End Sub

    Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
        'Subtracts the numbers



        If NumOne.Text = ("") Then
            MessageBox.Show("Please enter the first number")
            NumOne.Focus()
        ElseIf NumTwo.Text = ("") Then
            MessageBox.Show("Please enter the second number")
            NumTwo.Focus()
        End If

        Dim m As New MathOp2()
        Dim r As Integer

        Try
            r = CInt(CStr(m.Subtract(CInt(NumOne.Text), CInt(NumTwo.Text))))

            Result.Text = CStr(r)

            ListBox1.Tag = "-"
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
        'Multiplies the numbers


        If NumOne.Text = ("") Then
            MessageBox.Show("Please enter the first number")
            NumOne.Focus()
        ElseIf NumTwo.Text = ("") Then
            MessageBox.Show("Please enter the second number")
            NumTwo.Focus()
        End If

        Dim m As New MathOp2()
        Dim r As Integer

        Try
            r = CInt(CStr(m.Mul(CInt(NumOne.Text), CInt(NumTwo.Text))))

            Result.Text = CStr(r)

            ListBox1.Tag = "x"

        Catch ex As Exception

        End Try
    End Sub

    Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
        'Divides the numbers, with a divide by 0 exception


        If NumOne.Text = ("") Then
            MessageBox.Show("Please enter the first number")
            NumOne.Focus()
        ElseIf NumTwo.Text = ("") Then
            MessageBox.Show("Please enter the second number")
            NumTwo.Focus()
        End If

        Dim m As New MathOp2()
        Dim r As Integer

        Try
            r = CInt(CStr(m.Div(CInt(NumOne.Text), CInt(NumTwo.Text))))

            Result.Text = CStr(r)

            ListBox1.Tag = "/"

        Catch ex As Exception

        End Try

        'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN

        Try

            If CDbl(NumTwo.Text) = Val(0) Then
                Result.Text = ""
                MessageBox.Show("You cannot divide by 0, please input another number")
                NumTwo.Focus()
            End If

        Catch ex As Exception


        End Try

    End Sub

    Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
        'Simply closes the form
        Me.Close()
    End Sub

    Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click

        If NumOne.Text = ("") Then
            MessageBox.Show("Please enter some numbers")
            NumOne.Focus()
        ElseIf NumTwo.Text = ("") Then
            MessageBox.Show("Please enter another number")
            NumTwo.Focus()
        End If

        Try
            If ListBox1.Items.Count < 10 Then
                ListBox1.Items.Add(NumOne.Text & ListBox1.Tag.ToString() & NumTwo.Text & ("=") & Result.Text)
            End If
        Catch ex As Exception


        End Try
    End Sub


    Private Sub ButtonClear_Click(sender As Object, e As EventArgs) Handles ButtonClear.Click
        ListBox1.Items.Clear()
    End Sub


    Private Sub ButtonReset_Click_1(sender As Object, e As EventArgs) Handles ButtonReset.Click
        NumOne.Text = ""
        NumTwo.Text = ""
        Result.Text = ""
    End Sub



    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       
        Dim toolTip1, toolTip2, toolTip3, toolTip4, toolTip5, toolTip6, toolTip7, toolTip8 As New ToolTip()

        toolTip1.SetToolTip(Me.m3, "Addition")
        toolTip2.SetToolTip(Me.ButtonSub, "Subtraction")
        toolTip3.SetToolTip(Me.ButtonMul, "Multiplication")
        toolTip4.SetToolTip(Me.ButtonDiv, "Division")
        toolTip5.SetToolTip(Me.ButtonSave, "Save operation")
        toolTip6.SetToolTip(Me.ButtonReset, "Reset operation")
        toolTip7.SetToolTip(Me.ButtonExit, "Exit application")
        toolTip8.SetToolTip(Me.ButtonClear, "Clear list")

    End Sub



    Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick


        'This is part of the second assignment, double click on list items and they show up in the text boxes, still working on the other part

        If ListBox1.SelectedIndex = -1 Then
            Return
        End If

        'Get the text of the selected item
        Dim selectedtext As String = ListBox1.Items(ListBox1.SelectedIndex).ToString()

        'Split the item by the operator and the = into an array of strings
        Dim parts As String() = selectedtext.Split("+"c, "-"c, "x"c, "/"c, "="c)

        If parts.Length > 2 Then
            'Define a variable for each part
            Dim part1 As String = parts(0).Trim()
            Dim part2 As String = parts(1).Trim()

            NumOne.Text = part1
            NumTwo.Text = part2

            'Make text boxes set to part1 and part2. part1 = 1, part2 = 1
        End If

    End Sub

    Private Sub ButtonSaveDB_Click(sender As Object, e As EventArgs) Handles ButtonSaveDB.Click
        Dim con As New OleDb.OleDbConnection
        Dim dbProvider As String
        Dim dbSource As String

        dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0"
        dbSource = "Data Source=|DataDirectory|bin\debug\CalcDB.accdb"

        con.ConnectionString = dbProvider & dbSource


        Dim arr As New List(Of String)
        Dim cmdText As String = "INSERT INTO CalcOps (NumOne, Operator, NumTwo, Result)"
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(cmdText)
        cmd.CommandType = CommandType.Text

        con.Open()

        With cmd.Parameters
            .Add("@NumOne", OleDb.OleDbType.VarChar).Value = NumOne.Text


        End With




    End Sub

    Private Sub ButtonRet_Click(sender As Object, e As EventArgs) Handles ButtonRet.Click

    End Sub

    Private Sub CalcOpsBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Me.Validate()
        Me.CalcOpsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.CalcDBDataSet)

    End Sub
End Class




Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>