By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,499 Members | 829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,499 IT Pros & Developers. It's quick & easy.

How to refer to a subform ?

P: n/a
How to refer to a subform ?
On my main form called FrmEmployees I have a button to open a report.
I want to forbid opening the report in case the liters in the subform are 0.
To this end I have build an If condition:

Dim ClLiters As Control
ClLiters = Forms!FrmEmployees!FrmEmployeesSub.Form!liters
If IsNull(strLiters) Then
DoCmd.Beep
Exit Sub
Else
DoCmd.OpenReport strDocName, acPreview, , strWhere

However, Access point to an error :

"Object variable or With block variable not set"

Obvioulsy the fault lies with my subform definition, since otherwise,
When I refer to a control in the main form, the report is opened:

If IsNull(Me![FirstName]) Then
DoCmd.Beep
Else
DoCmd.OpenReport strDocName, acPreview, , strWhere
Nov 12 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
solar wrote:
How to refer to a subform ?

On my main form called FrmEmployees I have a button to open a report.
I want to forbid opening the report in case the liters in the subform are 0.
To this end I have build an If condition:

Dim ClLiters As Control
ClLiters = Forms!FrmEmployees!FrmEmployeesSub.Form!liters
If IsNull(strLiters) Then
DoCmd.Beep
Exit Sub
Else
DoCmd.OpenReport strDocName, acPreview, , strWhere

However, Access point to an error :

"Object variable or With block variable not set"

Obvioulsy the fault lies with my subform definition, since otherwise,
When I refer to a control in the main form, the report is opened:

If IsNull(Me![FirstName]) Then
DoCmd.Beep
Else
DoCmd.OpenReport strDocName, acPreview, , strWhere


May I assume Liters is a field and that the subform may contain 1 or many
records? If so, you would want to check the records. The following is DAO
code.

Dim rst As Recordset
set rst = Forms!MainFormName!SubFormName.Form.Recordsetclone
rst.FindFirst "Liters = 0"
If rst.Nomatch then
docmd.openreport "ReportName"
else
msgbox "All records need to have a liter count"
endif

if you want to see if any records exist, modify to this
Dim rst As Recordset
set rst = Forms!MainFormName!SubFormName.Form.Recordsetclone
If rst.RecordCount > 0 then
docmd.openreport "ReportName"
else
msgbox "You need to create a liter record"
endif
Nov 12 '05 #2

P: n/a
I think you need to use the SET keyword:
Set ClLiters = Forms!FrmEmployees!FrmEmployeesSub.Form!liters
For absolute clarity, it might also be a good idea to specify
If IsNull(strLiters.value) Then

HTH
- Turtle

"solar" <sv******@yahoo.com> wrote in message
news:7b**************************@posting.google.c om...
How to refer to a subform ?
On my main form called FrmEmployees I have a button to open a report.
I want to forbid opening the report in case the liters in the subform are 0. To this end I have build an If condition:

Dim ClLiters As Control
ClLiters = Forms!FrmEmployees!FrmEmployeesSub.Form!liters
If IsNull(strLiters) Then
DoCmd.Beep
Exit Sub
Else
DoCmd.OpenReport strDocName, acPreview, , strWhere

However, Access point to an error :

"Object variable or With block variable not set"

Obvioulsy the fault lies with my subform definition, since otherwise,
When I refer to a control in the main form, the report is opened:

If IsNull(Me![FirstName]) Then
DoCmd.Beep
Else
DoCmd.OpenReport strDocName, acPreview, , strWhere

Nov 12 '05 #3

P: n/a

"solar" <sv******@yahoo.com> wrote in message
news:7b**************************@posting.google.c om...
How to refer to a subform ?
On my main form called FrmEmployees I have a button to open a report.
I want to forbid opening the report in case the liters in the subform are 0. To this end I have build an If condition:

Dim ClLiters As Control
ClLiters = Forms!FrmEmployees!FrmEmployeesSub.Form!liters
If IsNull(strLiters) Then
DoCmd.Beep
Exit Sub
Else
DoCmd.OpenReport strDocName, acPreview, , strWhere
There's always many ways to do these sorts of things. I personally like to
look for simple solutions.

If Val(Forms!FrmEmployees!FrmEmployeesSub.Form!liters & "") > 0 Then
DoCmd.OpenReport ...
Else
Do something else such as warn user of invalid entry
End If

If the code is in the click event for the button on the main form then you
should use Me. rather than Forms!FrmEmployees!

HTH,
Randy Harris
However, Access point to an error :

"Object variable or With block variable not set"

Obvioulsy the fault lies with my subform definition, since otherwise,
When I refer to a control in the main form, the report is opened:

If IsNull(Me![FirstName]) Then
DoCmd.Beep
Else
DoCmd.OpenReport strDocName, acPreview, , strWhere

Nov 12 '05 #4

P: n/a
sv******@yahoo.com (solar) wrote in
news:7b**************************@posting.google.c om:
How to refer to a subform ?


I put some code in the subform's module, or set its Has Module Property to
true.

Then the subform is publicly available as the

Form_FormName

object.

example:

this code in the main form ... Form5 is the subform.

Private Sub Form_Click()
MsgBox Nz(Form_Form5.Text0.Value, "Null")
End Sub

--
Lyle
(for e-mail refer to http://ffdba.com/contacts.htm)
Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.