Solved

DCount Not Working

Posted on 2010-08-26
10
316 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

840 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