I have an Access form with a datasheet subform. This datasheet subform is displaying a dynamically created recordset (a pivot), and is using some VBA to dynamically load that pi
The problem was that Access manually fills in the form's record source property with invalid SQL when using a parameterized recordset as source.
The solution I used was to set the record source property, instead of the recordset property:
Public Function LoadSQL(sqlString As String)
Dim myRS As DAO.RecordSet
Set myRS = CurrentDb.OpenRecordset(sqlString)
Dim i As Long
Dim myTextbox As textbox
Dim fld As Field
i = 0
With myRS
For Each fld In myRS.Fields
Set myTextbox = Me.Controls("Text" & i)
myTextbox.Properties("DatasheetCaption").Value = fld.NAME
myTextbox.ControlSource = fld.NAME
myTextbox.ColumnHidden = False
i = i + 1
Next fld
End With
For i = i To 255
Set myTextbox = Me.Controls("Text" & i)
myTextbox.ColumnHidden = True
Next i
Me.RecordSource = sqlString
End Function
The somewhat ridiculous part is that the code I provided in the question did block sorting on the form. But not early enough to keep Access from crashing, apparently.