Results 1 to 6 of 6
  1. #1
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154

    Autofit Columns in Table

    is there a way to autofit the columns of a table in Access using VBA?

  2. #2
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    No. Why would you need to do this ? A table is for holding data, not displaying or working with it.
    That's what you use forms for.

    On a data sheet form you can use

    Code:
    me.<fieldName>.ColumnWidth = -2
    
    
    to autofit that column.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154
    Hi Minty,

    I am afraid I have to say I disagree with you there.
    Say you have a table with 37 Columns and 1000 records. And you need to look at least the first 10 columns before you decided to amend values in the other columns.
    You are telling me that it is wrong/inefficient to look at a table where everything is on one screen and where you can scroll up and down to find records that look odd? You are saying you would use a form to do this? Ok, well how will you spot odd values using a form?

  4. #4
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Disagree all you want. Access tables aren't spreadsheets. They are data storage tables.

    For occasional background "fiddling" as the database admin I'm sure we have all delved straight into a table, to look at things, but if you need to do it vaguely regularly, build a datasheet form with just the columns that matter. Make it so you can only edit the fields that should be changed. If you deleted a FK field by mistake whilst in the table how would you ever tie it back to the main record?
    You can also build in filters and search boxes to narrow down your search without risk of accidentally deleting your important data, by leaning on the delete key.

    If you ever switch over to an enterprise level DBMS such as SQL Server it's actually pretty difficult to do what you are currently doing.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  5. #5
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    mp3909,

    Minty isn't making things up here. It is widely accepted that best practices say that users should NEVER have direct access to the tables (too much chance for danger and to bypass important checks, as Minty pointed out).
    All access to data should be controlled via forms.

    Note if your main hang-up is that you want it to have a similar structure as what is seen in the table, take a look at creating a Datasheet view for your form.
    See here for pcitures of different layouts you can do for your form: http://www.excel-networks.com/docs/m...20Examples.pdf

  6. #6
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    FWIW, agree with last 2 opinions but that's not the only reason for me posting here.

    When you're looking at a table and double click to expand a field width, it's only as good as what's in the current view anyway. Move down a page, and any record where a field exceeds the new width isn't wide enough again. Thus even if you could do so via vba, it would seem to be dependent on the current view. Ever filtered or sorted a table and saved it before closing? You just applied a view that may not be wanted next time, so there's another reason not to play in them too much.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 10-24-2017, 05:16 PM
  2. Replies: 2
    Last Post: 10-26-2015, 06:14 AM
  3. AutoFit Text size to display in a form textbox
    By QA_Compliance_Advisor in forum Programming
    Replies: 6
    Last Post: 08-08-2015, 05:00 PM
  4. how to AUTOFIT columns
    By florida2001 in forum Access
    Replies: 3
    Last Post: 04-07-2014, 02:16 PM
  5. Excel Automation Autofit Column (or Cell?) Width
    By orcinus in forum Programming
    Replies: 3
    Last Post: 02-09-2011, 11:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums