Here is a VBA routine I used to export data with more than 255 columns.
Note that I had two recordsets based on queries. The queries had the same
number of records and the records were ordered in a unique order by same set
of fields (or the primary key). The first query had the unique order fields
as the first fields in the query. The second query did not return those
fields (not in the select list).
Hope you can expand this to cover your situation.
Public Function funExportToTabDelimited()
Dim db As database
Dim rs1 As Recordset, rs2 As Recordset
Dim strHolder As String, strTemp As String
Dim intCounter As Integer
Dim strFldDelimiter As String
strFldDelimiter = vbTab 'or Comma "," or Other value
Set db = CurrentDb
Set rs1 = db.OpenRecordset("tblExportValues", dbOpenDynaset)
Set rs2 = db.OpenRecordset("tblExportValues_Section2", dbOpenDynaset)
strHolder = db.Name
strHolder = Left(strHolder, Len(strHolder) - Len(Dir(strHolder)))
Open strHolder & "DataFlatFile" & Format(Date, "_mm-dd-yy") & ".txt" For
Output As #1
With rs1
strHolder = ""
'Get Field Names
For intCounter = 0 To .Fields.Count - 1
If intCounter <> 1 Then
strHolder = strHolder & Left$(.Fields(intCounter).Name, 8) &
strFldDelimiter
End If
Next intCounter
For intCounter = 0 To rs2.Fields.Count - 1
If intCounter > 1 Then
strHolder = strHolder & Left$(rs2.Fields(intCounter).Name, 8) &
strFldDelimiter
End If
Next intCounter
'Write Field Names to file
strHolder = Left(strHolder, Len(strHolder) - Len(strFldDelimiter))
Print #1, strHolder
'Get field contents for each record and write to file
Do While Not .EOF
'get matching line of data in secondary file
strHolder = "SurveyDocId ='" & .Fields("SurveyDocID") & "'"
rs2.FindFirst strHolder
strHolder = vbNullString
For intCounter = 0 To .Fields.Count - 1
If intCounter <> 1 Then
strTemp = .Fields(intCounter) & ""
strTemp = ReplaceString(strTemp, vbTab, " ") 'Strip Tabs
strTemp = ReplaceString(strTemp, vbCrLf, " ") 'Strip C/R
'----Surround with Quotes if appropriate
If strFldDelimiter <> "|" Then
If InStr(1, strTemp, ",") > 0 Then
strTemp = Chr(34) & strTemp & Chr(34)
End If
End If
strHolder = strHolder & strTemp & strFldDelimiter
End If
Next intCounter
For intCounter = 0 To rs2.Fields.Count - 1
If intCounter > 1 Then
strTemp = rs2.Fields(intCounter) & ""
strTemp = ReplaceString(strTemp, vbTab, " ")
strTemp = ReplaceString(strTemp, vbCrLf, " ")
If InStr(1, strTemp, ",") > 0 Then
strTemp = Chr(34) & strTemp & Chr(34)
End If
strHolder = strHolder & strTemp & strFldDelimiter
End If
Next intCounter
strHolder = Left$(strHolder, Len(strHolder) - Len(strFldDelimiter))
Print #1, strHolder
.MoveNext
Loop
End With 'rs1
Beep
funExportToTabDelimited_Exit:
Close #1
rs1.Close
rs2.Close
db.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing
Exit Function
funExportToTabDelimited_Error:
End Function
As far as I know the Cross tab query is unique to Access. MS SQL may have
something like it in the later versions.
[quoted text clipped - 74 lines]