473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Multi-level Group BY clause is not allowed in a subquery.

Denburt
1,356 Expert 1GB
It has been a long time since I have run into this issue but using a subquery in a Query that is used in a sub-report that uses a group header or footer returns the title (from this thread) as a message. I suppose I'm now off to rewrite a whole mess of code (inside as well as outside of MS Access) then use an update query to place the data in a table so I can use it in the report. This is going to take some extensive planning so I will be putting it off for at least the next couple of days to work on some other items so any input would be appreciated.

This is a very extensive query and has to be exported using a server that does not have MS Access installed so I can't use Dlookup() or any other functions that are MS Access specific.

I just thought I would throw this out there just to see if anyone has any ideas on another approach or possible solution?
May 4 '09 #1
6 4080
Denburt
1,356 Expert 1GB
I do believe I have found a workaround, I used a local table that I keep 1 record in and converted the select query into a Union query with a where statement that would leave that portion of the Union empty.
Expand|Select|Wrap|Line Numbers
  1. UNION SELECT Null, Null, Null, Null
  2. From sometable
  3. Where 1 = 2
If anyone has ever attempted this or knows of any reason this may present a problem please let me know.

If anyone has a better idea of a workaround that would also be appreciated thanks.

Although reluctant to insert a link to the thread where I found this information I would like to credit the user "Marshall Barton" for providing the workaround to this issue.
May 4 '09 #2
NeoPa
32,556 Expert Mod 16PB
I can't say I've ever come across that problem specifically Den, but I do understand what may cause the workaround to work. I've had similar problems resolved in a similar way.

Sometimes, Access's attempts to compile the SQL fails abysmally. It's not as clever a SQL compiler as other, more sophisticated, engines. It also fails to recognise when it can't manage to optimise something so when it fails it falls over.

It does recognise however, that it can't optimise any UNION queries. Thus, when it comes across this it puts it aside to run as is. It may run less quickly than optimised queries, but it runs at least. A useful technique to have available for various situations.

Sorry I couldn't be more help on this one.
May 7 '09 #3
Denburt
1,356 Expert 1GB
Thanks for the input NeoPa you definately have it nailed, from what I have read it appears that when grouping of the subreports MS Access reconstructs the subreports query and thats when things get messy thus the error. This report is already quite slow due to the detail of the report and the number of subreports used so I am just hoping it doesn't slow things up to much. I think it will still be faster than running an update to a table then going that route.

I also found that this isn't going to be as easy to implement due to the reporting form I am using and a lot of the code behind it. I have been rewriting code like mad so I can get this back up and running but when I am done I will have some routines that are more flexible than ever. The queries I am using require that they be redesigned on the fly inserting and replacing where statements and such and since it have multiple select statements now... Well lets just say it is interesting to say the least. Looping looking for the last from clause then locating the where statement etc. Anyone looking for a nice routine for removing or inserting where statements? LOL That might be good for an article sometime. Tons of fun but I should be getting close. Thanks again for the input.
May 8 '09 #4
NeoPa
32,556 Expert Mod 16PB
Ah, you should have said Den.

I have some routines I'm happy to share. I hope none call other procedures I haven't included. I just grabbed them all en-block.

I know sometimes using other people's code is more trouble than it's worth, but in case they can be of help here they are :
Expand|Select|Wrap|Line Numbers
  1. 'MultiReplace replaces all occurrences of varParam in strMain with varReplace.
  2. 'Using VbBinaryCompare means that case is not ignored.
  3. Public Function MultiReplace(ByRef strMain As String, _
  4.                              ByVal varParam As Variant, _
  5.                              ByVal varReplace As Variant, _
  6.                              ParamArray avarArgs())
  7.     Dim intIdx As Integer
  8.  
  9.     If (UBound(avarArgs) - LBound(avarArgs)) Mod 2 = 0 Then Stop
  10.     MultiReplace = Replace(Expression:=strMain, _
  11.                            Find:=Nz(varParam, ""), _
  12.                            Replace:=Nz(varReplace, ""), _
  13.                            Compare:=vbBinaryCompare)
  14.     For intIdx = LBound(avarArgs) To UBound(avarArgs) Step 2
  15.         MultiReplace = Replace(Expression:=MultiReplace, _
  16.                                Find:=Nz(avarArgs(intIdx), ""), _
  17.                                Replace:=Nz(avarArgs(intIdx + 1), ""), _
  18.                                Compare:=vbBinaryCompare)
  19.     Next intIdx
  20. End Function
