> >This IS the actual code. There are no lookup fields involved. The first
> >[FundNo] in the FindFirst statement is the index key field of the table and
[quoted text clipped - 8 lines]
>
> John W. Vinson [MVP]
>Whops, I thought I wa stelling you the data type by telling you it is a long
>integer, but I see my error because that could be either a number or
>Autonumber. This index FundNo is a Number.
Sorry, missed that in your post. One possibility is a name ambiguity: if you
have a textbox FundNo bound to a field FundNo, then the expression Me!FundNo
is ambiguous. I'm not sure which Access will look at!
Try setting a breakpoint in the code and updating the record. What is the
value of Me.FundNo?
Just fwiw, here's how I'd write the code (which is not to say it's the only
way to write the code, yours very well may work):
Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "[FundNo] = " & Me![FundNo]
If rs.NoMatch Then
' Do nothing
Else
MsgBox "Fund of this number already exists ", vbOKOnly +
vbExclamation, "Enter different fund number . . ."
Cancel = True
Me!FundNo.Undo ' erase the input
Me!FundNo.SetFocus
End If
Set rs = Nothing
End Sub
Note that if the Form is filtered, or is based on a query returning a subset
of the funds, you won't find the fund unless it passes the filter; you would
need instead:
Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("YourTableName", dbOpenSnapshot)
rs.FindFirst "[FundNo] = " & Me![FundNo]
If rs.NoMatch Then
' Do nothing
Else
MsgBox "Fund of this number already exists ", vbOKOnly +
vbExclamation, "Enter different fund number . . ."
Cancel = True
Me!FundNo.Undo ' erase the input
Me!FundNo.SetFocus
End If
rs.Close
Set rs = Nothing
End Sub
John W. Vinson [MVP]
ctdak - 31 Jul 2007 02:02 GMT
Well, I appreciate your help, but so far nothing. I've tried everything you
suggested and more. No matter what I do, the rs.NoMatch conditional will not
find a match.
The table behind the form is not filtered and is not a query returning a
subset. Code is not modifying the recordset in any way. The full table is
the form's data record source. I changed the control name to avoid any
ambiguity. I changed the Dim and Set statements. I experimented with
different forms of strings for the rs.FirstFind statement. I have used a
breakpoint and the value of the control is what you would expect, but a match
is never found when there is one.
I'm totally stumped! I guess I'll just have to forget it and allow the user
to get the stock Access error message, even though they will not likely know
why they are getting it.
ctdak
> >Whops, I thought I wa stelling you the data type by telling you it is a long
> >integer, but I see my error because that could be either a number or
[quoted text clipped - 50 lines]
>
> John W. Vinson [MVP]
AccessVandal - 31 Jul 2007 02:28 GMT
Hi,
Keyin this line just before the "rs.FindFirst "[FundNo] = " & Me![FundNo]"
Debug.Print Me![FundNo]
See the immediate window of the result and show us the result here.
or
MsgBox "Result " & Me![FundNo]
to display a message box.
>ctdak wrote:
>Well, I appreciate your help, but so far nothing. I've tried everything you
[quoted text clipped - 12 lines]
>to get the stock Access error message, even though they will not likely know
>why they are getting it.

Signature
Please Rate the posting if helps you
ctdak - 31 Jul 2007 05:46 GMT
The result is exactly what I typed into the FundNo control. No mystery there.
However, I just realized that the problem must be related to the fact that
my code is opening this form in Add mode. I never mentioned that before (I
assumed it) and didn't think about that having an effect on setting up a
recordest clone in the form's code. What is going on with the form's
underlying table when you open a form in Add mode? Can you do what I'm
trying to do code-wise when in Add mode? Is this why FindFirst isn't finding
anything?
ctdak
> Hi,
>
[quoted text clipped - 24 lines]
> >to get the stock Access error message, even though they will not likely know
> >why they are getting it.
John W. Vinson - 31 Jul 2007 07:05 GMT
>However, I just realized that the problem must be related to the fact that
>my code is opening this form in Add mode. I never mentioned that before (I
[quoted text clipped - 3 lines]
>trying to do code-wise when in Add mode? Is this why FindFirst isn't finding
>anything?
Yes. If you're in Add mode, I'm pretty sure that the form's Recordset.Clone is
an empty recordset (with just the newly created record).
Open a recordset based on the table itself instead, or use DLookUp.
John W. Vinson [MVP]
ctdak - 31 Jul 2007 07:38 GMT
Yes, I've proven that this was indeed the problem all along. I obviously
didn't paint my scenario well enough at the start. Thanks to both responders
for hanging in there with me.
ctdak
> >However, I just realized that the problem must be related to the fact that
> >my code is opening this form in Add mode. I never mentioned that before (I
[quoted text clipped - 10 lines]
>
> John W. Vinson [MVP]