Search code examples
vb.netvisual-studioms-accessparametersoledb

Parameter @Something Has No Default Value (Access Database, Visual Studio VB.NET)


I am using an Access Database to store records of occurrences of errors to track their quantity and frequency. Thanks to some great help from some gifted programmers on this site, I am now getting closer to my end goal of pulling the occurrence counts back out of the database, and into charts in an Visual Studio program.

The problem I'm running into is that there is an error being thrown at this line in my function:

count = command.ExecuteScalar()

The error is as follows:

System.Data.OleDb.OleDbException: 'Parameter @MY_OF_LOG has no default value.'

I am successfully returning a value for count, which is 0. This is a possible value, but I am now wondering if the fact that the count returns as zero is what might be partially contributing to this error?

Note: the values that will be returned from these counts for each month will be used as y values for a chart.

Does anyone have any ideas on why this error occurred? I will insert a snippet of my code below. Thanks for the help.

Private Function CountMissedParts() Handles MyBase.Load

    Dim sql = $"SELECT COUNT(*)
        FROM DataCollection
        WHERE [MISSED PART] = 'Missed Part'
        AND [M/Y OF LOG] = @MY_OF_LOG;"
    Dim JANmyOfLog = #1/1/2021#
    Dim FEBmyOfLog = #2/1/2021#
    Dim MARmyOfLog = #3/1/2021#
    Dim APRmyOfLog = #4/1/2021#
    Dim MAYmyOfLog = #5/1/2021#
    Dim JUNmyOfLog = #6/1/2021#
    Dim JULmyOfLog = #7/1/2021#
    Dim AUGmyOfLog = #8/1/2021#
    Dim SEPmyOfLog = #9/1/2021#
    Dim OCTmyOfLog = #10/1/2021#
    Dim NOVmyOfLog = #11/1/2021#
    Dim DECmyOfLog = #12/1/2021#
    Dim count As Integer
    Dim JanuaryMP As Double
    Dim FebruaryMP As Double
    Dim MarchMP As Double
    Dim AprilMP As Double
    Dim MayMP As Double
    Dim JuneMP As Double
    Dim JulyMP As Double
    Dim AugustMP As Double
    Dim SeptemberMP As Double
    Dim OctoberMP As Double
    Dim NovemberMP As Double
    Dim DecemberMP As Double

    Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database1.accdb"),
            command As New OleDbCommand(sql, connection)
        JanuaryMP = command.Parameters.Add("@MY_OF_LOG", OleDbType.Date).Value = JANmyOfLog
        FebruaryMP = command.Parameters.Add("@MY_OF_LOG", OleDbType.Date).Value = FEBmyOfLog
        MarchMP = command.Parameters.Add("@MY_OF_LOG", OleDbType.Date).Value = MARmyOfLog
        AprilMP = command.Parameters.Add("@MY_OF_LOG", OleDbType.Date).Value = APRmyOfLog
        MayMP = command.Parameters.Add("@MY_OF_LOG", OleDbType.Date).Value = MAYmyOfLog
        JuneMP = command.Parameters.Add("@MY_OF_LOG", OleDbType.Date).Value = JUNmyOfLog
        JulyMP = command.Parameters.Add("@MY_OF_LOG", OleDbType.Date).Value = JULmyOfLog
        AugustMP = command.Parameters.Add("@MY_OF_LOG", OleDbType.Date).Value = AUGmyOfLog
        SeptemberMP = command.Parameters.Add("@MY_OF_LOG", OleDbType.Date).Value = SEPmyOfLog
        OctoberMP = command.Parameters.Add("@MY_OF_LOG", OleDbType.Date).Value = OCTmyOfLog
        NovemberMP = command.Parameters.Add("@MY_OF_LOG", OleDbType.Date).Value = NOVmyOfLog
        DecemberMP = command.Parameters.Add("@MY_OF_LOG", OleDbType.Date).Value = DECmyOfLog
        connection.Open()
        count = command.ExecuteScalar()

        ' set 0,0
        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(0, 0)
        ' other points
        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(1, JanuaryMP)

        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(2, FebruaryMP)

        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(3, MarchMP)

        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(4, AprilMP)

        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(5, MayMP)

        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(6, JuneMP)

        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(7, JulyMP)

        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(8, AugustMP)

        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(9, SeptemberMP)

        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(10, OctoberMP)

        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(11, NovemberMP)

        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(12, DecemberMP)

    End Using
    NotInEpicorCHRT.ChartAreas(0).AxisX.Minimum = 0.0
    NotInEpicorCHRT.ChartAreas(0).AxisX.Maximum = 12
    NotInEpicorCHRT.ChartAreas(0).AxisX.Interval = 1
    NotInEpicorCHRT.ChartAreas(0).AxisY.Minimum = 0.0
    NotInEpicorCHRT.ChartAreas(0).AxisY.Maximum = 45
    NotInEpicorCHRT.ChartAreas(0).AxisY.Interval = 5

End Function

Solution

  • Since you already accepted an answer, I assumed your problem was solved. My function would look like this.

    Private Function CountMissedParts() As DataTable
        Dim sql = "SELECT [M/Y OF LOG], COUNT([Missed Part]) As Total 
                    FROM [Data Collection] 
                    WHERE [MISSED PART] = 'MISSED PART' GROUP BY [M/Y OF LOG]  
                    ORDER BY [M/Y OF LOG];"
        Using dt As New DataTable
            Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database1.accdb"),
            command As New OleDbCommand(sql, connection)
                connection.Open()
                Using reader = command.ExecuteReader
                    dt.Load(reader)
                End Using
            End Using
            Return dt
        End Using
    End Function
    

    Usage.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim dt = CountMissedParts()
        'Add a grid to your form just to see what data was returned
        'If all is well delete grid and the following line
        DataGridView1.DataSource = dt
        'To show you how to access the data
        'Inside the For loop is where you would put the code to fill you graph
        For Each row As DataRow In dt.Rows
            Debug.Print($"{row("M/Y OF LOG")}  -  {row("Total")}")
        Next
    End Sub
    

    BTW, if the previous answer didn't work, you can click the check again and choose another answer.