How to check if a subform is empty? (1 Viewer)

adamtate94

Registered User.
Local time
Today, 05:04
Joined
Sep 6, 2016
Messages
31
Hi guys,
been playing around with VBA on how to check if a subform has records displaying in it or now,
i've tried things like if Me!subfrmHazardRAMS.Form!RecordSource = "" Then.


Just getting error..

Thanks in advance
 

CJ_London

Super Moderator
Staff member
Local time
Today, 13:04
Joined
Feb 19, 2013
Messages
16,619
try

Me.subfrmHazardRAMS.Form.count

use dots(.) rather than bangs (!)
 
Last edited:

adamtate94

Registered User.
Local time
Today, 05:04
Joined
Sep 6, 2016
Messages
31
Thanks for the reply, getting this error..
Image Attached
 

Attachments

  • errormsg.png
    errormsg.png
    42.3 KB · Views: 350

TJPoorman

Registered User.
Local time
Today, 06:04
Joined
Jul 23, 2013
Messages
402
You can load a copy of the subform's recordset into memory then check if it's empty.
Code:
Dim rs as RecordSet

Set rs = me.subfrmHazardRAMS.Form.RecordsetClone
If rs.BOF and rs.EOF Then
    MsgBox "No Records"
End If
 

CJ_London

Super Moderator
Staff member
Local time
Today, 13:04
Joined
Feb 19, 2013
Messages
16,619
which line is causing the error? click on debug to see which one is highlighted

for the code to work, subfrmHazardRAMS has to be a subform on the form from where it is run

as you type me. you will be prompted for all the available objects, subfrmHazardRAMS should be one of them, as is (presumably) combo183
 

adamtate94

Registered User.
Local time
Today, 05:04
Joined
Sep 6, 2016
Messages
31
The If line with the code you gave me have the error
 

adamtate94

Registered User.
Local time
Today, 05:04
Joined
Sep 6, 2016
Messages
31
The Recordset code has also given me an error on this line :


(Attached)
 

Attachments

  • recrdseterror.png
    recrdseterror.png
    21.6 KB · Views: 243

CJ_London

Super Moderator
Staff member
Local time
Today, 13:04
Joined
Feb 19, 2013
Messages
16,619
please don't just read the first line of the post. read it all and respond to each part. I don't have time to keep reminding you.

Answer these

for the code to work, subfrmHazardRAMS has to be a subform on the form from where it is run
is it?

as you type me. you will be prompted for all the available objects, subfrmHazardRAMS should be one of them, as is (presumably) combo183
does it (and combo183) appear?
 

adamtate94

Registered User.
Local time
Today, 05:04
Joined
Sep 6, 2016
Messages
31
The subreport is part of the form i am adding the code to, still getting an error. Please see attached
 

Attachments

  • subfrmerror.png
    subfrmerror.png
    41 KB · Views: 136

adamtate94

Registered User.
Local time
Today, 05:04
Joined
Sep 6, 2016
Messages
31
The expression you entered refers to an object that is closed or doesn't exist. On the line with the Me.subfrmHazardRAMS.Form.count on.

Thanks
 

CJ_London

Super Moderator
Staff member
Local time
Today, 13:04
Joined
Feb 19, 2013
Messages
16,619
it works for me. Alternative try

Me.subfrmHazardRAMS.Form.recordset.recordcount
 

CJ_London

Super Moderator
Staff member
Local time
Today, 13:04
Joined
Feb 19, 2013
Messages
16,619
one other thought - I presume your subform does have a sourceobject
 

Users who are viewing this thread

Top Bottom