Getting around Access 255 Column Limit

Excellor_From_London

Board Regular
Joined
Aug 5, 2007
Messages
50
Hello there,

I have a cross tab query that returns more than 256 columns of data from a table but because Access has a limit in terms of the number of columns it can display, I am not getting any results back.

To by pass this, does anyone know if it is possible to write the output from a cross tab query directly to a text file? Then I could just import the file into another application (or the new excel that accommodates 16K columns) to work on.

Or is it possible to connect another querying tool (e.g. aqua data or some other sql tool) directly to access/the table and run the query from there?

Thanks

Fellow Excellor
 
Hi Giacomo,

I didn't realise I could use a query instead of a table directly in the code. I've tried this and while it creates a output file, there are blanks instead of values (example below) in the output file. When you run the query there are values for these dates and in fact I checked the raw data file and indeed this is the case.

Any idea why this maybe? Could it be I need to change a parameter in the VBA code?

Thanks,

Excellor
---------------
Date Value

26/01/2007 444
02/02/2007 442.75
09/02/2007
16/02/2007 432.75
23/02/2007 423.5
02/03/2007
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You're creating a crosstab, so not only does the date need to exist but the column value for that date needs to exist as well... are you saying this is the case? If so can you post an example?

Maybe I'm misunderstanding the post, if you're just wanting to replace the nulls with zero's then change this code:

stValList = stValList & rs3.Fields(0) & ","

to this:

stValList = stValList & Nz(rs3.Fields(0), 0) & ","

hth,
Giacomo
 
Upvote 0
Hi,

Yes indeed, the column values for those dates do exist as the example will make clear.

Raw Data Table - Called Hamy

Code Date Close
RSA 21-Jan-94 313.76
RSA 28-Jan-94 325.02
RSA 04-Feb-94 325.02
RSA 11-Feb-94 308.13
RTR 21-Jan-94 474.25
RTR 28-Jan-94 493.25
RTR 04-Feb-94 483.75
RTR 11-Feb-94 508
RTR 18-Feb-94 517

Output from Cross Tab (AccCSV File):

date RSA RTR
21/01/1994 313.76 474.25
28/01/1994 325.02 493.25
04/02/1994
11/02/1994
18/02/1994 304.1 517


As you can see values for the dates 04/02/94 and 11/02/94 are missing in the output file but are in the orginal data table above. The parameters in the VBA code are below.

Thanks,

Execllor

Sub csvCrossTabber()

* Change These Values As Neeed *********

Const myFilePath = "C:\Documents and Settings\bulsarah\Files\trading\" ' don't forget to end with a slash \
Const OutputFileName = "AccCSV"
Const OutNum As Integer = 2

stTableName = "jamyhmay" ' Name of table to query
stColField = "Code" ' Name of field to use as the column in crosstab
stValField = "Close" ' Name of field to use as the value in crosstab
stAggFunction = "sum" ' Aggregate function to perform on value field ( Example sum ,count, min, max )

Const numFields = 0 'This is the number of row fields in your crosstab starting at 0 (0 = 1, 1 = 2, etc.)

Dim arrRowFields(numFields) As String

arrRowFields(0) = "Close" ' Row Field 1
'arrRowFields(1) = "Department" ' Row Field 2
' add more row fields as needed, increment the numFields too


' ******************************** Do not modify anything below this line ************************************
 
Upvote 0
interesting... is it always dates where the day is less than 13 like in your examples? If so there must be some sort of date conversion issue, if you can confirm I'll look at the code with that in mind and see if I can find the bug.

Hope you see where I'm going with the date thing, example these dates worked: 21/01/1994, 28/01/1994, 18/02/1994
but these did not: 04/02/1994, 11/02/1994

almost seems as if it's reading the dates as mm/dd/yyyy instead of dd/mm/yyyy when the day is <= 12

hth,
Giacomo
 
Upvote 0
interesting... is it always dates where the day is less than 13 like in your examples? If so there must be some sort of date conversion issue, if you can confirm I'll look at the code with that in mind and see if I can find the bug.

Hope you see where I'm going with the date thing, example these dates worked: 21/01/1994, 28/01/1994, 18/02/1994
but these did not: 04/02/1994, 11/02/1994

almost seems as if it's reading the dates as mm/dd/yyyy instead of dd/mm/yyyy when the day is <= 12

hth,
Giacomo

It's a quirk of VBA-generated SQL. It *requires* US date formats, regardless of your regional settings -- and there are 2 workarounds that I know of. One is to wrap CDbl around your dates to convert them to numbers; the SQL works fine with this, and you don't get 1/7/2007 being interpreted as 7 Jan instead of 1 Jul.

The second option is to use a custom function to convert the dates into US format. See this post.

Denis
 
Upvote 0
Denis - Thanks! I know I've seen you explain that before but it won't sink in for me.

Excellor - I'm not at the computer where I have that code right now so I won't be able to post a fix until later.
 
Upvote 0
OK, I think I fixed it... hard to tell though because being in the US the date issue does not affect me. So give this a try and let me know if it worked...

replace everything I gave you with this, I made a couple changes and it ran faster for me.

Code:
Option Explicit
Option Base 0

Sub csvCrossTabber()

'Create File
    Dim CrossTabFile As String
    Dim stTableName As String
    Dim stColField As String
    Dim stColList As String
   
    Dim stRowList As String
    Dim stRowList2 As String
    Dim stValField As String
    Dim stValList As String
    Dim stWhere As String
    Dim stWhere2 As String
    Dim stAggFunction As String
    Dim colRows As New Collection
       
    ' *****************************************  Change These Values As Needed *************************************
   
    Const myFilePath = "C:\giacomo\"  ' don't forget to end with a slash \
    Const OutputFileName = "AccCSV"
    Const OutNum As Integer = 2
   
    stTableName = "Order Summary"  ' Name of table to query
    stColField = "Customer ID"     ' Name of field to use as the column in crosstab
    stValField = "Sub Total"       ' Name of field to use as the value in crosstab
    stAggFunction = "Sum"          ' Aggregate function to perform on value field ( Example sum ,count, min, max )
   
   ' add more row fields as needed
    colRows.Add "Order Date"
    
      
