DCount not functioning correctly

matthewnsarah07

Registered User.
Local time
Today, 10:24
Joined
Feb 19, 2008
Messages
192
I have got the following DCount code set up in a command button that is supposed to only allow the frmppe to open if the [Staffname] on MainForm is not present in qryppe.

The code runs fine but no matter whether a name entered on the form is in the query or not the frmppe still opens regardless, What is missing from this code to make it work properly??

Thanks

Code:
Private Sub Command6_Click()
If DCount("[StaffName]", "qryppe", "[StaffName]=' " & Forms("MainForm").Staffname & "'") > 0 Then Exit Sub
DoCmd.OpenForm "frmppe", acNormal
End Sub
 
Put a break point on the 'if' line, execute the code and hoover the mouse over the 'Forms("MainForm").Staffname' part and see what the value is. That's the only thing I can see that would cause a problem...
 
The text box [Staffname] is an unbound text box, could this cause a problem as the value has not been saved or stored?
 
It should be ok. Have you tried the hover?
 
Last edited:
DCount() takes some time to go thru the recordset, depending on its size, and so opening the form may occur before DCount() is finished. Access doesn't wait for one command to be completed before starting the next. If that's the case maybe this would work:

Code:
Private Sub Command6_Click()
If DCount("[StaffName]", "qryppe", "[StaffName]=' " & Forms("MainForm").Staffname & "'") <> 0 Then 
 Exit Sub
Else
 DoCmd.OpenForm "frmppe", acNormal
End If
End Sub

How does StaffName get populated if the textbox isn't bound?
 
Added your suggestion but still does the same

The text box value is just typed in at the moment as the I am still building and testing it
 
I've attached the sample Dbase I am using to try and test this feature before using it anywhere else.

You need to open mainform and enter the dates as From 01/10/2008 and To 31/12/2008. The qryppe takes dates from these.

Maybe you can find what I am missing here, the command 6 button should not open frmppe if either of the names in the current tblppe are entered

Hope you can help?
 

Attachments

I don't have time to really play around with this right now, but from Access Help on DCount()

Domain: A string expression identifying the set of records that constitutes the domain. It can be a table name or a query name for a query that does not require a parameter. I believe qryppe is a parameter query, is it not?
 
This thing has driven me crazy, but I finally got it to work.

First, as stated, you can't use a Parameter Query as the Domain in the DCount(), you'll either have to make the query without a parameter, or use DCount() against the underlying table instead of the query. Either will work.

Secondly, and this is what took so long to spot, in your line

If DCount("[StaffName]", "qryppe", "[StaffName]=' " & Me.txtStaffname & "'") > 0 Then

you have a space between the Single Quote and the Double Quote immediately following

"[StaffName]=

You need to remove that space, so that it reads

If DCount("[StaffName]", "qryppe", "[StaffName]= '" & Me.txtStaffname & "'") > 0 Then
 
Thank you so much for solving this one, I don't think I would have noticed that space for a long time!

Interestingly I got the DCount to work immediately by removing the space and then I added a Between line into the crieria of qryppe again, and it is now working perfectly with criteria rather than a parameter.

Thanks again
 

Users who are viewing this thread

Back
Top Bottom