Solved

DCount Not Working

Posted on 2010-08-26
10
317 Views
Last Modified: 2012-06-21
Hi Experts,
Can you please help me with the following. I don't know why the following code isn't working.

According to the sample data, a msgbox should appear but it doesn't if i were to repeatedly click on the cmdbutton.

Thanks
Ric

Private Sub Command9_Click()
Dim rs As dao.Recordset
Set rs = CurrentDb.OpenRecordset("tblClassMeeting")
    With rs
         .AddNew
         !ClassProductID = Me.ClassProductID
         !ClassDate = Me.ClassDate
         .Update
    End With
rs.Close
Call ClassMeetingUpdate_AfterUpdate
End Sub

Private Sub ClassMeetingUpdate_AfterUpdate()
If DCount("[ClassMeetingID]", "tblClassMeeting", "[ClassProductID]=" & Me.ClassProductID & " and [ClassDate] = " & Me.ClassDate) > 0 Then
        MsgBox "Duplicate Entry"
Cancel = True
      DoCmd.RunCommand acCmdUndo
      DoCmd.GoToRecord , , acNewRec
   End If
End Sub

Open in new window

ClassMeetingForm.jpg
ClassMeetingSampleData.jpg
0
Comment
Question by:RiCzN
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 77

Accepted Solution

by:
peter57r earned 250 total points
ID: 33533263
......and [ClassDate] =#" & format(Me.ClassDate, "yyyy-mm-dd") & "#")
0
 
LVL 42

Expert Comment

by:dqmq
ID: 33533270
Probably need delimiters on the date:

DCount("[ClassMeetingID]", "tblClassMeeting", "[ClassProductID]=" & Me.ClassProductID & " and [ClassDate] = #" & Me.ClassDate)  & "#"
0
 
LVL 2

Expert Comment

by:bgrandjean
ID: 33533331
You need #s around your date to indicate it is a date in the where condition, try it like this:

If DCount("[ClassMeetingID]", "tblClassMeeting", "[ClassProductID]=" & Me.ClassProductID & " and [ClassDate] = #" & Me.ClassDate & "#") > 0 Then
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:RiCzN
ID: 33533413
Still have problem with the following line:

DoCmd.RunCommand acCmdUndo
0
 

Author Comment

by:RiCzN
ID: 33533539
When i use Me.Undo, the last record would Undo but using the DoCmd doesn't. Would it be a problem if i used Me.Undo instead?
0
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 250 total points
ID: 33533549
There is nothing to UNDO.  You need to put the DCOUNT logic BEFORE you update the recordset.

Also, I recommend you reconsider the primary key for ClassMeeting table.  It should prevent the duplication you are concerned about.  Alternatively, you can add a no-duplicates index on ClassProductID + ClassDate.    
0
 
LVL 2

Expert Comment

by:bgrandjean
ID: 33533582
I think you are too late to do an Undo at this point.  You should do the check for an existing item before you do the add.
0
 

Author Comment

by:RiCzN
ID: 33533619
How do i set up no-duplicates for ClassProductID + ClassDate?
Is it just a matter of selecting Index: Yes (No duplicates) for the two fields?
IndexNoDuplicates.jpg
0
 

Author Closing Comment

by:RiCzN
ID: 33533842
Thanks
0
 
LVL 42

Expert Comment

by:dqmq
ID: 33534025
Creating an index like THAT only allows you to have one column in the index. In other words, you will end up with a separate unique index on each column. From table design, you need to open the Indexes menu.  There, you can create an index with multiple columns.  
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question