OpenRecordset Syntax failing

thandel
thandel used Ask the Experts™
on
I'm successfully using the following to bring up data from a table.  

    sqlStr = "SELECT * FROM tDr as sDr"

    '"SELECT tDr.LName, tDr.LName FROM tDr WHERE (((tDr.Lisc) Is Not Null)) ORDER BY tDr.LName;"

    Set db = CurrentDb
    Set rs = db.OpenRecordset(sqlStr)

    rs.MoveFirst

    Do While Not rs.EOF
      sDr = sDr & "Dr. " & rs!FName & " " & rs!LName & ", "
      iDrCount = iDrCount + 1
      If (iDrCount) Mod 3 = 0 Then sDr = sDr & vbCrLf   '3 Drs per signature line
      rs.MoveNext
    Loop
    
    rs.Close 'Close the recordset
    Set rs = Nothing 'Clean up

Open in new window


Now I want to add a criteria... where Lisc in the Table Dr is not null.  Same code but with:

sqlStr = "SELECT * FROM tDr WHERE ((tDr.Lisc) Is Not Null) as sDr"

Open in new window


With this change the code fails.  Is my syntax off?  Its working in a plan old query but not in the code.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NorieAnalyst Assistant

Commented:
What are you trying to do with this part of the SQL statement?
as sDr

Open in new window

Author

Commented:
This was implemented as a string (recommended by EE) so I can add text to the search with each find.  Basically to put "Dr" at the front of the results

sDr = sDr & "Dr. " & rs!FName & " " & rs!LName & ", "

Author

Commented:
Hmm now that you ask I changed it to:

sqlStr = "SELECT * FROM tDr WHERE ((tDr.Lisc) Is Not Null)"

That seems to work.  Is that the correct approach?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

NorieAnalyst Assistant

Commented:
Isn't that the same as the original query?
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
thandle,

the query:
SELECT * FROM tDr as sDr

Open in new window

simply provides an alias for the table (tDr) which is not really needed when creating a recordset. With a table name that short, I would never create an alias for the table name in a query, but if you had a table named "tblDoctorPointsOfContact", I would alias that with something like: "tDPOC"

HTH
Dale

Author

Commented:
Norie: Isn't that the same as the original query?

No simliar but without the as sDr

Dale: Thanks with my working change was that the proper syntax aside from your suggestion?
Owner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010
Commented:
Yes, that syntax should work,

sqlStr = "SELECT * FROM tDr WHERE ((tDr.Lisc) Is Not Null)"

although you really don't need the parenthesis.  Access will add them automatically, and I generally like to wrap each criteria in a single set of parentheses, for clarity, like:

sqlStr = "SELECT * FROM tDr WHERE (tDr.Lisc Is Not Null)"

Dale
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
To make it even simplier:
sqlStr = "SELECT * FROM tDr WHERE Lisc Is Not Null;"

Open in new window

Unless there are ambiguities, the table name is optional in front of column names.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial