Hi guys
I have a query that I am using to create a list of customers details that I
would like to display on a form without using CBF. I would like to write a
select query that list the field names as in:
SELECT [AllFieldNames] FROM qryCustDetails;
I know that you can get the objets name from the MsysObjects table and the
field name from the MsysQueries table, I just can't find a link between
them.
Any help appreciated
Tony
Ron Weiner - 21 Jan 2005 12:22 GMT
Tony
I am not even sure what you want to do is possible. For instance how could
Access tell what field names would be returned from a crosstab query? Here
is a can't fail way of doing what you want using a recordset and iterating
through its fields collection.
Public Sub GetFieldNames(strQueryName)
' Get names of all Columns a query will return
Dim rs As ADODB.Recordset, fld As ADODB.Field
Set rs = New ADODB.Recordset
rs.Open strQueryName, CurrentProject.Connection
For Each fld In rs.Fields
Debug.Print fld.Name
Next
rs.Close
Set rs = Nothing
End Sub
Ron W
> Hi guys
>
[quoted text clipped - 11 lines]
>
> Tony
Graham R Seach - 21 Jan 2005 12:56 GMT
Tony,
Funny, I wrote this just today.
Public Function GetFieldNames(sQueryName As String) As String
Dim qdf As DAO.QueryDef
Dim fld As DAO.Field
Dim sSQL As String
Set qdf = CurrentDb.QueryDefs(sQueryName)
For Each fld In qdf.Fields
'SELECT [AllFieldNames] FROM qryCustDetails;
sSQL = sSQL & "[" & fld.Name & "], "
Next fld
sSQL = Left(sSQL, Len(sSQL) - 2)
sSQL = "SELECT " & sSQL & " FROM qryCustDetails"
GetFieldNames = sSQL
Set fld = Nothing
Set qdf = Nothing
End Function
Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------
> Hi guys
>
[quoted text clipped - 11 lines]
>
> Tony
Jeff Conrad - 21 Jan 2005 16:38 GMT
Graham,
To be used in other places shouldn't this line of code:
sSQL = "SELECT " & sSQL & " FROM qryTable1"
be.....
sSQL = "SELECT " & sSQL & " FROM " & sQueryName
or was your code to provide the exact syntax for just the
query the OP mentioned?

Signature
Jeff Conrad
Access Junkie
Bend, Oregon
> Tony,
>
[quoted text clipped - 40 lines]
> >
> > Tony
Graham R Seach - 22 Jan 2005 00:10 GMT
Picky, picky, picky! :-)
Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------
> Graham,
>
[quoted text clipped - 55 lines]
>> >
>> > Tony
Jeff Conrad - 22 Jan 2005 00:10 GMT
Just checking.
:-)

Signature
Jeff Conrad
Access Junkie
Bend, Oregon
> Picky, picky, picky! :-)
>
[quoted text clipped - 63 lines]
> >> >
> >> > Tony
The Goulds - 24 Jan 2005 19:14 GMT
Wow - so lessee, the query forms a list based on an existing query?
> Picky, picky, picky! :-)
>
[quoted text clipped - 64 lines]
>>> >
>>> > Tony
Graham R Seach - 25 Jan 2005 10:47 GMT
<<...the query forms a list based on an existing query?>>
Yep.
Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------
> Wow - so lessee, the query forms a list based on an existing query?
>
[quoted text clipped - 66 lines]
>>>> >
>>>> > Tony
The Goulds - 25 Jan 2005 15:40 GMT
Thanks Graham - I suspect the code can be modified to pull from tables as
opposed to queries?
Mike
> <<...the query forms a list based on an existing query?>>
> Yep.
[quoted text clipped - 76 lines]
>>>>> >
>>>>> > Tony
Tony Wainwright - 29 Jan 2005 23:17 GMT
Please show me how - that's what I'm looking for
> Thanks Graham - I suspect the code can be modified to pull from tables as
> opposed to queries?
[quoted text clipped - 81 lines]
>>>>>> >
>>>>>> > Tony
Graham R Seach - 30 Jan 2005 10:08 GMT
Tony,
Just change "QueryDef" and "QueryDefs", to "TableDef" and "TableDefs",
respectively.
Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------
> Please show me how - that's what I'm looking for
>> Thanks Graham - I suspect the code can be modified to pull from tables as
[quoted text clipped - 82 lines]
>>>>>>> >
>>>>>>> > Tony
JohnFol - 21 Jan 2005 13:17 GMT
I think you want this
SELECT MSysQueries.Expression
FROM MSysObjects INNER JOIN MSysQueries ON MSysObjects.Id =
MSysQueries.ObjectId
WHERE (((MSysObjects.Name)="qryCustDetails") AND
((MSysQueries.Expression)<>""));
> Hi guys
>
[quoted text clipped - 11 lines]
>
> Tony
Tony Wainwright - 21 Jan 2005 13:23 GMT
Thanks John
>I think you want this
>
[quoted text clipped - 19 lines]
>>
>> Tony
The Goulds - 24 Jan 2005 20:34 GMT
ummm...my version of these files did not contain all the tables w/in my
database
>I think you want this
>
[quoted text clipped - 19 lines]
>>
>> Tony
Graham R Seach - 25 Jan 2005 11:13 GMT
Unfortunately John, that query may return inconsistent values, because
sometimes MSysQueries.Expression contains a criteria expression.
Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------
>I think you want this
>
[quoted text clipped - 19 lines]
>>
>> Tony
The Goulds - 24 Jan 2005 19:24 GMT
what is CBF?
> Hi guys
>
[quoted text clipped - 11 lines]
>
> Tony
The Goulds - 24 Jan 2005 20:28 GMT
ahh...Code Behind Forms.
> what is CBF?
>
[quoted text clipped - 13 lines]
>>
>> Tony