Cancel sorting or disallow sorting on Access form

前端 未结 1 902
猫巷女王i
猫巷女王i 2021-01-21 18:20

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

1条回答
  •  梦毁少年i
    2021-01-21 18:42

    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.

    0 讨论(0)
提交回复
热议问题