473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Why does DCount return a wrong result using VBA?

176 100+
I have what seems to me a strange problem. I have a continous form, toggle buttons of a [ToAdd] field, a text control, and a command's button caption. When I write the following in the text control's source:

=DCount("Id_Customer","Distribution","ToAdd=True")
I get the correct result all the time, but when I write in code -

Expand|Select|Wrap|Line Numbers
  1. Private Sub ToAdd_Click()
  2.     Me.Command8.Caption = Str(DCount("Id_Customer", "Distribution", "ToAdd=True"))
  3.     Me.Requery
  4. End Sub
, once I click on the toggle button, I get a result which is bigger by 1 than the correct result , when I click on the same toggle button again, the result I get is by 1 smaller then that correct result (that which is being shown in the text control).

When I click a toggle button of a different record/row, the result I get is always by 1 smaller than the correct count result.

Bewildered.
Does anyone know what is the source of this?
Thanks.
Dec 10 '06 #1
4 2341
NeoPa
32,556 Expert Mod 16PB
Is it because the recordset hasn't been updated yet?
Perhaps the .Requery should come before the DLookup code?
It's hard to follow the logic fully as there is so much we don't know of your situation. That's the only thing I can think of from what you've said.
Dec 11 '06 #2
MMcCarthy
14,534 Expert Mod 8TB
Try removing the Str() function from around the DCount as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ToAdd_Click()
  2.     Me.Command8.Caption = DCount("Id_Customer","Distribution","ToAdd=True")
  3.     Me.Requery
  4. End Sub
  5.  
If it won't let you pass the number into the caption let me know.

Mary
Dec 11 '06 #3
Michael R
176 100+
Yes, the .Requery should've come before DCount. The trouble wasn't in getting the value into Caption, as MsgBox (Dcount...) had also beein returning the wrong value.

Thanks!

So, when the expression (Dcount...) is a source of a text control, and I click a command button that will alter the count, I need a Requery for that commands' button code. When the expression is in VBA, I need Requery after the expression. Is this means that VBA runs before the calculated controls of the form?

Thanks again for your help.
Dec 11 '06 #4
NeoPa
32,556 Expert Mod 16PB
Unless you'd previously moved to another record, the VBA code in the ToAdd_Click() procedure would run first.
Dec 11 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dan Leeder | last post by:
stroccur = DCount("", "empnotes", " = " & Chr(34) & Me.Rpt_Card_Type & Chr(34) & " And = " & Me.SSN & " And #" & & "# > " & DateSerial(Year(Me.datetime) - 1, Month(Me.datetime),...
6
by: Mike Conklin | last post by:
This one really has me going. Probably something silly. I'm using dcount for a report to determine the number of different types of tests proctored in a semester. My report is based on a...
2
by: blongmire | last post by:
.... I know just enough to be dangerous, but the real danger is that I might fall asleep and hit my head on my keyboard waiting for this code to finish executing. Some preliminaries: WinXP Pro,...
3
by: BerkshireGuy | last post by:
I am having difficulty with using the Dcount function. I am trying to return the number of records from qryIndividualFeedbackDetail where the TimelyManner field is set to 4. So, in the new...
7
by: Daniel Rudy | last post by:
Hello, I have a peice of code that I'm making an attempt to code. The problem is that I need to return an arbitrary number of char strings. int function(char *fname, int *dcount, char *data)...
52
by: Julie | last post by:
I'm supporting an application at work. Below are some code segments that I can't understand how they work. First let me say, I would never code this standard. I'm just really creeped out that it...
2
by: Wingz | last post by:
Hiya, Fairly new to Access and was wondering what the best way to perform Dcounts on groups in an Access report. For example, I have 10 employees and the different instances of jobs they can...
36
by: bmyers | last post by:
Good afternoon, I am attempting to count only those records within a report, which is based on a query, where Status is equal to Closed. I have tried multiple variations of DCOUNT but am...
3
by: trafford | last post by:
I want to use Dcount to return a value that I can post back into a report. This must be straightforward but I can't get it to work! The text below is in the report open event. Text44 is an unbound...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.