I am running a bunch of aggregate insert queries to populate a table.
When I run the SQL statement via a saved query, all is well. However,
when I run it as an ADO recordset in VBA, the count is one greater
than the actual number of records. Out of over 15 queries, only two
do this and there does not appear to be anything special about any of
the records that satisfy the criteria.
Any ideas?! (Code below)
Fred
Sample SQL:
INSERT INTO InstallByMarket ( Area, Market, SiteCnt )
SELECT area, "Manhattan", Count(ID) AS CountOfID
FROM Installs WHERE((Clustername = "New York") AND (Marketareaname =
"West NY") AND ((Relos = 0) AND (Sector = 0)))
GROUP BY area, 'Manhattan';
VBA code to automate:
Dim rs As ADODB.Recordset, rsIn As ADODB.Recordset
Dim Cnn As ADODB.Connection
Dim strSQL As String, strCrit As String, strOther As String
Dim Fld As Field
Dim i As Integer
Set rs = New ADODB.Recordset
Set rsIn = New ADODB.Recordset
Set Cnn = CurrentProject.Connection
rs.Open "SELECT * FROM MarketCriteria", Cnn, adOpenStatic
rsIn.Open "DELETE FROM InstallByMarket", Cnn
rsIn.Open "UPDATE Installs SET Market = 0", Cnn
Do Until rs.EOF
strSQL = "INSERT INTO InstallByMarket ( Area, Market, SiteCnt
) " & vbCrLf
strSQL = strSQL & "SELECT area, """ & rs.Fields("Market") &
""", Count(ID) AS CountOfID " & vbCrLf
strSQL = strSQL & "FROM Installs " & vbCrL
strCrit = "WHERE("
'create dynamic criteria here
For i = 3 To rs.Fields.Count - 1
If Not IsNull(rs.Fields(i)) Then
strCrit = strCrit & "(" & rs.Fields(i) & ") AND "
End If
Next i
strOther = "((Relos = 0) AND (Sector = 0)))"
strSQL = strSQL & strCrit & strOther & vbCrLf & "GROUP BY
area, '" & rs.Fields("Market") & "';"
rsIn.Open strSQL, Cnn
Debug.Print strSQL
Debug.Print
rs.MoveNext
Loop 2 15801
Clark, no suggestions on why this is happening since it should not, but does
it make any difference if the literal value is omitted from the GROUP BY
clause?
INSERT INTO InstallByMarket (Area, 'Manhattan', SiteCnt)
SELECT area, Count( ...
ORDER BY area;
If this does solve the problem, please post back. It would be good to raise
the issue as a bug if you have a reproducable scenario.
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"Clark Savage Jr" <cl*********@yahoo.com> wrote in message
news:3c**************************@posting.google.c om... I am running a bunch of aggregate insert queries to populate a table. When I run the SQL statement via a saved query, all is well. However, when I run it as an ADO recordset in VBA, the count is one greater than the actual number of records. Out of over 15 queries, only two do this and there does not appear to be anything special about any of the records that satisfy the criteria.
Any ideas?! (Code below)
Fred
Sample SQL: INSERT INTO InstallByMarket ( Area, Market, SiteCnt ) SELECT area, "Manhattan", Count(ID) AS CountOfID FROM Installs WHERE((Clustername = "New York") AND (Marketareaname = "West NY") AND ((Relos = 0) AND (Sector = 0))) GROUP BY area, 'Manhattan';
VBA code to automate: Dim rs As ADODB.Recordset, rsIn As ADODB.Recordset Dim Cnn As ADODB.Connection Dim strSQL As String, strCrit As String, strOther As String Dim Fld As Field Dim i As Integer
Set rs = New ADODB.Recordset Set rsIn = New ADODB.Recordset Set Cnn = CurrentProject.Connection
rs.Open "SELECT * FROM MarketCriteria", Cnn, adOpenStatic rsIn.Open "DELETE FROM InstallByMarket", Cnn rsIn.Open "UPDATE Installs SET Market = 0", Cnn
Do Until rs.EOF
strSQL = "INSERT INTO InstallByMarket ( Area, Market, SiteCnt ) " & vbCrLf strSQL = strSQL & "SELECT area, """ & rs.Fields("Market") & """, Count(ID) AS CountOfID " & vbCrLf strSQL = strSQL & "FROM Installs " & vbCrL strCrit = "WHERE(" 'create dynamic criteria here For i = 3 To rs.Fields.Count - 1 If Not IsNull(rs.Fields(i)) Then strCrit = strCrit & "(" & rs.Fields(i) & ") AND " End If Next i
strOther = "((Relos = 0) AND (Sector = 0)))" strSQL = strSQL & strCrit & strOther & vbCrLf & "GROUP BY area, '" & rs.Fields("Market") & "';"
rsIn.Open strSQL, Cnn Debug.Print strSQL Debug.Print
rs.MoveNext Loop
I switched to DAO and had to change the wildcards to "*" --now all is well.
Very strange indeed!
"Allen Browne" <al*********@SeeSig.invalid> wrote in message news:<3f**********************@freenews.iinet.net. au>... Clark, no suggestions on why this is happening since it should not, but does it make any difference if the literal value is omitted from the GROUP BY clause?
INSERT INTO InstallByMarket (Area, 'Manhattan', SiteCnt) SELECT area, Count( ... ORDER BY area;
If this does solve the problem, please post back. It would be good to raise the issue as a bug if you have a reproducable scenario. -- Allen Browne - Microsoft MVP. Perth, Western Australia. Tips for Access users - http://allenbrowne.com/tips.html Reply to group, rather than allenbrowne at mvps dot org.
"Clark Savage Jr" <cl*********@yahoo.com> wrote in message news:3c**************************@posting.google.c om... I am running a bunch of aggregate insert queries to populate a table. When I run the SQL statement via a saved query, all is well. However, when I run it as an ADO recordset in VBA, the count is one greater than the actual number of records. Out of over 15 queries, only two do this and there does not appear to be anything special about any of the records that satisfy the criteria.
Any ideas?! (Code below)
Fred
Sample SQL: INSERT INTO InstallByMarket ( Area, Market, SiteCnt ) SELECT area, "Manhattan", Count(ID) AS CountOfID FROM Installs WHERE((Clustername = "New York") AND (Marketareaname = "West NY") AND ((Relos = 0) AND (Sector = 0))) GROUP BY area, 'Manhattan';
VBA code to automate: Dim rs As ADODB.Recordset, rsIn As ADODB.Recordset Dim Cnn As ADODB.Connection Dim strSQL As String, strCrit As String, strOther As String Dim Fld As Field Dim i As Integer
Set rs = New ADODB.Recordset Set rsIn = New ADODB.Recordset Set Cnn = CurrentProject.Connection
rs.Open "SELECT * FROM MarketCriteria", Cnn, adOpenStatic rsIn.Open "DELETE FROM InstallByMarket", Cnn rsIn.Open "UPDATE Installs SET Market = 0", Cnn
Do Until rs.EOF
strSQL = "INSERT INTO InstallByMarket ( Area, Market, SiteCnt ) " & vbCrLf strSQL = strSQL & "SELECT area, """ & rs.Fields("Market") & """, Count(ID) AS CountOfID " & vbCrLf strSQL = strSQL & "FROM Installs " & vbCrL strCrit = "WHERE(" 'create dynamic criteria here For i = 3 To rs.Fields.Count - 1 If Not IsNull(rs.Fields(i)) Then strCrit = strCrit & "(" & rs.Fields(i) & ") AND " End If Next i
strOther = "((Relos = 0) AND (Sector = 0)))" strSQL = strSQL & strCrit & strOther & vbCrLf & "GROUP BY area, '" & rs.Fields("Market") & "';"
rsIn.Open strSQL, Cnn Debug.Print strSQL Debug.Print
rs.MoveNext Loop This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: dmiller23462 |
last post by:
My brain is nuked....Can anybody tell me right off the bat what is
wrong with this code? Along with any glaring errors, please let me
know the syntax to display a message (Response.Write would be...
|
by: kinne |
last post by:
The following code is supposed to reverse the date in "yyyy-mm-dd" format,
but it produces different results in Firefox 1.0 and in Internet Explorer
6SP1. In Firefox, the result is correct...
|
by: Dom Boyce |
last post by:
Hi
First up, I am using MS Access 2002.
I have a database which records analyst rating changes for a list of
companies on a daily basis. Unfortunately, the database has been set
up (by my...
|
by: Taylor |
last post by:
Good afternoon.
I have recently converted an Access 97 application to Access 2003. The
conversion went rather smoothly. However I am getting different
results from the same exact query. I can...
|
by: Gerry Abbott |
last post by:
Hi all,
I having some confusing effects with recordsets in a recent project.
I created several recordsets, each set with the same number of records, and
related with an index value.
I create...
|
by: google |
last post by:
I'm using ADODB to connect to an Access database from VB6. I'm running
a query that's returning strange results- the following query returns
different results in VB & Access:
SELECT * FROM...
|
by: Oko |
last post by:
I'm currently developing an MS Access Data Project (.adp) in MS Access
2002.
One of the reports within the DB uses data that is Dynamic and cannot
be stored on the SQL Server. To resolve this, I...
|
by: wallconor |
last post by:
Hi,
I am having a problem using Dreamweaver CS3 standard recordset paging behavior. It doesn’t seem to work when I pass parameter values from a FORM on my search page, to the recordset on my...
|
by: Gord |
last post by:
Me again,
I'm new to Access and am self teaching from a couple of books, so bear with
me. (I've got a little experience with Visual Basic)
As I understand so far, if I want to perform a bunch...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |