I have a lot of code where I'm basically using code to compare two tables in
such a way that a query can't handle, at least not easily (different logic
between different comparisons on the same record depending on what the answer
of the first comparison is, etc.). So for me, setting the SQL to a string is
a lot less confusing:
Set db = CurrentDB()
sql = "SELECT * FROM [tbl1];"
sql2 = "SELECT * FROM [tbl2];"
sql3 = "SELECT * FROM [tbl3];"
Set rs = db.OpenRecordset(sql)
Set rs2 = db.OpenRecordset(sql2)
Set rs3 = db.OpenRecordset(sql3)
It makes it less confusing when you're debugging, at least for me anyway, lol.
Frank said:
That's fine ... we all have our ways ... I set up a recordset so -
Set rst = CurrentDb.OpenRecordset( _
"SELECT DISTINCT CompanyName" & _
" FROM tblContacts" & _
" ORDER BY CompanyName"
also for clarity ... when I want to debug a rs's SQL I suspend execution,
copy the code to the Immediate Window, and "print" it ... for me it's better
that way ... in 9 out of 10 instances rs SQL works from the start ... and I
don't have to go back to finished code removing Debug.Print's ... and I'm not
left with clock-cycle-wasting code.
To each her own.
:
Perhaps a matter of style, but I much prefer assigning the sql to a string
before using it to open a recordset.
Why?
Because then I can print it to the immediate window and copy and paste it
into a query for debugging.
I can also format "Format" it in the VBA, so I can better visualize the
query.
StrSQL = "SELECT DISTINCT TblContacts.CompanyName" & _
" FROM tblContacts" & _
" ORDER BY CompanyName"
Also, there is really no problem with embedding vbCrLf in the SQL string.
It can make it more readable if you do decide to use debug.print to send it
to the immediate window.
--
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
..
I think -
strSQL = "SELECT DISTINCT tblContacts.CompanyName " & vbCrLf & _
"FROM tblContacts;"
should be -
strSQL = "SELECT DISTINCT tblContacts.CompanyName " & _
"FROM tblContacts;"
or, better yet, do away with strSQL and -
Set rst = CurrentDb.OpenRecordset("SELECT DISTINCT CompanyName FROM
tblContacts")
:
I can't figure it out. Once I have rst set as the query, how do I get
the
data out of it?
strSQL = "SELECT DISTINCT tblContacts.CompanyName " & vbCrLf & _
"FROM tblContacts;"
Set rst = CurrentDb.OpenRecordset(strSQL)
...this should give me a list of company names...
so, now I want to loop through them, like this:
do while not rst.EoF
...but now how to get the name
rst!CompanyName doesn't work
deBug.print rst.Fields doesn't work
where is my text?
Pathetically Dumb,
me