Table field names to appear alphabetically

S

Simon S

Hi,

I have just started doing queries in access on our accounting database and I
would like to know is there a way I can get the tables in the query to list
all the fields alphabetically. It does it automatically when I do a query
using excel

Thanks
 
J

John W. Vinson

Hi,

I have just started doing queries in access on our accounting database and I
would like to know is there a way I can get the tables in the query to list
all the fields alphabetically. It does it automatically when I do a query
using excel

Thanks

The order of fieldnames in a table - or a query - should be irrelevant. The
Query will reflect the order of fieldnames in the table (which you can
alphabetize manually), but you should not be looking at either table *or*
query datasheets in any case.

Tables are for storing data; queries are for selecting and sorting data; Forms
are for viewing and editing data; Reports are for printing. You can lay out
the controls in any order you like - alphabetically, by importance, in logical
groups, whatever - on a form or report.

Could you explain how your tables are structured, and what benefit you expect
from alphabetizing fieldnames?
 
S

Simon S

They are linked tables to the accounting software and tables often contain
over 100 fields. I select the fields to include in my query in a particular
order e.g.
ITM_NO (the item number), ITM_DES (the item no's description), ITM_PRESUP
(preferd supplier), ITM_USER1 (user information), ITM_CAT (the product
category) etc. I normally select 45 fields to be included in the query.
When I do a query using microsoft query in excel it lists the fields in the
table alphabetically e.g
ITM_BUY
ITM_CAT
ITM_CLASS
ITM_CREATED
ITM_PRESUP
ITM_USER1
etc (this makes it much eaiser (because they are in alphabetical order) to
select the fields from the table to include in the query.

The table in access has the fields in no particular order e.g.
ITM_CREATED
ITM_BUY
ITM_PRESUP
ITM_CLASS
ITM_USER1
ITM_CAT
and so on, I just want to know if I can get access to display trhe fields in
the table alphabetically.
 
J

John Spencer

The only way to display the fields in alpha order is to move them around in
the table. You can drag and drop fields in table design view to rearrange
them. But since you are linking to accounting software I would guess that is
not allowed.

An option is to build base queries with the fields in alpha order and then use
that base query for all your queries.

So if you had a table ITEMS, you would create a query named qItems and add the
fields to that query in the desired order. Do not apply any criteria to this
query.

Then in all your new queries, you could use qItems instead of the Items table.


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
S

Simon S

Thanks

John Spencer said:
The only way to display the fields in alpha order is to move them around in
the table. You can drag and drop fields in table design view to rearrange
them. But since you are linking to accounting software I would guess that is
not allowed.

An option is to build base queries with the fields in alpha order and then use
that base query for all your queries.

So if you had a table ITEMS, you would create a query named qItems and add the
fields to that query in the desired order. Do not apply any criteria to this
query.

Then in all your new queries, you could use qItems instead of the Items table.


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County


.
 
P

PieterLinden via AccessMonster.com

<SNIP>An option is to build base queries with the fields in alpha order and
then use
that base query for all your queries.</SNIP>

Here's one way to crank out the queries automatically with all the fields
included in alpha order...

Option Compare Database
Option Explicit


Function ShowAllTables(Optional bShowFieldsToo As Boolean)
'Purpose: List the tables (and optionally their fields) using ADOX.
Dim cat As New ADOX.catalog 'Root object of ADOX.
Dim tbl As ADOX.Table 'Each Table in Tables.
Dim col As ADOX.Column 'Each Column in the Table.

'Point the catalog to the current project's connection.
Set cat.ActiveConnection = CurrentProject.Connection

'Loop through the tables.
For Each tbl In cat.Tables
Debug.Print tbl.name, tbl.Type
If bShowFieldsToo Then
'Loop through the columns of the table.
For Each col In tbl.Columns
Debug.Print , col.name, col.Type
Next
Debug.Print "--------------------------------"
'Stop
End If
Next

'Clean up
Set col = Nothing
Set tbl = Nothing
Set cat = Nothing
End Function

Private Function ShowColumsOfTable(ByVal strTable As String) As String
'tweak of Allen Browne's code for ShowAllTables...
'found here: http://www.allenbrowne.com/func-ADOX.html

'Purpose: return an alphabetized list of columns
Dim cat As New ADOX.catalog
Dim tbl As ADOX.Table
Dim col As ADOX.Column
Dim strFieldList As String ' temp variable to accumulate field list into

'Point the catalog to the current project's connection
Set cat.ActiveConnection = CurrentProject.Connection

Set tbl = cat.Tables(strTable)

'Columns are already sorted... append them to a string
For Each col In tbl.Columns
strFieldList = strFieldList & "[" & col.name & "], "
Next col

' trim off trailing comma-space
ShowColumsOfTable = Left$(strFieldList, Len(strFieldList) - 2)
End Function

Public Function CreateQueryWithAlphaColumns(ByVal strTable As String) As
String
Dim strSQL As String
Dim strFieldList As String
strFieldList = ShowColumsOfTable(strTable)
strSQL = "SELECT " & strFieldList & " FROM [" & strTable & "];"
CreateQueryWithAlphaColumns = strSQL

'you could turn this into a sub and create a querydef using DAO here...
End Function


Function CreateQueryDAO(ByVal strTable As String)
'Purpose: How to create a query
'Note: Requires a table named MyTable.
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strQueryName As String

'assuming no spaces in table name
strQueryName = "qryAlpha_" & strTable
Set db = CurrentDb()

'The next line creates and automatically appends the QueryDef.
Set qdf = db.CreateQueryDef(strQueryName)

'Set the SQL property to a string representing a SQL statement.
qdf.SQL = CreateQueryWithAlphaColumns(strTable)

'Do not append: QueryDef is automatically appended!

Set qdf = Nothing
Set db = Nothing
Debug.Print strQueryName & " created."
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top