Expand|Select|Wrap|Line Numbers
  1. 'GetSQL gets the SQL component from a named query OR a SQL string.
  2. 'When subqueries are specified in MS Access they are changed internally
  3. 'from   "FROM (SELECT blah blah blah) AS" to
  4. 'either "FROM [SELECT blah blah blah]. AS"
  5. 'or     "FROM [SELECT blah blah blah] AS" both of which are invalid SQL.
  6. 'This code assumes any effected subquery will start with " [SELECT ".
  7. 'This reverts SQL to correct format and loses ';' at end if requested.
  8. ' 11/11/2004    Updated to detect second problem and to use InStr
  9. Public Function GetSQL(strQuery As String, _
  10.                        Optional blnLoseSC As Boolean = True) As String
  11.     Dim intDepth As Integer
  12.     Dim lngLeft As Long, lngOpen As Long, lngRight As Long, lngAdjust As Long
  13.  
  14.     'If param passed is already SQL then leave as is - Otherwise get from query
  15.     If Left(strQuery, 1) = "(" Then
  16.         GetSQL = strQuery
  17.     Else
  18.         On Error Resume Next
  19.         GetSQL = CurrentDb.QueryDefs(strQuery).SQL
  20.     End If
  21.     lngLeft = -7&
  22.     Do
  23.         'Check for corrupted subquery
  24.         lngLeft = InStr(lngLeft + 8&, GetSQL, " [SELECT ", vbTextCompare)
  25.         If lngLeft = 0& Then Exit Do
  26.         'To find end correctly we must treat '[' & ']' as matched pairs
  27.         intDepth = 1
  28.         lngRight = lngLeft + 8&
  29.         lngOpen = -lngRight
  30.         Do
  31.             'Find next ']'
  32.             lngRight = InStr(lngRight + 1&, GetSQL, "]", vbBinaryCompare)
  33.             If lngRight = 0& Then
  34.                 GetSQL = ""
  35.                 Exit Function
  36.             End If
  37.             intDepth = intDepth - 1
  38.             Do
  39.                 'For lngOpen negative numbers mean that item has been counted
  40.                 'If already counted get next one - Otherwise drop through
  41.                 If lngOpen < 0& Then _
  42.                     lngOpen = InStr(-lngOpen + 1&, GetSQL, "[", vbBinaryCompare)
  43.                 'we're only interested (now) if it found one BEFORE the ']'
  44.                 If lngOpen > lngRight Or lngOpen = 0& Then Exit Do
  45.                 intDepth = intDepth + 1
  46.                 lngOpen = -lngOpen
  47.             Loop
  48.         Loop While intDepth > 0
  49.         'If '].' found then be sure to drop the '.' too
  50.         lngAdjust = IIf(Mid(GetSQL, lngRight + 1&, 1) = ".", 1&, 0&)
  51.         GetSQL = Left(GetSQL, lngLeft) & "(" & _
  52.                  Mid(GetSQL, lngLeft + 2&, lngRight - lngLeft - 2&) & ")" & _
  53.                  Right(GetSQL, Len(GetSQL) - (lngRight + lngAdjust))
  54.     Loop
  55.     'Lose ";" at end if requested and it exists
  56.     If blnLoseSC And Right(GetSQL, 3) = ";" & vbCrLf Then _
  57.         GetSQL = Left(GetSQL, Len(GetSQL) - 3)
  58. End Function
Expand|Select|Wrap|Line Numbers
  1. 'ChangeWhere changes the WHERE clause (in strSQL) for the field found in
  2. 'strWhere to match the criteria specified by varFrom & varTo.
  3. '20/1/2004  Added strTemp to restart search in right place.
  4. Public Sub ChangeWhere(strSQL As String, strWhere As String, _
  5.                        strType As String, strCmp As String, _
  6.                        Optional ByVal varFrom As Variant = Null, _
  7.                        Optional ByVal varTo As Variant = Null)
  8.     Dim intLeft As Integer, intRight As Integer
  9.     Dim strAND As String, strTemp As String
  10.  
  11.     intRight = 1
  12.     Do
  13.         intLeft = InStr(intRight, strSQL, strWhere, vbTextCompare) - 1
  14.         If intLeft > 0 Then
  15.             intRight = GetRight(strSQL, intLeft, strAND)
  16.             If strAND = "L" Then intLeft = intLeft - 5
  17.         Else
  18.             If intRight > 1 Then Exit Do
  19.             intLeft = InStr(1, strSQL, "WHERE (", vbTextCompare) + 6
  20.             If intLeft < 7 Then Exit Do
  21.             intRight = GetRight(strSQL, intLeft - 1, strAND) - 1
  22.             If intLeft < (intRight - 1) Then
  23.                 intLeft = intRight - 1
  24.                 strAND = "L"
  25.             End If
  26.         End If
  27.         strTemp = NewWhere(strWhere, strType, strAND, strCmp, varFrom, varTo)
  28.         strSQL = Left(strSQL, intLeft) & strTemp & Mid(strSQL, intRight)
  29.         intRight = intLeft + Len(strTemp) + 1
  30.     Loop
  31. End Sub
