Link to home
Start Free TrialLog in
Avatar of wipnav
wipnav

asked on

Autofit column widths on subform in datasheet view

I found this website which describes how to autofit the columns of a datasheet for a query or a table. I have a datasheet subform that is based off of a query that I am trying to auto fit the columns on, but I am having difficulty trying to use the code in my situation.

Here is the site: http://msdn.microsoft.com/en-us/library/Aa217449

Below are the functions listed in the article.

Is it possible to do this for a datasheet that is in a subform?

Public Function FixColumnWidthsOfQuery _
               (stName As String)
    Dim db As Database
    Dim qdf As QueryDef
    Dim fld As DAO.Field
    Dim frm As Form
    Dim ictl As Integer
    Dim ctl As control
    
    Set db = CurrentDb
    Set qdf = db.QueryDefs(stName)
    DoCmd.OpenQuery stName, acViewNormal
    Set frm = Screen.ActiveDatasheet
    For ictl = 0 To frm.Controls.Count - 1
     Set ctl = frm.Controls(ictl)
     ctl.ColumnWidth = -2
     Call SetDAOFieldProperty(qdf.Fields(ictl), _
      "ColumnWidth", ctl.ColumnWidth, dbInteger)
    Next ictl
    DoCmd.Close acQuery, stName, acSaveYes
End Function

Public Function FixColumnWidthsOfTable _
                      (stName As String)
    Dim db As Database
    Dim tdf As TableDef
    Dim fld As DAO.Field
    Dim frm As Form
    Dim ictl As Integer
    Dim ctl As control
    
    Set db = CurrentDb
    Set tdf = db.TableDefs(stName)
    DoCmd.OpenTable stName, acViewNormal
    Set frm = Screen.ActiveDatasheet
    For ictl = 0 To frm.Controls.Count - 1
     Set ctl = frm.Controls(ictl)
     ctl.ColumnWidth = -2
     Call SetDAOFieldProperty(tdf.Fields(ictl), _
      "ColumnWidth", ctl.ColumnWidth, dbInteger)
    Next ictl
    DoCmd.Save acTable, stName
End Function

Private Sub SetDAOFieldProperty(fld As DAO.Field, stName As String, vValue As Variant, lType As Long)
    Dim prp As DAO.Property
    
    For Each prp In fld.Properties
        If StrComp(prp.Name, stName, _
         vbBinaryCompare) = 0 Then
            prp.Value = vValue
            Exit For
        End If
        Set prp = Nothing
    Next prp
    
    If prp Is Nothing Then
        Set prp = fld.CreateProperty(stName, _
         lType, vValue)
        fld.Properties.Append prp
    End If
End Sub

Open in new window

Avatar of ldunscombe
ldunscombe
Flag of Australia image

See if this is what you're after

http://www.lebans.com/autocolumnwidth.htm

Leigh
ASKER CERTIFIED SOLUTION
Avatar of msacc97
msacc97
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wipnav
wipnav

ASKER

Thanks, that was much simpler than I thought! I just looped through all of the controls in the from and set their column width and it works like a charm.

    For Each cCon In Me![frmSearchmaster subform].Controls
        If cCon.ControlType = 109 Then
            Set tb = cCon
            tb.ColumnWidth = -2
        End If
    Next