how do I change column width on table datasheet with VBA?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Deekay
    New Member
    • May 2010
    • 21

    how do I change column width on table datasheet with VBA?

    Help !! My code below generates error 425 'object required'. (I'm using Access 2003)
    I have a table opened as a datasheet, but the column widths need adjusting programatically to fit. I have seen posts about doing this on datasheets on forms or subforms - but can I do it this way on a table opened as a datasheet? The application is being distributed as an .mde file to general users with only custom menus showing. I can't therefore use any 'sendkeys' operation as the format menu won't be showing.
    This is my code where 'tblname' is the name of the table.
    Code:
    Dim db as DAO.Database
    Dim td as DAO.TableDef
    Dim col as DAO.Field
    Dim tblname, colname as String
    set db as CurrentDb
    ...more code to identify 'td' as the targeted table (because the name might vary) and then
    Code:
    tblename = td.Name 
    DoCmd.OpenTable tblname, acViewNormal, ReadOnly
    For each col in td.fields
        colname = col.name
        Table!tblname.Field.colname.ColumnWidth = -2
    Next
    'tblname' is returning the correct table name and 'colname' is returning the correct fields/column names.
    The error is generated at line 5 in the second section of code. I have tried variations on addressing the column width setting, all without success. What am I doing wrong?
    Any ideas please
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    I don't think there's any formatting that can be done to a table using VBA, for a very simple reason. Tables should never be made directly available to end users! The data should only be accessed by users by way of forms or queries. You can use the Form Wizard to create a form from your table, in Datasheet View, in less than a minute.

    Linq ;0)>

    Comment

    • Deekay
      New Member
      • May 2010
      • 21

      #3
      Thanks for this suggestion which I had considered. I was coming to the conclusion that this would be the case. I was trying to avoid this since I don't know the name of the table until runtime - although the variable 'tblname' picks it up.
      Can I assign the record source of such a form from another form's VBA script and what is the syntax having assigned no record source upon opening? Is it

      set Forms!name_of_n ew_form.RecordS ource = tblname

      Presumably this has to be set after not before the form has opened?

      Thanks

      Comment

      Working...