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
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi

Why are there more than 256 columns? Can you rearrange the data the other way round? Or is that not an option? Also, have you considered filtering the data in some way to limit the columns and just work with chunks of data?

Andrew
 
Upvote 0
As Andrew said, 256 cols is a LOT of columns! Surely there is a way to filter, and reduce the column count?

Otherwise,
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.
You can pull the raw data from Access into Excel and use a pivot table to generate the output. If you have 2007, click the Data ribbon. From Access is the first button: click and follow the prompts. In earlier versions, Data > Get External Data > New Database Query and follow the wizard to set it up.

Denis
 
Upvote 0
Hi,

I do indeed need more than 256 columns. In fact I have 3000 columns (each column represents a stock price - I'm doing some clustering analysis). I thought about using excel's pivot table, the problem is the query I’m running does various things to the data from the source access table including converting it from daily to weekly series. So I would have to work out a way to do the equivalent of what the query does in excel. Not great as I've spent a lot of time setting things up in Access and getting everything into the right format.

Excellor
 
Upvote 0
Maybe way off beam, but...

Is it possible to use yield or % return instead of the raw stock price?
Either way, you could use the Partition function to group the prices / yields into, say, 200 buckets rather than the individual values. If you're worried about skewing the results, you could keep a count of stocks in each bucket.

That will fit in a crosstab.

Denis
 
Upvote 0
Have you thought about swapping Axis?
Putting the 3000 items along each row and something else across the top?
 
Upvote 0
Thanks for your comments guys. The number of rows also exceeds the column limit (255) so switching rows to col is not an option.

Just for the record, I tried out one of my own suggestions - connecting a front end sql tool to access and using this to retrieve the data from the backend. This does not work as the things you can do through the access odbc driver (which you connect to access databases/tables) is limited by the functionalty of access itself.

What I don't understand is how microsoft can offer more columns in excel (16K) in the new version and while keeping the number of columns the same in access! It seems pretty obvious that they do not want to give anything away for free and that if you need more columns then you need to buy different software from them (SQL Server). It's just a way to ensure the sale of alternative products.

Anyway, I think I have exhusted my options now. I will just have to run the query several times and join all the data together at the end in excel or something.

Thanks Anyway,

Excellor
 
Upvote 0
This will create a .csv file for you that you can then import into excel. I'm using Office 2003 so when I open it I still get the file not loaded completely error. You'll have to let me know if you can open the .csv in Excell 2007

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
        
    ' *****************************************  Change These Values As Needed *************************************
   
    Const myFilePath = "C:\Documents and Settings\giacomo\My Documents\"  ' don't forget to end with a slash \
    Const OutputFileName = "AccCSV"
    Const OutNum As Integer = 2
    
    stTableName = "Table2"  ' Name of table to query
    stColField = "Day"      ' Name of field to use as the column in crosstab
    stValField = "Sales"    ' 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 = 1 '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) = "Region Name" ' 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  ************************************
    CrossTabFile = myFilePath & OutputFileName & ".csv"
    
    'Kill Previous File if 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 rs3 As ADODB.Recordset
    Dim stSql1 As String
    Dim stSql2 As String
    Dim stSql3 As String
    
    'Get First Row
    Set con = Application.CurrentProject.Connection
    stSql1 = "SELECT distinct [" & stColField & "] FROM [" & stTableName & "];"
    Set rs1 = New ADODB.Recordset
    rs1.Open stSql1, con, 1
       
    Dim i As Integer
    For i = 0 To numFields
        stRowList = stRowList & arrRowFields(i) & ","
        stRowList2 = stRowList2 & fieldBracket(arrRowFields(i)) & ","
    Next i
    
    If Not (rs1.EOF) Then
        Do While (Not (rs1.EOF))
            stColList = stColList & rs1(stColField) & ","
            rs1.MoveNext
        Loop
    End If
    
    stColList = Left(stColList, Len(stColList) - 1)
    
    '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
    Set rs3 = New ADODB.Recordset
    
    rs2.Open stSql2, con, 1

    If Not (rs2.EOF) Then
        Do While (Not (rs2.EOF))
            stValList = ""
            stWhere = ""
            
            For i = 0 To numFields
                stValList = stValList & rs2.Fields(i) & ","
                stWhere = stWhere & fieldBracket(rs2.Fields(i).Name) & " = " & getDeliminator(rs2.Fields(i).Type) & rs2.Fields(i) & getDeliminator(rs2.Fields(i).Type) & " AND "
            Next i
            
            If Not (rs1.EOF) Then
                Do While (Not (rs1.EOF))
                    stWhere2 = stWhere
                    stWhere2 = stWhere2 & fieldBracket(stColField) & " = " & getDeliminator(rs1(stColField).Type) & rs1(stColField) & getDeliminator(rs1(stColField).Type)
                    
                    stSql3 = "SELECT " & stAggFunction & "([" & stValField & "]) FROM [" & stTableName & "] where " & stWhere2
                    rs3.Open stSql3, con, 1
                    
                    If Not (rs3.EOF) Then
                        Do While (Not (rs3.EOF))
                            stValList = stValList & rs3.Fields(0) & ","
                            rs3.MoveNext
                        Loop
                    End If
                    rs1.MoveNext
                    
                    rs3.Close
                Loop
            End If
                            
                            
            Print #OutNum, Left(stValList, Len(stValList) - 1)
            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 fieldBracket(fieldName As String) As String

fieldBracket = fieldName

If InStr(1, fieldName, " ") > 0 Then
    fieldBracket = "[" & fieldName & "]"
End If

End Function

Function getDeliminator(fieldType As Integer) As String

Select Case fieldType

Case 7
getDeliminator = "#"

Case 202
getDeliminator = "'"

Case Else
getDeliminator = ""

End Select
End Function
 
Upvote 0
Giacomo,

Thanks very much for the code. It was just the kind of thing I was looking for. But just one question, do you know if there is any scope within the code to put in extra criteria in terms of the rows it returns.

For example in my orginal query I took daily data and transformed it to weekly (e.g via SELECT * FROM table WHERE weekday(date)=6;). In the procedure above your working with the data in the table as opposed to the query results.

Of course this is no big deal, I could just run a query to create a new table with weekly data and run the above code on it, but I just thought I'd ask.

Many Thanks,

Excellor
 
Upvote 0
You can use a query name in place of the table name. So for instance if you have a query to get the weekday(someDate) = 6 part and you name it "Query1" just use Query1 in place of Table2 in the stTableName variable.

That should be the easiest solution. Worst-case I guess you can make a table.

hth,
Giacomo
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,424
Members
448,896
Latest member
MadMarty

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