dbTalk Databases Forums  

inserting as variable

comp.databases.ms-access comp.databases.ms-access


Discuss inserting as variable in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
christianlott1@yahoo.com
 
Posts: n/a

Default inserting as variable - 07-08-2010 , 05:06 PM






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



(I really want to use this in a loop, so this is just a simple
example..)

thanks

Reply With Quote
  #2  
Old   
Bob Barrows
 
Posts: n/a

Default Re: inserting as variable - 07-08-2010 , 05:19 PM






christianlott1 (AT) yahoo (DOT) com wrote:
Quote:
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

--
HTH,
Bob Barrows

Reply With Quote
  #3  
Old   
christianlott1@yahoo.com
 
Posts: n/a

Default Re: inserting as variable - 07-08-2010 , 05:47 PM



Thanks!

Reply With Quote
  #4  
Old   
David W. Fenton
 
Posts: n/a

Default Re: inserting as variable - 07-09-2010 , 07:18 PM



"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/

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.