Solved Object variable or With block variable not set Error (1 Viewer)

Momma

Member
Local time
Today, 22:59
Joined
Jan 22, 2022
Messages
114
I get an "Object variable or With block not set" error on the following code. I did a Debug Compile and it compiled without any errors.
What could be the problem?

Code:
            Dim rs As DAO.Recordset
            Set rs = db.OpenRecordset("SELECT Count([pkFilename]) AS RecCount FROM TblDocument where [fileName] = filename1;")
            RecCount1 = rs![RecCount]
            rs.Close
            Set rs = Nothing
 

June7

AWF VIP
Local time
Today, 04:59
Joined
Mar 9, 2014
Messages
5,472
What is filename1? Where does that parameter come from?

Your code works for me after I changed db to CurrentDb - but I get "missing parameter" error because of the embedded filename1 parameter. Need to concatenate variable inputs and use apostrophe delimiters if filename is text data type.

"SELECT Count([pkFilename]) AS RecCount FROM TblDocument where [fileName] = '" & filename1 & "'")

Or you could just use DCount() domain aggregate function

RecCount1 = DCount("*", "TblDocument", "filename='" & filename1 & "'")

You should have Option Explicit at top of every code module. Set your VBA editor to do that by default for new modules. Will have to manually add to existing.
 
Last edited:

tvanstiphout

Active member
Local time
Today, 05:59
Joined
Jan 22, 2016
Messages
222
I get an "Object variable or With block not set" error on the following code. I did a Debug Compile and it compiled without any errors.
What could be the problem?

Code:
            Dim rs As DAO.Recordset
            Set rs = db.OpenRecordset("SELECT Count([pkFilename]) AS RecCount FROM TblDocument where [fileName] = filename1;")
            RecCount1 = rs![RecCount]
            rs.Close
            Set rs = Nothing
Probably because "db" is not declared. Add this:
dim db as dao.database
set db=currentdb
 

June7

AWF VIP
Local time
Today, 04:59
Joined
Mar 9, 2014
Messages
5,472
Actually, without declaring and setting db, the error is simply "object required".
 

Momma

Member
Local time
Today, 22:59
Joined
Jan 22, 2022
Messages
114
What is filename1? Where does that parameter come from?

Your code works for me after I changed db to CurrentDb - but I get "missing parameter" error because of the embedded filename1 parameter. Need to concatenate variable inputs and use apostrophe delimiters if filename is text data type.

"SELECT Count([pkFilename]) AS RecCount FROM TblDocument where [fileName] = '" & filename1 & "'")

Or you could just use DCount() domain aggregate function

RecCount1 = DCount("*", "TblDocument", "filename='" & filename1 & "'")

You should have Option Explicit at top of every code module. Set your VBA editor to do that by default for new modules. Will have to manually add to existing.
Sorry, I declare it as follows:

Code:
    Dim Filename1 As String
    Filename1 = "SaleGuarantee-" & Contact & ".pdf"
    Dim Contact As String
    Contact = Forms!frmContactgrouplistbox!ContactList.Column(2, i)
    Dim rs As DAO.Recordset
    Set rs = db.OpenRecordset("SELECT Count([pkFilename]) AS RecCount FROM TblDocument where [fileName] = filename1;")
    RecCount1 = rs![RecCount]
    rs.Close
    Set rs = Nothing
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 07:59
Joined
Feb 28, 2001
Messages
27,186
When you DIM a variable, it is usually created with a default value. For string data types, the default is a zero-length (i.e. empty) string. It actually is legal to DIM variables out of order because the DIM statements are "gathered" together. They actually aren't executable. (Note that a REDIM statement IS executable.) So it doesn't matter that you defined the variables pretty much as you needed them. But it is considered slightly preferred form to DIM stuff before the first executable statement of any routine. Makes it easier to understand.

In this snippet, you assign a value to Contact AFTER you use it to assign a value to Filename1. Therefore, at the time that you define Filename1, you would get the string "SaleGaurantee-.pdf" which might not point to anything meaningful. Which means the recordset you defined would fail in its definition.
 

June7

AWF VIP
Local time
Today, 04:59
Joined
Mar 9, 2014
Messages
5,472
Well, did you try the suggested corrections?
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 08:59
Joined
Apr 27, 2015
Messages
6,341
In addition to not setting your db Object to Currentdb, you have a couple of errors:

Code:
Filename1 = "SaleGuarantee-" & Contact & ".pdf"
Dim Contact As String
Contact = Forms!frmContactgrouplistbox!ContactList.Column(2, i)

You are trying to use the string 'Contact' as part of another string BEFORE you have populated it. Try this:
Code:
Dim db As DAO.Database
Dim Filename1 As String
Dim Contact As String
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT Count([pkFilename]) AS RecCount FROM TblDocument where [fileName] = filename1;")

Contact = Forms!frmContactgrouplistbox!ContactList.Column(2, i)
Filename1 = "SaleGuarantee-" & Contact & ".pdf"

RecCount1 = rs![RecCount]
rs.Close
Set rs = Nothing
Set db = Nothing

However, this will still not work because your 2nd argument (row) in Contact = Forms!frmContactgrouplistbox!ContactList.Column(2,i) is not being populated. You need to declare 'i' as an integer and then populate it before you run this event.
 
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 13:59
Joined
Jul 9, 2003
Messages
16,282
Try This:-

Code:
Dim db As DAO.Database
Set db = CurrentDb()

Dim rs As DAO.Recordset
Set rs = db.OpenRecordset("SELECT Count([pkFilename]) AS RecCount FROM TblDocument WHERE [fileName] = '" & filename1 & "';")
RecCount1 = rs![RecCount]
rs.Close
Set rs = Nothing
 

Momma

Member
Local time
Today, 22:59
Joined
Jan 22, 2022
Messages
114
When you DIM a variable, it is usually created with a default value. For string data types, the default is a zero-length (i.e. empty) string. It actually is legal to DIM variables out of order because the DIM statements are "gathered" together. They actually aren't executable. (Note that a REDIM statement IS executable.) So it doesn't matter that you defined the variables pretty much as you needed them. But it is considered slightly preferred form to DIM stuff before the first executable statement of any routine. Makes it easier to understand.

In this snippet, you assign a value to Contact AFTER you use it to assign a value to Filename1. Therefore, at the time that you define Filename1, you would get the string "SaleGaurantee-.pdf" which might not point to anything meaningful. Which means the recordset you defined would fail in its definition.

Well, did you try the suggested corrections?
Sorry for the late reply. I end up using your 2nd suggestion and it worked.
The first one gives a run-time error, although it compiled.

Code:
            Dim rs As DAO.Recordset
            Dim db As DAO.Database
            Set db = CurrentDb
            Set rs = db.OpenRecordset("SELECT Count([pkFilename]) AS RecCount FROM TblDocument where [fileName] = '" & Filename1 & "'")
            RecCount1 = rs![RecCount]
            rs.Close
            Set rs = Nothing
1698382365325.png
 

Users who are viewing this thread

Top Bottom