views:

308

answers:

1

I'm getting a formatting problem if I use more than 10 UNION ALL statements in my VBA Code.

If I use 10 or less everything works great.

What I'm trying to do is combine 12 worksheets (Excel 2007).

I have a numerical column called SC that turns into string and date if I have more than 10 UNION ALL. If I try to use ROUND with more than 10 UNION ALL my last selection will change all the records by one unit.

I'm using Microsoft.ACE.OLEDB.12.0 as my provider and my connection string has worked for several things in my code so far.

Is there any limit for UNION ALL statements when using OLEDB?

Here is my code.

Dim StrOr As String
Dim i As Variant
Dim Cnt As ADODB.Connection
Dim Rs As ADODB.Recordset

For i = 1 To 12
    StrOr = StrOr & " " & "SELECT SC FROM [" & MonthName(i, True) & "$" & "] UNION ALL"
Next

StrOr = Left(StrOr, Len(StrOr) - 9) & ";"

Call GetADOCnt

Call ADORs
A: 

If you're appending the UNION of the 12 worksheets, why not just use your loop to append each worksheet one at a time? You obviously don't need to de-dupe (you're using UNION ALL), so that seems like it would be an obvious solution to avoiding whatever problem is happening with your UNION'ed recordset.

David-W-Fenton
I was thinking the same thing, but I believe they also what to GROUP BY.
AMissico
I interpreted this differently: "I'm just copying the merged tables to another table. My goal was to group by the merged tables". That seems to say GROUP BY after the append, but you may be right that he was trying to do it in one go. If the UNION doesn't work, perhaps he could use a staging table as the GROUP BY source. But ultimately, if your surmise about the reason for the error is correct, it's going to encounter the error even appending one-by-one, and changing to that approach would make it easier to diagnose which spreadsheet is the problem child and address it individually.
David-W-Fenton
Yes I'm trying to union all the tables to do a group by after that.
VBGKM
If you're doing the GROUP BY on the table instead of on the UNION SQL, then changing to append one worksheet at a time should help you resolve the problem and not change the results at all.
David-W-Fenton
How can I append one worksheet at a time and then do the GROUP BY?
VBGKM
Inside your loop, instead of writing a SQL UNION, write a SQL INSERT and execute each one in each repetition of the loop. Then once the loop finishes, you'll have all your data in the table and can then GROUP BY on the appended table data.
David-W-Fenton
I tried StrOr = "INSERT INTO [" " And I got the following error (Operation is not allowed when the object is closed.)
VBGKM
Does the SELECT part work by itself, without the INSERT part?
David-W-Fenton
Yes the SELECT part work by itself.
VBGKM
Then the problem is in the SQL for the INSERT and you'll have to troubleshoot that to figure out what the problem is. I don't know the table and field names of your destination table, so can't really offer any specific advice.
David-W-Fenton