Expand|Select|Wrap|Line Numbers
  1. 'GetRight finds the rightmost position of the current field (where intLeft is
  2. 'pointing) in the strSQL WHERE clause.
  3. Public Function GetRight(strSQL As String, ByVal intLeft As Integer, _
  4.                          ByRef strAND As String) As Integer
  5.     Dim intParenth As Integer, intClose As Integer
  6.  
  7.     strAND = IIf(intLeft > 5 And _
  8.                  Mid(strSQL, intLeft - 4, 5) = " AND ", "L", "N")
  9.     Do
  10.         intClose = InStr(intLeft + 1, strSQL, ")", vbBinaryCompare)
  11.         intParenth = intParenth - 1
  12.         Do
  13.             intLeft = InStr(intLeft + 1, strSQL, "(", vbBinaryCompare)
  14.             Select Case intLeft
  15.                 Case 0, Is > intClose
  16.                     intLeft = intClose
  17.                 Case Else
  18.                     intParenth = intParenth + 1
  19.             End Select
  20.         Loop While intLeft < intClose
  21.     Loop Until intParenth <= 0
  22.     GetRight = intClose + 1
  23.     If strAND = "N" And Mid(strSQL, GetRight, 5) = " AND " Then
  24.         strAND = "R"
  25.         GetRight = GetRight + 5
  26.     End If
  27. End Function
Expand|Select|Wrap|Line Numbers
  1. 'NewWhere formulates a new section of a WHERE clause for strField.
  2. '15/3/2005  Added support for the 'Like' comparison.
  3. Public Function NewWhere(strField As String, strType As String, _
  4.                          strAND As String, strCmp As String, _
  5.                          Optional ByVal varFrom As Variant = Null, _
  6.                          Optional ByVal varTo As Variant = Null) As String
  7.     If IsNull(varFrom) Then
  8.         If IsNull(varTo) Then
  9.             NewWhere = ""
  10.             Exit Function
  11.         End If
  12.         varFrom = varTo
  13.         varTo = Null
  14.     End If
  15.     If strCmp = "Not" Then strCmp = "<>"
  16.     If Not IsNull(varTo) _
  17.     And StrComp(varFrom, varTo, vbTextCompare) <> 0 Then
  18.         If strCmp = "=" Then strCmp = "Between"
  19.         If strCmp = "<>" Then strCmp = "Not Between"
  20.     End If
  21.     If VarType(varFrom) = vbDate Then _
  22.         varFrom = SQLDate(varDate:=varFrom, blnHash:=False)
  23.     If VarType(varTo) = vbDate Then _
  24.         varTo = SQLDate(varDate:=varTo, blnHash:=False)
  25.     Select Case strCmp
  26.         Case "=", "<", "<=", ">", ">=", "<>"
  27.             NewWhere = strField & strCmp & strType & varFrom & strType & ")"
  28.         Case "In", "Not In"
  29.             NewWhere = strField & " " & strCmp & "(" & varFrom & "))"
  30.         Case "Between", "Not Between"
  31.             NewWhere = strField & " " & strCmp & " " & _
  32.                        strType & varFrom & strType & " And " & _
  33.                        strType & varTo & strType & ")"
  34.         Case "Like"
  35.             NewWhere = strField & " Like '" & varFrom & "')"
  36.     End Select
  37.     NewWhere = IIf(strAND = "L", " AND ", "") & _
  38.                NewWhere & _
  39.                IIf(strAND = "R", " AND ", "")
  40. End Function
May 8 '09 #5
Denburt
1,356 Expert 1GB
Thanks for the routines i haven't had a chance to read through them all just yet I swear some of it is a mirror image to what I have, at least in some ways. I have about 4 projects I am working (at this moment) so it's going to be a busy day. When I can I will try and implement the routines you have so graciously provided and let you know how things go. Thanks again
May 8 '09 #6
NeoPa
32,556 Expert Mod 16PB
If you feel you can work with them sure Den. Otherwise just cherry-pick the bits you think will help.

It's no big deal, I've had to develop them myself for my own uses, and they've served me well. I'm happy for anyone to use them.

I have more for other things. If you ever fancy looking through any just ask me. Specifically or generally, whatever works for you.
May 8 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
6
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.