0

How do I select the actual variable if the recordset is JOINed from to tables?

 Dim db As DAO.Database
 Set db = CurrentDb
 Dim rs As DAO.Recordset
 Set rs = Currentdb.OpenRecordset("SELECT tableA.data, tableB.data “ & _
                                  “FROM tabelA INNER JOIN tableB ON tableA.ID = tableB.xyz”)

 Debug.Print (srs!tableA.data) ' does not work

Yes, data could be an element of tableA or B ... but how to specify?

THank you, Reinhard

Workaround like "as TabA_data" works but there must be a more elegant solution?

1
  • The alias is certainly the most elegant solution for this situation (at least if you can't rename the fields). :-) Commented Sep 29, 2023 at 10:39

1 Answer 1

3

If you do indeed have two fields in different tables with the same name, then you have several options.

  • Firstly, you could actually name the fields differently in the tables. For example, Address in table Customer could become CustomerAddress and Address in Supplier could be SupplierAddress. This is probably the approach that I would recommend, as it prevents ambiguity/confusion.

  • Secondly, as you have mentioned, you could alias the field names in the query.

  • Finally, you could refer to the fields in the recordset by their position:

Debug.Print rs(0)  ' Recordsets are 0-indexed, so this returns the first field

I'm not keen on doing this, as if extra fields are introduced into the recordset, this may no longer work.

Sign up to request clarification or add additional context in comments.

3 Comments

Accessing fields of recordsets by their index does definitely still work. rs.fields(0) will return the first field in the recordset.
What I meant, is if you originally SELECT FieldA, FieldB, and then alter the SQL to be SELECT FieldA, FieldC, FieldB, rs(1) will now return data for FieldC rather than FieldB.
I see, sorry for the confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.