Cancel Adding New Record If Condition is True (1 Viewer)

Bee

Registered User.
Local time
Today, 11:32
Joined
Aug 1, 2006
Messages
487
Hi

I wrote code that should validate a field when entering a new record and then if a condition is true, that new record should be cancelled and not entered into the table.

I managed to partially achieve this by writing the code below, but the new record does not get cancelled because the table will still create a PK for that record and leave the rest of the fields empty. I am using an autonumber for the PK that's why the table creates it automatically What I want to achieve is to cancel the creation of a new record at once, I don't want even PK created for that new record.

I used the CancelUpdate because I thought it would cancel the record creation, but it did not! When I read about it it said that I need to use it with either Edit or AddNew, (which i don't understand why!) but it still does not work.

Code:
Private Sub PlotNum_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_msg
Dim db As DAO.Database, rs As DAO.Recordset
Dim n As Integer, i As Integer
Dim vPlotNum As Integer
Dim vPhaseID As Integer

vPhaseID = Forms![frmHouse].Form![PhaseID]
vPlotNum = Forms![frmHouse].[qryHouse2].Form![PlotNum]

Set db = CurrentDb
Set rs = db.OpenRecordset("tblHouse")
rs.MoveLast
n = rs.RecordCount
rs.MoveFirst
If n > 0 Then
    For i = 1 To n
        If rs![PhaseID] = vPhaseID Then
            If rs![PlotNum] = vPlotNum Then
                    rs.Edit
                    rs.CancelUpdate
                    MsgBox "This plot number already exist in this particular phase." & vbCrLf & "Please choose a different Plot Number"
                    Forms![frmHouse].qryHouse2.Form![PlotNum].Text = ""
            End If
        End If
        rs.MoveNext
    Next i
End If
rs.Close
db.Close
Set db = Nothing
Set rs = Nothing

Exit_Err_msg:
    Exit Sub

Err_msg:
        MsgBox Err.Description
        Resume Exit_Err_msg
End Sub

Any suggestions will be very much appreciated.
Thanks.
B
 

boblarson

Smeghead
Local time
Today, 03:32
Joined
Jan 12, 2001
Messages
32,059
You're just missing the most important part

If the criteria is such that you want to cancel the update, you don't use a recordset object for this. All you do is put

Cancel = True

in your code.
 

Bee

Registered User.
Local time
Today, 11:32
Joined
Aug 1, 2006
Messages
487
That did not work boblarson. I hope I have added it to the right location of my code. I still need to use the record set object to get the condition in the if statment to work right.

Here is the code with the change applied:
Code:
Private Sub PlotNum_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_msg
Dim db As DAO.Database, rs As DAO.Recordset
Dim n As Integer, i As Integer
Dim vPlotNum As Integer
Dim vPhaseID As Integer

vPhaseID = Forms![frmHouse].Form![PhaseID]
vPlotNum = Forms![frmHouse].[qryHouse2].Form![PlotNum]

Set db = CurrentDb
Set rs = db.OpenRecordset("tblHouse")
rs.MoveLast
n = rs.RecordCount
rs.MoveFirst
If n > 0 Then
    For i = 1 To n
        If rs![PhaseID] = vPhaseID Then
            If rs![PlotNum] = vPlotNum Then
                    Cancel = True
                    MsgBox "This plot number already exist in this particular phase." & vbCrLf & "Please choose a different Plot Number"
                    Forms![frmHouse].qryHouse2.Form![PlotNum].Text = ""
            End If
        End If
        rs.MoveNext
    Next i
End If
rs.Close
db.Close
Set db = Nothing
Set rs = Nothing

Exit_Err_msg:
    Exit Sub

Err_msg:
        MsgBox Err.Description
        Resume Exit_Err_msg
End Sub
 

boblarson

Smeghead
Local time
Today, 03:32
Joined
Jan 12, 2001
Messages
32,059
Set a breakpoint and step through the code (F8) to see if it is even getting to that line.
 

Bee

Registered User.
Local time
Today, 11:32
Joined
Aug 1, 2006
Messages
487
It's getting to that line because when I enter a plot Number that already exists, I get the message "This plot number already exist in this particular phase." & vbCrLf & "Please choose a different Plot Number" which is after Cancel = True.
 

boblarson

Smeghead
Local time
Today, 03:32
Joined
Jan 12, 2001
Messages
32,059
It's getting to that line because when I enter a plot Number that already exists, I get the message "This plot number already exist in this particular phase." & vbCrLf & "Please choose a different Plot Number" which is after Cancel = True.


Have you tried using

Cancel=True
Me.Undo

to undo the form's record?

And why are you using the recordset object again? I'm not sure I understand.
 

Bee

Registered User.
Local time
Today, 11:32
Joined
Aug 1, 2006
Messages
487
The Me.Undo seems to have fixed it. The reason I am still using the recordset object is because I need to search through existing records in the database. Thanks :)
 

