"Bob Barrows" <reb01501 (AT) NOyahoo (DOT) SPAMcom> wrote in
news:i15fdo$q53$1 (AT) news (DOT) eternal-september.org:
Quote:
christianlott1 (AT) yahoo (DOT) com wrote:
How do I get past this problem, where I want to place a variable
where it's expecting the exact name:
strTable = "Query1"
Set rs = CurrentDb.QueryDefs!(strTable).OpenRecordset
Get rid of the bang operator. This should work
Set rs = CurrentDb.QueryDefs(strTable).OpenRecordset |
Just to explain why, so it doesn't seem arbitrary:
The ! is a shortcut for using an object's default collection. When
you use Me!MyControl on a form or report, you're using the default
collection (in forms and reports it is a union of the controls and
fields collections). In a recordset, the default collection is the
Fields collection, so rs!NameOfAField is a way to directly refer to
a particular field.
An alternative way is:
rs.Fields("NameOfAField")
....which can be abbreviated:
rs("NameOfAField")
For the object collections (TableDefs, QueryDefs, etc.), you can use
the bang when you know the name:
CurrentDB.TableDefs!NameOfATable
....which is an equivalent to:
CurrentDB.TableDefs("NameOfATable")
....and when you have a variable, you can pass it instead of the
literal string:
CurrentDB.TableDefs(strTableName)
So, you can use a bang when you know the exact name, and the () for
items in the collection when you don't know it and need to use a
variable.
--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/