If statements

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a form with a subform attached to it from the form I run code as below

Public Function CodeVATCal(vatJobCode, vatJobGross)
If vatJobCode Like "CR" Then
CodeVATCal = vatJobGross
Else
CodeVATCal = (vatJobGross / 117.5) * 100
End If
End Function

The works fine however if the recordset of the subform is empty this causes
a runtme error. My question is how can I make the code ignore if the
recordset is empty or has no value.

thanks

Matt
 
Matt said:
Hi,

I have a form with a subform attached to it from the form I run code as
below

Public Function CodeVATCal(vatJobCode, vatJobGross)
If vatJobCode Like "CR" Then
CodeVATCal = vatJobGross
Else
CodeVATCal = (vatJobGross / 117.5) * 100
End If
End Function

The works fine however if the recordset of the subform is empty this
causes
a runtme error. My question is how can I make the code ignore if the
recordset is empty or has no value.

thanks

Matt

Try only calling your function if the subform's recordset clone's record
count is >0, something like:

If Me.RecordsetClone.RecordCount >0 Then ' Run Code

Keith.
www.keithwilby.com
 
If no new records can be added to the subform (e.g. its AllowAdditions is
No, or it is based on a read-only query), the entire Detail section of the
subform goes blank when there are no records.

If that is the issue, you can test the RecordCount of the RecordsetClone of
the Form in the subform control like this:
If Me.[Sub1].Form.RecordsetClone.RecordCount > 0 Then

This assumes:
a) the subform control is named "Sub1";
b) the code goes in the main form's module.
 
Looking good so far. Do I attached this code to an event in the main form, I
have tried the on current event and it does not seem to work. I am convinced
the code works just do not know where to put it

Thanks

Matt

Allen Browne said:
If no new records can be added to the subform (e.g. its AllowAdditions is
No, or it is based on a read-only query), the entire Detail section of the
subform goes blank when there are no records.

If that is the issue, you can test the RecordCount of the RecordsetClone of
the Form in the subform control like this:
If Me.[Sub1].Form.RecordsetClone.RecordCount > 0 Then

This assumes:
a) the subform control is named "Sub1";
b) the code goes in the main form's module.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Matt said:
I have a form with a subform attached to it from the form I run code as
below

Public Function CodeVATCal(vatJobCode, vatJobGross)
If vatJobCode Like "CR" Then
CodeVATCal = vatJobGross
Else
CodeVATCal = (vatJobGross / 117.5) * 100
End If
End Function

The works fine however if the recordset of the subform is empty this
causes
a runtme error. My question is how can I make the code ignore if the
recordset is empty or has no value.
 
Whateever you want to achieve, Matt.

You posted a public function, that accepts 2 variables and calculates a 3rd.
I have no idea why, or what you want to do with it.

If CodeVATCal is a control bound to a field, it would not be a good idea to
do this in Form_Current though. It make no sense to assign a value to a
bound control just because you visit a record.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Matt said:
Looking good so far. Do I attached this code to an event in the main form,
I
have tried the on current event and it does not seem to work. I am
convinced
the code works just do not know where to put it

Thanks

Matt

Allen Browne said:
If no new records can be added to the subform (e.g. its AllowAdditions is
No, or it is based on a read-only query), the entire Detail section of
the
subform goes blank when there are no records.

If that is the issue, you can test the RecordCount of the RecordsetClone
of
the Form in the subform control like this:
If Me.[Sub1].Form.RecordsetClone.RecordCount > 0 Then

This assumes:
a) the subform control is named "Sub1";
b) the code goes in the main form's module.

Matt said:
I have a form with a subform attached to it from the form I run code as
below

Public Function CodeVATCal(vatJobCode, vatJobGross)
If vatJobCode Like "CR" Then
CodeVATCal = vatJobGross
Else
CodeVATCal = (vatJobGross / 117.5) * 100
End If
End Function

The works fine however if the recordset of the subform is empty this
causes
a runtme error. My question is how can I make the code ignore if the
recordset is empty or has no value.
 
Sorry Allen you are right, I was trying to do two things at once not a good
idea. Got it working in a public function as I need to call it in more than
form.

Thanks

Matt

Allen Browne said:
Whateever you want to achieve, Matt.

You posted a public function, that accepts 2 variables and calculates a 3rd.
I have no idea why, or what you want to do with it.

If CodeVATCal is a control bound to a field, it would not be a good idea to
do this in Form_Current though. It make no sense to assign a value to a
bound control just because you visit a record.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Matt said:
Looking good so far. Do I attached this code to an event in the main form,
I
have tried the on current event and it does not seem to work. I am
convinced
the code works just do not know where to put it

Thanks

Matt

Allen Browne said:
If no new records can be added to the subform (e.g. its AllowAdditions is
No, or it is based on a read-only query), the entire Detail section of
the
subform goes blank when there are no records.

If that is the issue, you can test the RecordCount of the RecordsetClone
of
the Form in the subform control like this:
If Me.[Sub1].Form.RecordsetClone.RecordCount > 0 Then

This assumes:
a) the subform control is named "Sub1";
b) the code goes in the main form's module.


I have a form with a subform attached to it from the form I run code as
below

Public Function CodeVATCal(vatJobCode, vatJobGross)
If vatJobCode Like "CR" Then
CodeVATCal = vatJobGross
Else
CodeVATCal = (vatJobGross / 117.5) * 100
End If
End Function

The works fine however if the recordset of the subform is empty this
causes
a runtme error. My question is how can I make the code ignore if the
recordset is empty or has no value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top