Daniel said:
In your example, your query is a simple SELECT query where all the fields
come from the same table so yes, they both work fine. One is implicit and
the other explicit. However, in the case, for example, of a join query where
you can have fields from multiple sources, join tableA with tableB and return
me some fields from A and other from B, you would need to explicitly use the
tablename.fieldname format. It depends on your query.
I try to protect myself from joins on two tables where a field might be
added to, say, the second table that is already in the first table
causing a dynamically created SQL string to go awry because of the
resulting field name ambiguity. My concern is greater when the joined
tables come from separate databases. I wrote the following function:
'---Begin module code---
Public Function IsFieldInTableDef(strFieldName As String, strTableName
As String, Optional strDatabase As String) As Boolean
Dim MyDB As DAO.Database
Dim tdf As TableDef
Dim fld As Field
Dim boolFound As Boolean
If strDatabase = "" Then
Set MyDB = CurrentDb
Else
Set MyDB = OpenDatabase(strDatabase, False, True)
End If
Set tdf = MyDB.TableDefs(strTableName)
boolFound = False
For Each fld In tdf.Fields
If fld.Name = strFieldName Then
boolFound = True
Exit For
End If
Next fld
IsFieldInTableDef = boolFound
'The following line is not absolutely required
Set tdf = Nothing
MyDB.Close
Set MyDB = Nothing
End Function
'----End module code----
Then I check fields I am unsure about (usually in the remote database)
and prepend the table name where it's needed. I like using just the
field name whenever possible. Alternatively, you can skip using the
function altogether and always prepend the table name to be safe when
you're unsure

. The function is also useful in situations where a
join may or may not be needed in a dynamically generated SQL string
depending on which fields are required.
James A. Fortune
[email protected]