Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Query Syntax

Posted on 2011-09-09
4
Medium Priority
?
306 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

722 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