225 pts.
 VB6 with too many SQL Select Statements
Hi Team, hope you can help me out here.. I have a VB6 form that is populated using VB6 and Access being the Repository. The problem is that I am opening and closing the Database for every query I have.. Approx 87 queries. In short I have 12 Rooms that I need stats for.. Ie Grades & Size Volumes.. I need to combine these into less queries as it takes about 2 minutes to populate each field on the VB form. Extract of Query: Call dbConnect     SQL = "SELECT Sum(Currentweight) as GradePR1 FROM Weighroom WHERE (currentdate = DateValue(Now) and Roomno = '01' And Grade = 'PR')"     rs.Open SQL, Conn     If IsNull(rs(0)) Then         TxtGradePRoom(0) = 0         Else          TxtGradePRoom(0) = rs(0)         End If     Conn.Close Call dbConnect     SQL = "SELECT Sum(Currentweight) as GradePR1 FROM Weighroom WHERE (currentdate = DateValue(Now) and Roomno = '01' And Grade = 'AP')"     rs.Open SQL, Conn     If IsNull(rs(0)) Then         TxtGradePRoom(0) = 0         Else          TxtGradePRoom(0) = rs(0)         End If     Conn.Close If I simply do 1 select statement with a group by Grade, How do I extract each value. This is the Group by... BUT Call dbConnect     SQL = "SELECT Grade, Sum(Currentweight) as GradePR1 FROM Weighroom WHERE (currentdate = DateValue(Now) and Roomno = '01') Group by Grade"     rs.Open SQL, Conn     If IsNull(rs(0)) Then         TxtGradePRoom(0) = 0         Else          TxtGradePRoom(0) = rs(0)          TxtGradePRoom(1) = rs(1)         End If     Conn.Close The values in rs(0) is AA The Values in rs(1) is 436.38 These values are related and correct.. but im missing all the other values for AP, PR, CA. If I put a rs(2) or rs(-1) it fails.. I know they should be retrieved but I dont know how to access them. My DB call is:- Global Conn As New adodb.Connection, rs As New adodb.Recordset Global onTop As New clsOnTop, i As Integer, SQL As String, Cancel As Boolean Public Sub dbConnect()   Set Conn = New adodb.Connection   Conn.ConnectionString = strConn   Conn.Open End Sub Public Function strConn() As String      strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "Greenhillmushrooms.mdb" & ";Persist Security Info=False" End Function Regards Witsend

Software/Hardware used:
VB6 & Access97
ASKED: June 6, 2012  9:08 AM
UPDATED: June 7, 2012  2:29 PM
  Help
 Approved Answer - Chosen by carlosdl

I don't currently have a VB6 installation to test, but if I remember well, you can use the recordset's EOF property to control a loop to read all the rows in the recordset.

Something like this:

<pre>Call dbConnect
SQL = "SELECT Grade, Sum(Currentweight) as GradePR1 FROM Weighroom WHERE (currentdate = DateValue(Now) and Roomno = '01') Group by Grade"
rs.Open SQL, Conn
<b>if rs.BOF and rs.EOF then
'do something, the recordset is empty
else
rs.MoveFirst
Do Until rs.EOF
'do something with the current record, the same way you are currently doing it
rs.MoveNext
Loop</b>
End If</pre>

ANSWERED:  Jun 6, 2012  10:29 PM (GMT)  by carlosdl

 
Other Answers:

I don’t currently have a VB6 installation to test, but if I remember well, you can use the recordset’s EOF property to control a loop to read all the rows in the recordset.

Something like this:

<pre>Call dbConnect
SQL = “SELECT Grade, Sum(Currentweight) as GradePR1 FROM Weighroom WHERE (currentdate = DateValue(Now) and Roomno = ’01′) Group by Grade”
rs.Open SQL, Conn
<b>if rs.BOF and rs.EOF then
‘do something, the recordset is empty
else
rs.MoveFirst
Do Until rstPublishers.EOF
‘do something with the current record, the same way you are currently doing it
rs.MoveNext
Loop</b>
End If</pre>

Last Wiki Answer Submitted:  June 6, 2012  10:06 pm  by  carlosdl   63,535 pts.
Latest Answer Wiki Contributors:  carlosdl   63,535 pts.
To see other answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Carlosdl.. huge thankyou.. such a simpy thing to add and it works a treat..

 225 pts.

 

Great.

I’m glad it was helpful.

 63,535 pts.