How to Correctly Define the CommandText and Parameters for an OleDb command Returning a Single Value

Hello Again Experts.

I am trying to return a single value from a data table using the following code:
    Private Sub Calc_PAT()
        ' Calculate and Update Total/Match Points and Running Totals after each Match
        '
        ' Note that table Scoring has been replaced with a simple String array (SL_P2W), and
        ' a new table called Scoring which contains what is needed for calculating Match Points.
        '
        ' The following SQL command is an example of a query that would return the number of
        ' Match Points given to the loser with a Skill Level of 3, and their Total Points was 20:
        ' SELECT DISTINCT SL3 FROM Scoring GROUP BY LS, SL3 HAVING LS = 20;
        Dim command_builder As New OleDbCommandBuilder(OleDbDataAdapterMatches)
        Dim LsrMP As Int32 = 0, WnrMP As Int32 = 0, LsrNo As Int32 = IIf(TN = 1, 2, 1)
        Dim LoserSL As Int32 = CInt(PT(SK, LsrNo).Text)
        Dim SL_Clmn As String = "SL" & CStr(LoserSL)
        Dim LoserTP As Int32 = CInt(PT(MP, LsrNo).Text)
        Using con As New OleDbConnection(OleDbConnection.ConnectionString)
            con.Open()
            Using cmd As New OleDbCommand
                cmd.Connection = con
                cmd.CommandText = "SELECT DISTINCT ? FROM Scoring GROUP BY ?, " _
                  & SL_Clmn & " HAVING LS = " & PT(MP, LsrNo).Text & ";"
                cmd.Parameters.Add(New OleDbParameter("@Param1", SL_Clmn))
                cmd.Parameters.Add(New OleDbParameter("@Param2", LoserTP))
                Try
                    LsrMP = cmd.ExecuteScalar()
                Catch ex As OleDbException
                    MessageBox.Show(ex.Message)
                End Try
            End Using
        End Using
        WnrMP = 20 - LsrMP
        PT(MP, TN).Text = CStr(WnrMP)
        PT(MP, LsrNo).Text = CStr(LsrMP)
        ' Adjust Running Totals - Make sure we are updating the correct team
        If PT(NO, TN).Text = PT(TN, P1).Text Then
            PT(RT, P1).Text = CInt(PT(RT, P1).Text) + WnrMP
            PT(RT, P2).Text = CInt(PT(RT, P2).Text) + LsrMP
        Else
            PT(RT, P1).Text = CInt(PT(RT, P1).Text) + LsrMP
            PT(RT, P2).Text = CInt(PT(RT, P2).Text) + WnrMP
        End If
    End Sub

Open in new window


The error being returned is
You tried to execute a query  that does not include the specified expression 'LS=0' as part of an aggregate function.

Note that there is an example of a known working SQL command in the comments of the Calc_PAT subroutine.

I guess I just need to figure out how to correctly define the CommandText and Parameters.

Any thoughts?
Tony GardnerSr. Programmer/AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You don't need DISTINCT with Group By (they're distinct by default). Group By is used when aggregating data, but you're not doing that here.

cmd.CommandText = "SELECT DISTINCT ? FROM Scoring WHERE LS = " & PT(MP, LsrNo).Text
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tony GardnerSr. Programmer/AnalystAuthor Commented:
Thanks so much for the help, Scott. Your answer was dead on correct.

I'm moving on to the next challenge!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.