Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DCount Not Working

Posted on 2010-08-26
10
Medium Priority
?
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 77

Accepted Solution

by:
peter57r earned 1000 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:Ronniel Allan Castanito
ID: 33533413
Still have problem with the following line:

DoCmd.RunCommand acCmdUndo
0
 

Author Comment

by:Ronniel Allan Castanito
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 1000 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:Ronniel Allan Castanito
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:Ronniel Allan Castanito
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

721 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