boblarson

Smeghead
Local time
Today, 03:32
Joined
Jan 12, 2001
Messages
32,059
The Me.Undo seems to have fixed it. The reason I am still using the recordset object is because I need to search through existing records in the database. Thanks :)
Could you not use a couple of DCOUNT functions to do it quicker?
 

Bee

Registered User.
Local time
Today, 11:32
Joined
Aug 1, 2006
Messages
487
I am not familiar with DCOUNT functions. It does it pretty quickly with the code I have got at the moment that's why I did not think about alternatives.
 

boblarson

Smeghead
Local time
Today, 03:32
Joined
Jan 12, 2001
Messages
32,059
Well, a DCount is like a DLookup and it would be two lines (because of the IF) of code instead of the 10-15 lines you have now.
Code:
If DCount("[MyIDField]","MyTableName","[MyIDField]=" & Me!MyIDFieldOnForm)<> 0 Then
   Cancel = True
   Me.Undo
End If
 

Bee

Registered User.
Local time
Today, 11:32
Joined
Aug 1, 2006
Messages
487
If you have noticed in my code, I need to get the value of both phaseNo and PlotNo. I don't know if DCount can do that. I still don't know what it's parameters are, but it looks neater. Is it garanteed to run faster?
 

boblarson

Smeghead
Local time
Today, 03:32
Joined
Jan 12, 2001
Messages
32,059
If you have noticed in my code, I need to get the value of both phaseNo and PlotNo. I don't know if DCount can do that.
Yes, see sample below
I still don't know what it's parameters are, but it looks neater. Is it garanteed to run faster?
As you get more and more records the recordset will start to get slower and slower and the DCount should stay relatively the same.

Code:
If DCount("[MyIDField]","MyTableName","[MyIDField]=" & Me!MyIDFieldOnForm)<> 0 Then
   If DCount("[MyOtherField]","MyOtherTable","[MyOtherID]=" & Me!MyOtherIDOnForm)<>0 Then
         Cancel = True
         Me.Undo
   End If
End If
 

Bee

Registered User.
Local time
Today, 11:32
Joined
Aug 1, 2006
Messages
487
Ok I will try it and see. The code definately looks smaller and less complicated.
 

Bee

Registered User.
Local time
Today, 11:32
Joined
Aug 1, 2006
Messages
487
I hope I have done this right. It did not work though. The code I have written was as follows:

Code:
If DCount("[HouseID]", "tblHouse", "[HouseID]=" & Me!PlotNum) <> 0 Then
   If DCount("[PhaseID]", "tblHouse", "[HouseID]=" & Me!PhaseID) <> 0 Then
         Cancel = True
         Me.Undo
         MsgBox "This plot number already exist in this particular phase." & vbCrLf & "Please choose a different Plot Number"
         Forms![frmHouse].qryHouse2.Form![PlotNum].Text = ""
   End If

Can you spot any flaws please?
 

boblarson

Smeghead
Local time
Today, 03:32
Joined
Jan 12, 2001
Messages
32,059
Did you get an error or did it just not get to the second Dcount?
 

Bee

Registered User.
Local time
Today, 11:32
Joined
Aug 1, 2006
Messages
487
I did not get an error and it got to the second DCount; however, it seems to jump over the Cancel = True ...etc straight to the End if. Does DCount return 0 or 1? I don't understand whey you used <> 0.
 

boblarson

Smeghead
Local time
Today, 03:32
Joined
Jan 12, 2001
Messages
32,059
If DCount returns a 0 then there are no records that match the criteria.

If it returns any number OTHER than 0 then that is the number of records that exist that meets that criteria.
 

Bee

Registered User.
Local time
Today, 11:32
Joined
Aug 1, 2006
Messages
487
Cool, I found out what was wrong, I made a typo. Now it works pretty well. Thanks.
What is the difference if any between using something like Me.ControlName and Me!ControlName?
 

Users who are viewing this thread

Top Bottom