How to refer to a subform ? | | |
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 | | | | re: How to refer to a subform ?
solar wrote:
[color=blue]
> 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[/color]
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 | | | | re: How to refer to a subform ?
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" <svetljop@yahoo.com> wrote in message
news:7bdf9a84.0401180430.3b9dc405@posting.google.c om...[color=blue]
> 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[/color]
0.[color=blue]
> 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[/color] | | | | re: How to refer to a subform ?
"solar" <svetljop@yahoo.com> wrote in message
news:7bdf9a84.0401180430.3b9dc405@posting.google.c om...[color=blue]
> 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[/color]
0.[color=blue]
> 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[/color]
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
[color=blue]
> 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[/color] | | | | re: How to refer to a subform ? svetljop@yahoo.com (solar) wrote in
news:7bdf9a84.0401180430.3b9dc405@posting.google.c om:
[color=blue]
> How to refer to a subform ?[/color]
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) |  | Similar Microsoft Access / VBA bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|