Querie Dilemma (1 Viewer)

TEE45SEE

New member
Local time
Today, 18:45
Joined
Jul 8, 2023
Messages
2
I have a relatively simple SELECT query which when run it gives me "Runtime error 91"

strSQL = "SELECT * FROM tblMonthlyHomeExpenses" _
& " WHERE DatePart('m',[dteTransDate]) =" & intMonthNumber & ";"

When I take the same query and run it as SQL it does exactly what I would expect

SELECT * FROM tblMonthlyHomeExpenses WHERE DatePart('m',[dteTransDate]) =3;

Could anyone please explain to me what is going on. Thanks
 

plog

Banishment Pending
Local time
Today, 17:45
Joined
May 11, 2011
Messages
11,653
You've not given us enough information, please post all the VBA code. Is the VBA line above the one being highlighted when the error flags?

Also, how did you get the SQL that you posted above? Did you Debug.Print your VBA? or are you just assuming that's the SQL that the VBA is creating?
 

tvanstiphout

Active member
Local time
Today, 15:45
Joined
Jan 22, 2016
Messages
228
The statement is correct, but the way you're running it is not. Show us the VBA context.

Example of how this error can occur:
dim db as dao.database
dim rs as dao.recordset
strSql = "..."
set rs = db.OpenRecordset(strSql, ...) 'OOPS, db was never set. Error 91.
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:45
Joined
May 21, 2018
Messages
8,547
91 is an object not set error, not related to the syntax. Your problem is somewhere else.
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:45
Joined
Sep 21, 2011
Messages
14,336
Please supply error description as well in the future.
Not all of us (certainly me) do not know the error codes to heart.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:45
Joined
Feb 28, 2001
Messages
27,207
On a different note, that was your first post. Hello and welcome to the forum.

Now, as to your problem... I think MajP has correctly pointed the discussion. Tom has given a likely example of a common error made when dealing with manually vs. automatically run queries.
 

TEE45SEE

New member
Local time
Today, 18:45
Joined
Jul 8, 2023
Messages
2
Thanks to each of you for responding and pointing me in the right direction. Have found and corrected with everything working as it should.
Thank you to The_Doc_Man for the welcome
Till the next time.
 

Mike Krailo

Well-known member
Local time
Today, 18:45
Joined
Mar 28, 2020
Messages
1,044
Well you have to at least tell us what fixed it so that it can help other readers in the future.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:45
Joined
Feb 19, 2002
Messages
43,328
@TEE45SEE The logic of your query is incorrect. It will only work if there is NEVER more than ONE year's worth of data in your table. You have to use Year and Month, not just month to select the correct data. Unless of course, you want April from all years in the table.
 

Users who are viewing this thread

Top Bottom