Whats wrong with my Code!

B

Bob

Private Sub cmdClose_Click()
If IsNull(Me.subHorseDetailsChild.Form.OwnerID) + (tbName) Then
If Me.Dirty Then
Me.Undo
End If
If IsNull(Me.subHorseDetailsChild.Form.OwnerID) + (cbFatherName) Then
If Me.Dirty Then
Me.Undo
End If

If Me.Dirty Then
Me.Undo
End If
End If
End If

DoCmd.Close acForm, Me.Name

End Sub

Cheers Bob





..........Jenny Vance
 
G

Graham Mandeno

Hi Bob

I'm not sure what you are trying to achieve, so it's difficult to advise how
to fix your code.

However, I can tell you that this line:
If IsNull(Me.subHorseDetailsChild.Form.OwnerID) + (tbName) Then
does not make any sense at all.

I assume OwnerID is a field in the recordsource of a subform named
subHorseDetailsChild, and that tbName is a textbox on the main form,
correct?

IsNull(Me.subHorseDetailsChild.Form.OwnerID) will return True (-1) or False
(0) depending on whether or not OwnerID is null.

The + (tbName) bit will attempt to add that result to the contents of
tbName.

If tbName is numeric then it will do some arithmetic - say OwnerID is null
and tbName is 999, then the result will be -1+999 (=998).

So your If statement will be the same as:

If 998 Then...

Since anything non-zero will be treated as True, this will take the Then
path.

However, if tbName is, say, "Zabeel", then the "addition" will just result
in joining two text strings, so the end result will be:

If "-1Zabeel" Then

Clearly both of these are nonsense!

So, clearly you want to check some combinations of field values for
validity, and undo the changes if they are not OK, but what DOES constitute
a valid record? Post back with some more info please!

Also, it is usual for record validation code to be in Form_BeforeUpdate, not
in the Click event of a command button. What would happen if your user
closed the form some other way, say by clicking the X in the corner?
 
B

Bob

In Short Graham, unless this form had data in:
Me.subHorseDetailsChild.Form.OwnerID) and (tbName)
Or
Me.subHorseDetailsChild.Form.OwnerID) and (cbFatherName)
the record would not be saved
thanks for your help...Bob

Graham Mandeno said:
Hi Bob

I'm not sure what you are trying to achieve, so it's difficult to advise
how to fix your code.

However, I can tell you that this line:
If IsNull(Me.subHorseDetailsChild.Form.OwnerID) + (tbName) Then
does not make any sense at all.

I assume OwnerID is a field in the recordsource of a subform named
subHorseDetailsChild, and that tbName is a textbox on the main form,
correct?

IsNull(Me.subHorseDetailsChild.Form.OwnerID) will return True (-1) or
False (0) depending on whether or not OwnerID is null.

The + (tbName) bit will attempt to add that result to the contents of
tbName.

If tbName is numeric then it will do some arithmetic - say OwnerID is null
and tbName is 999, then the result will be -1+999 (=998).

So your If statement will be the same as:

If 998 Then...

Since anything non-zero will be treated as True, this will take the Then
path.

However, if tbName is, say, "Zabeel", then the "addition" will just result
in joining two text strings, so the end result will be:

If "-1Zabeel" Then

Clearly both of these are nonsense!

So, clearly you want to check some combinations of field values for
validity, and undo the changes if they are not OK, but what DOES
constitute a valid record? Post back with some more info please!

Also, it is usual for record validation code to be in Form_BeforeUpdate,
not in the Click event of a command button. What would happen if your
user closed the form some other way, say by clicking the X in the corner?
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand




Bob said:
Private Sub cmdClose_Click()
If IsNull(Me.subHorseDetailsChild.Form.OwnerID) + (tbName) Then
If Me.Dirty Then
Me.Undo
End If
If IsNull(Me.subHorseDetailsChild.Form.OwnerID) + (cbFatherName) Then
If Me.Dirty Then
Me.Undo
End If

If Me.Dirty Then
Me.Undo
End If
End If
End If

DoCmd.Close acForm, Me.Name

End Sub

Cheers Bob





.........Jenny Vance
 
G

Graham Mandeno

OK, so there must be data in Me.subHorseDetailsChild.Form.OwnerID AND there
must be data in EITHER tbName OR cbFatherName?

Or, to put it another way, the record is *invalid* if
Me.subHorseDetailsChild.Form.OwnerID is null OR if BOTH tbName AND
cbFatherName are null.

Then the test for an invalid record would be:

If IsNull(Me.subHorseDetailsChild.Form.OwnerID) Or _
(IsNull(tbName) And IsNull(cbFatherName)) Then

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Bob said:
In Short Graham, unless this form had data in:
Me.subHorseDetailsChild.Form.OwnerID) and (tbName)
Or
Me.subHorseDetailsChild.Form.OwnerID) and (cbFatherName)
the record would not be saved
thanks for your help...Bob

