Avatar of Patrick O'Dea
Patrick O'Dea
Flag for Ireland asked on

Access VBA .... Quotes!

See attached code
How do I add to the where clause a second condition ... being .... len(TransDesc)=0.
The quotes are confusing me (again!)

Var = CurrentProject.Connection.Execute( _
"SELECT COUNT(*) FROM tblBankAIB " _
& " WHERE AIBCreditValue<>0 ").Collect(0)

MsgBox "There are " & Var & " lodgements with no Giro reference"



End Sub

Open in new window

Microsoft AccessVBA

Avatar of undefined
Last Comment
Dale Fye

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
IrogSinta

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Patrick O'Dea

ASKER
Thx!  Easy one!
Dale Fye

The best way to do this is to build the SQL before the Execute method, so that you can debug it until it is correct.  

Plus, you cannot set the value of a variable to the results of an EXECUTE method.

Dim rs as DAO.Recordset
strSQL = "SELECT COUNT(*) as Lodgements FROM tblBankAIB " _
             & "WHERE AIBCreditValue<>0"
debug.print strSQL
set rs = currentdb.openrecordset(strSQL, , dbfailonerror)
Var = rs!Lodgements
rs.close
set rs = nothing

MsgBox "There are " & Var & " lodgements with no Giro reference"

Open in new window

Dale Fye

Ron, what is that:

.Collect(0)

syntax?  I've never seen that before?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
IrogSinta

You're right, Dale.  I didn't even pay attention to that.  I would normally use:
           .Fields(0)
In order to get the result of the first field.
Dale Fye

@Ron,  I've never seen that done in an Execute method, and cannot figure out how to do that.

I assume that you were thinking that the OP was opening a recordset, not trying to retrieve data through an Execute method.  Is that correct?

@Dewsbury,

Did you even try that syntax in the context you provided?  If so, and it worked, could you please post what worked for you, because I've never seen it done that way before.

Dale
IrogSinta

@Dale, I looked into it and apparently the syntax is correct.  It is used for ADO connections.  I even did a quick test and it works.  I think we both learned something new :-)

Ron
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Dale Fye

Thanks, Ron.

I so rarely use ADO, that I didn't pickup on that syntax:

CurrentProject.Connection.Execute

I was focused on the Execute.
Patrick O'Dea

ASKER
As the OP .... I must confess that the Collect.(0) syntax was cut and pasted from elsewhere..... (perhaps contrary to best practice)!
Dale Fye

21Dewsbury,

No, don't assume that at all.  I just rarely use ADO, so didn't recognize the syntax.  It's good when even the experts get a chance to learn something new.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes