Solved

SQL Query Syntax

Posted on 2011-09-09
4
299 Views
Last Modified: 2012-05-12
The following works, returns the correct count:
           Dim BsWt_n1 As Integer
            Dim conn1 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Process1\db\Main Application.mdb")
            Dim qry1 As String
            qry1 = "SELECT COUNT(BsWtAvg) FROM tblProd2"
            Dim cmd1 As New OleDb.OleDbCommand(qry1, conn1)
            conn1.Open()
            BsWt_n1=cmd1.ExecuteScalar()
            conn1.Close()
            TextBox1.Text = BsWt_n1

The following does not work, returns a count of 0:
            Dim BsWt_n1 As Integer
            Dim conn1 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Process1\db\Main Application.mdb")
            Dim qry1 As String
            qry1 = "SELECT COUNT(BsWtAvg) AS [BsWt_n1] FROM tblProd2"
            Dim cmd1 As New OleDb.OleDbCommand(qry1, conn1)
            conn1.Open()
            cmd1.ExecuteScalar()
            conn1.Close()
            TextBox1.Text = BsWt_n1

Since I need to include several other aggregate functions (Avg, StDev, Min, Max, etc.) I need to find out how the latter example needs to be constructed correctly to allow each result to be assigned to a variable.

Can someone provide an example?
0
Comment
Question by:Tim313
  • 3
4 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36509927
In your second example, shouldn't:

           cmd1.ExecuteScalar()

be:

           BsWt_n1=cmd1.ExecuteScalar()

?  In that second example, looks to me like you never actually populated the variable, and so it took the intiialized value of 0.
0
 

Author Comment

by:Tim313
ID: 36510141
Possibly, but how would I construct it if I were to have the following statement?:

qry1 = "SELECT COUNT(BsWtAvg) AS [BsWt_n1], AVG(BsWtAvg) AS [AvgBsWt], StDev(BsWtAvg) AS [DevBsWtAvg] FROM tblProd2"

0
 

Accepted Solution

by:
Tim313 earned 0 total points
ID: 36510753
Got the results needed by using a dataset.
0
 

Author Closing Comment

by:Tim313
ID: 36534677
Found correct method to do what I needed.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question