' ********************************  Do not modify anything below this line  ************************************
    CrossTabFile = myFilePath & OutputFileName & ".csv"
   
    'Kill Previous File if it Exists
    If FileExist(CrossTabFile) Then
        Kill (CrossTabFile)
    End If
   
    'Open File
    Open CrossTabFile For Append As OutNum

'Write Crosstab to File
    'Connection Variables
    Dim con As ADODB.Connection
    
    Dim rs1 As ADODB.Recordset
    Dim rs2 As ADODB.Recordset
    
    Dim stSql1 As String
    Dim stSql2 As String
    Dim stSql3 As String
         
    Dim i As Integer
    For i = 1 To colRows.Count
        stRowList = stRowList & colRows.Item(i) & ","
        stRowList2 = stRowList2 & getWrapper(colRows.Item(i), True) & ","
    Next i
   
    'Get First Row
    Set con = Application.CurrentProject.Connection
    stSql1 = "SELECT distinct [" & stColField & "] FROM [" & stTableName & "];"
    Set rs1 = New ADODB.Recordset
    rs1.Open stSql1, con, 1
         
    stColList = getRow(stSql1)
   
    'Print out first row
    Print #OutNum, stRowList; stColList

'    rs1.MoveFirst

'Write Values
    stRowList2 = Left(stRowList2, Len(stRowList2) - 1)
    stSql2 = "SELECT distinct " & stRowList2 & " FROM [" & stTableName & "];"
    Set rs2 = New ADODB.Recordset
   
    rs2.Open stSql2, con, 1

    If Not (rs2.EOF) Then
        Do While (Not (rs2.EOF))
            stValList = ""
            stWhere = ""
           
            For i = 0 To colRows.Count - 1
                stValList = stValList & rs2.Fields(i) & ","
                stWhere = stWhere & getWrapper(rs2.Fields(i).Name, True, rs2.Fields(i).Type) & " = " & getWrapper(rs2.Fields(i), False, rs2.Fields(i).Type) & " AND "
            Next i
           
            If Not (rs1.EOF) Then
                Do While (Not (rs1.EOF))
                    stWhere2 = stWhere
                    stWhere2 = stWhere2 & getWrapper(stColField, True) & " = " & getWrapper(rs1(stColField), False, rs1(stColField).Type)
                    stSql3 = "SELECT " & stAggFunction & "([" & stValField & "]) FROM [" & stTableName & "] where " & stWhere2
                    
                    stValList = stValList & getRow(stSql3)
                                        
                    rs1.MoveNext
                Loop
            End If
                           
                           
            Print #OutNum, stValList
            rs1.MoveFirst
            rs2.MoveNext
        Loop
    End If
            rs1.Close
            rs2.Close

'Cleanup
    Set rs1 = Nothing
    Set con = Nothing
   
    'Close File
    Close #OutNum
End Sub

Function FileExist(myFileName$)

  ' First Check to ensure that myFileName is a valid file

  On Error GoTo NotFound            ' provide alternate error handling

  Open myFileName$ For Input As 255 ' attempt to open file
  On Error Resume Next              ' if success, restore default error handling
  Close 255                         ' close file (it was only a test, afterall)

  FileExist = True
 
  Exit Function
 
NotFound:
  On Error Resume Next
  FileExist = False
 
End Function ' File Exist

Function getWrapper(value As String, isField As Boolean, Optional fieldType As Integer) As String

getWrapper = value

If isField And InStr(1, value, " ") > 0 Then
    getWrapper = "[" & value & "]"
End If


Select Case Nz(fieldType, 0)

Case 7
If isField Then
    getWrapper = "cdbl(" & getWrapper & ")"
Else
    getWrapper = "cdbl(#" & getWrapper & "#)"
End If
Case 202
getWrapper = "'" & getWrapper & "'"

Case Else
'Nothing

End Select

End Function

Function getRow(SQL As String) As String
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset

Set con = Application.CurrentProject.Connection
Set rs = New ADODB.Recordset

rs.Open SQL, con, 1

If Not (rs.EOF) Then
    Do While (Not (rs.EOF))
        getRow = getRow & rs.Fields(0) & ","
        rs.MoveNext
    Loop
End If

'getRow = Left(getRow, Len(getRow) - 1)

rs.Close
con.Close

Set rs = Nothing
Set con = Nothing
End Function

hth,
Giacomo
 
Upvote 0
Hi,

Thanks for the amended code. Unfortunately, something very strange seems to have happened and I have not been able to test out the new code.

Every time I run the old code (and new code); I am getting the following error:

"Method 'open' of object '_Recordset' failed" -2147467259. When I debug it goes to this line of the code below:

Line: rs2.Open stSql2, con, 1

'Write Values
stRowList2 = Left(stRowList2, Len(stRowList2) - 1)
stSql2 = "SELECT distinct " & stRowList2 & " FROM [" & stTableName & "];"
Set rs2 = New ADODB.Recordset
Set rs3 = New ADODB.Recordset

I can't seem to work out why this happening as nothing as changed. I'm still using the original table etc.

Any idea why this may have occured?

Thanks,

Excellor
 
Upvote 0
can you post the value of stSQL2 when the error occurs?

Just use the immediates window and type in: ? stSQL2

did you maybe change one of the field names in your query?
 
Upvote 0

Forum statistics

Threads
1,215,201
Messages
6,123,621
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top