Graham Mandeno said:
Hi Bob

I'm not sure what you are trying to achieve, so it's difficult to advise
how to fix your code.

However, I can tell you that this line:
If IsNull(Me.subHorseDetailsChild.Form.OwnerID) + (tbName) Then
does not make any sense at all.

I assume OwnerID is a field in the recordsource of a subform named
subHorseDetailsChild, and that tbName is a textbox on the main form,
correct?

IsNull(Me.subHorseDetailsChild.Form.OwnerID) will return True (-1) or
False (0) depending on whether or not OwnerID is null.

The + (tbName) bit will attempt to add that result to the contents of
tbName.

If tbName is numeric then it will do some arithmetic - say OwnerID is
null and tbName is 999, then the result will be -1+999 (=998).

So your If statement will be the same as:

If 998 Then...

Since anything non-zero will be treated as True, this will take the Then
path.

However, if tbName is, say, "Zabeel", then the "addition" will just
result in joining two text strings, so the end result will be:

If "-1Zabeel" Then

Clearly both of these are nonsense!

So, clearly you want to check some combinations of field values for
validity, and undo the changes if they are not OK, but what DOES
constitute a valid record? Post back with some more info please!

Also, it is usual for record validation code to be in Form_BeforeUpdate,
not in the Click event of a command button. What would happen if your
user closed the form some other way, say by clicking the X in the corner?
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand




Bob said:
Private Sub cmdClose_Click()
If IsNull(Me.subHorseDetailsChild.Form.OwnerID) + (tbName) Then
If Me.Dirty Then
Me.Undo
End If
If IsNull(Me.subHorseDetailsChild.Form.OwnerID) + (cbFatherName) Then
If Me.Dirty Then
Me.Undo
End If

If Me.Dirty Then
Me.Undo
End If
End If
End If

DoCmd.Close acForm, Me.Name

End Sub

Cheers Bob





.........Jenny Vance
 
B

Bob

Thanks Graham worked great No Zabeels without an owner :))) Regards Bob
Graham Mandeno said:
OK, so there must be data in Me.subHorseDetailsChild.Form.OwnerID AND
there must be data in EITHER tbName OR cbFatherName?

Or, to put it another way, the record is *invalid* if
Me.subHorseDetailsChild.Form.OwnerID is null OR if BOTH tbName AND
cbFatherName are null.

Then the test for an invalid record would be:

If IsNull(Me.subHorseDetailsChild.Form.OwnerID) Or _
(IsNull(tbName) And IsNull(cbFatherName)) Then

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Bob said:
In Short Graham, unless this form had data in:
Me.subHorseDetailsChild.Form.OwnerID) and (tbName)
Or
Me.subHorseDetailsChild.Form.OwnerID) and (cbFatherName)
the record would not be saved
thanks for your help...Bob

Graham Mandeno said:
Hi Bob

I'm not sure what you are trying to achieve, so it's difficult to advise
how to fix your code.

However, I can tell you that this line:
If IsNull(Me.subHorseDetailsChild.Form.OwnerID) + (tbName) Then
does not make any sense at all.

I assume OwnerID is a field in the recordsource of a subform named
subHorseDetailsChild, and that tbName is a textbox on the main form,
correct?

IsNull(Me.subHorseDetailsChild.Form.OwnerID) will return True (-1) or
False (0) depending on whether or not OwnerID is null.

The + (tbName) bit will attempt to add that result to the contents of
tbName.

If tbName is numeric then it will do some arithmetic - say OwnerID is
null and tbName is 999, then the result will be -1+999 (=998).

So your If statement will be the same as:

If 998 Then...

Since anything non-zero will be treated as True, this will take the Then
path.

However, if tbName is, say, "Zabeel", then the "addition" will just
result in joining two text strings, so the end result will be:

If "-1Zabeel" Then

Clearly both of these are nonsense!

So, clearly you want to check some combinations of field values for
validity, and undo the changes if they are not OK, but what DOES
constitute a valid record? Post back with some more info please!

Also, it is usual for record validation code to be in Form_BeforeUpdate,
not in the Click event of a command button. What would happen if your
user closed the form some other way, say by clicking the X in the
corner?
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand






Private Sub cmdClose_Click()
If IsNull(Me.subHorseDetailsChild.Form.OwnerID) + (tbName) Then
If Me.Dirty Then
Me.Undo
End If
If IsNull(Me.subHorseDetailsChild.Form.OwnerID) + (cbFatherName) Then
If Me.Dirty Then
Me.Undo
End If

If Me.Dirty Then
Me.Undo
End If
End If
End If

DoCmd.Close acForm, Me.Name

End Sub

Cheers Bob





.........Jenny Vance
 

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

Top