Solved

SQL Query Syntax

Posted on 2011-09-09
4
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 93

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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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.
Suggested Courses

626 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