Results 1 to 13 of 13
  1. #1
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618

    Count fields in a table

    I found ways on the internet to get the result counting the fields of one table.
    I do have a query that lists all tables.
    I would appreciate to get an exact expression in a query that returns the number of fields per table.


    VBA that returns the result will be good.
    Click image for larger version. 

Name:	230615a.png 
Views:	9 
Size:	20.1 KB 
ID:	50369

  2. #2
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You can use a simple VBA function like this:
    Code:
    Public Function fnCountFields (sTable as string) as IntegerDim tdf as DAO.TableDef,db as DAO.Database
    Set db=CurrentDb
    Set tdf=db.TableDefs(sTable)
    
    
    fnCountFields=tdf.Fields.Count
    
    
    Set tdf=nothing
    Set db=Nothing
    End Function

    You would use it in your query:
    FieldCount:fnCountFields ([TableNameB])

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Consider:

    Function CountFields(sT As String) As Integer
    CountFields = CurrentDb.TableDefs(sT).Fields.Count
    End Function
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    I tend to always use a variable for CurrentDb as I was getting unexpected things happening with using CurrentDb directly, but it should work in most cases...
    https://stackoverflow.com/questions/...using-currentd

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I would appreciate to get an exact expression in a query that returns the number of fields per table.
    I'm thinking that what is wanted is a way to loop over all tables and count for each?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Click image for larger version. 

Name:	230615b.png 
Views:	12 
Size:	15.2 KB 
ID:	50370Click image for larger version. 

Name:	230615c.png 
Views:	12 
Size:	39.6 KB 
ID:	50371

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Let's see the query SQL. Is it pulling from MSysObjects? In which case there is a field that can be referenced in the function call to pass table name.

    Fix the VBA code so DIM line not on procedure declaration line. That's a posting error caused by forum. For some reason it drops carriage return between first two lines. I basically have to correct this every time I post code.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    @Perfac: click after Public Function fnCountFields (sTable as string) as Integer and press enter, the Dim tdf as DAO.TableDef,db as DAO.Database
    should be on a new line.

    Seeing your table design you could probably convert this Select query into a Update query to update the FieldsCountB field with fnCountFields ([TableNameB]):

    UPDATE t9CountTables SET
    FieldsCountB = fnCountFields ([TableNameB])

    To do this open your Select query in design view, change the view to SQL and copy and paste from above (after you fix the function in VBA).

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  9. #9
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Thanks.

    SELECT MSysObjects.Name AS TableNameA, "Table" AS ObjectB, 127 AS Acobty_IDc, 1 AS RecordsTypeC, 2 AS RectypIDbFROM MSysObjects
    WHERE (((MSysObjects.Name) Not Like "f_*") AND ((MSysObjects.Type) In (1,4,6)) AND ((Left([Name],4))<>"MSys"));

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Reference MSysObjects.Name in function call.

    Why does query show alias name TableNameA but image of output shows TableNameB?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    I have a separate table where all object names are stored. There is a reason why the query was very slow. I appended it to a table. I also keep a separate table with all the 3000 field names, since I try to never give a second field the same name than another. My issue here is solved, thank you.

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by Perfac View Post
    Click image for larger version. 

Name:	230615b.png 
Views:	12 
Size:	15.2 KB 
ID:	50370Click image for larger version. 

Name:	230615c.png 
Views:	12 
Size:	39.6 KB 
ID:	50371
    Pleas try and understand the code you are supplied with.
    How is that you copied and pasted ever going to work.

    I was thinking of mentioning it, but then thought 'better not, as it is obvious'
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    I am not sure about the right word for my condition. But sorry for sometimes being stupid.
    Having been an international chess champion 40 years ago, an accounting degree and having employed a 1000 people over 32 years just don't improve the condition.
    This last one, I was just fast asleep, should have seen that.

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

Similar Threads

  1. Replies: 12
    Last Post: 03-27-2020, 09:46 AM
  2. Replies: 2
    Last Post: 03-09-2017, 04:50 PM
  3. Replies: 23
    Last Post: 01-27-2016, 01:42 PM
  4. Replies: 4
    Last Post: 11-12-2015, 01:15 PM
  5. Count fields
    By hithere in forum Reports
    Replies: 7
    Last Post: 04-22-2012, 02:15 PM

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