[AccessD] rsR("order") vs rsR!Order

Darryl Collins darryl at whittleconsulting.com.au
Mon Aug 1 01:35:37 CDT 2011



" can also use rs(0), rs(1) etc if you  are a masochist"

Hehehe, yeah, that would be my take on that approach too.  Not to mention I
find it risky.  What if the fields get rearranged, then suddenly the code
will return something unexpected.

Many thanks for taking the time to explain that.  Guess it is all pretty
clear and simple.  It is funny how you get used to a certain style.  I can
scan read the code with the ! syntax and understand immediately what is
happening in Access, where I really need to think about it when reading the
other syntax.  Part of the issue is the code is based in an Excel VBA /
workbook which talks to Access. So there is Excel VBA in there too which
looks very similar - so it is hard to just work out at a glance who is who
in the code zoo.  Ah well..

Cheers
Darryl

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Stuart McLachlan
Sent: Monday, 1 August 2011 4:06 PM
To: Access Developers discussion and problem solving
Subject: Re: [AccessD] rsR("order") vs rsR!Order

The advantage of rs("fieldname") is that it  also allows you to do things
like:

If something = true then 
  strField = "field1" 
else 
  strField = "field2"
end if
rs(strField) = strValue.

The disadvantage it that intellisense can't help you and a type can cause
errors.

Note that you can also use rs(0), rs(1) etc if you  are a masochist.

As to why it works - the same reason that the same syntax works with other
collections such 
as Forms, Reports, References etc.  It's just the way such objects are
designed:


References!referencename The referencename argument is the name of the 
Reference object. 
References("referencename") The referencename argument is the name of the 
Reference object. 
References(index) The index argument is the object's numerical position
within the 
collection.

-- 
Stuart

On 1 Aug 2011 at 13:28, Darryl Collins wrote:

> Hi guys & Gals,
> 
> Slower day at work today so I was poking around some code they use
> here in my new role and found this syntax when dealing with recordsets
> in Access VBA
> 
> rsR.AddNew
> rsR("order") = rsM("order")
> rsR("sheetname") = rsM("sheetname")
> rsR("sheetnumber") = rsM("sheetnumber")
> rsR.Update
> 
> It is very, ummm, MS Excel in style, but it does work ok and update
> the recordset(s) correctly. However I would have written it like:
> 
> With rsR
>  .AddNew
>       !order = rsM!order
>  !sheetname = rsM!sheetname
>  !sheetnumber = rsM!sheetnumber
>  !Update
> End with
> 
> Not withstanding then with / end with bit.  What is the advantage (if
> any) of one syntax over the other?  Is one method faster? Actually,
> Why does the first syntax even work?  I would have though you would
> have had to use the ! method, but very clearly I am totally wrong on
> that count.
> 
> I had not seen code used like that before for MS Access recordsets. 
> Maybe I need to get out more?
> 
> Your thoughts?
> 
> Cheers
> Darryl
> 
> -- 
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
> 



-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com




More information about the AccessD mailing list