MS Access: Hide columns with Access VBA or VB Script

I needed to hide specified columns in an access table by clicking a button in the form through VBA code. I could do with the below code.
Public Sub SetColumnHidden()

    Dim dbs As DAO.Database
    Dim fld As DAO.Field
    Dim prp As DAO.Property
    Const conErrPropertyNotFound = 3270

    ' Turn off error trapping.
    On Error Resume Next

    Set dbs = CurrentDb
   
    ' Set field property.
    Set fld = dbs.TableDefs!Products.Fields!ProductID
    fld.Properties("ColumnHidden") = True
   
    ' Error may have occurred when value was set.
    If Err.Number <> 0 Then
        If Err.Number <> conErrPropertyNotFound Then
            On Error GoTo 0
            MsgBox "Couldn't set property 'ColumnHidden' " & _
                   "on field '" & fld.Name & "'", vbCritical
        Else
            On Error GoTo 0
            Set prp = fld.CreateProperty("ColumnHidden", dbLong, True)
            fld.Properties.Append prp
        End If
    End If
   
    Set prp = Nothing
    Set fld = Nothing
    Set dbs = Nothing
   
End Sub

Source: Microsoft
http://msdn.microsoft.com/en-us/library/office/aa224064(v=office.11).aspx
http://msdn.microsoft.com/en-us/library/office/ff194134(v=office.14).aspx

No comments:

Post a Comment