This solution works, but it is not very eloquent.
1) tell the procedure how many rows you want.
2)calculate the total records
3) divide it by the number of desired rows to determine columns
4)create a new table with the desired columns (delete old table if it exists).
5)fill the table down and then across
6) open the form and set the recordsource to the new table
You could build the form dynamically, but that is another post.
This code works with any table: Provide a table name and field name.
Example. Northwind database with Table "Orders" and field "OrderID". The data looks like
[tt]
Order ID
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
....
11077
[/tt]
There is 847 records. It produces the tblSpreadSheet with 42 columns. Something like
[tt]
OrderID1 OrderID2 OrderID3 OrderID4 ............OrderID42
10249 10269 10289 10309 11076
10250 10270 10290 10310 11077
10251 10271 10291 10311
10252 10272 10292 10312
10253 10273 10293 10313
10254 10274 10294 10314
10255 10275 10295 10315
10256 10276 10296 10316
10257 10277 10297 10317
10258 10278 10298 10318
10259 10279 10299 10319
10260 10280 10300 10320
10261 10281 10301 10321
10262 10282 10302 10322
10263 10283 10303 10323
10264 10284 10304 10324
10265 10285 10305 10325
10266 10286 10306 10326
10267 10287 10307 10327
10268 10288 10308 10328
[/tt]
Code:
call the procedure
Public Sub test()
createSpreadSheet "Orders", "OrderID", 20
End Sub
code
Code:
Public Sub createSpreadSheet(tblName As String, fldName As String, intRecordsDisplayed As Integer)
Dim tblSpreadSheet As DAO.TableDef
Dim rsSpreadsheet As DAO.Recordset
Dim fld As DAO.Field
Dim rs As DAO.Recordset
Dim lngRecords As Long
Dim intFields As Integer
Dim fldType As Long
Dim intCount As Integer
Set rs = CurrentDb.OpenRecordset(tblName, dbOpenDynaset)
fldType = rs.Fields(fldName).Type
If Not (rs.BOF And rs.EOF) Then
rs.MoveFirst
rs.MoveLast
End If
lngRecords = rs.RecordCount
intFields = Fix(lngRecords / intRecordsDisplayed) + 1
If tblExists("tblSpreadSheet") Then
CurrentDb.TableDefs.Delete ("tblSpreadSheet")
End If
Set tblSpreadSheet = CurrentDb.CreateTableDef("tblSpreadSheet")
For intCount = 1 To intFields
'Debug.Print intFields
Set fld = tblSpreadSheet.CreateField(fldName & intCount)
tblSpreadSheet.Fields.Append fld
Set fld = tblSpreadSheet.Fields(fldName & intCount)
fld.Type = fldType
Next intCount
CurrentDb.TableDefs.Append tblSpreadSheet
Set rsSpreadsheet = CurrentDb.OpenRecordset("tblSpreadSheet", dbOpenDynaset)
For intCount = 1 To intRecordsDisplayed
rsSpreadsheet.AddNew
rsSpreadsheet.Update
Next intCount
intCount = 0
rs.MoveFirst
rsSpreadsheet.MoveFirst
Do While Not rs.EOF
If rs.AbsolutePosition Mod (intRecordsDisplayed) = 0 Then
rsSpreadsheet.MoveFirst
intCount = intCount + 1
Debug.Print rs.AbsolutePosition
End If
rsSpreadsheet.Edit
rsSpreadsheet.Fields(fldName & intCount) = rs.Fields(fldName)
rsSpreadsheet.Update
rsSpreadsheet.MoveNext
rs.MoveNext
Loop
End Sub
Public Function tblExists(strTblName As String) As Boolean
Dim tdf As TableDef
For Each tdf In CurrentDb.TableDefs
If tdf.Name = strTblName Then
tblExists = True
End If
Next tdf
End Function
This seems overly complicated, there is probably a better way to do this. Actually, now that I posted that I think you could do this with a crosstab query. I will give it a try.