Counter Problem (1 Viewer)

mohamedmatter

Registered User.
Local time
Today, 03:08
Joined
Oct 25, 2015
Messages
112
I have a problem with the Categories counter for the Category field on the form. When form load , the counters not show number category A Or B Or C , but the category counter appears in the first record on Office 2019

Please help to run all the counters


Note I had previously set it up in another database and it was working fine on Office 2013
i attach database
 

Attachments

  • counter problem.accdb
    548 KB · Views: 92

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:08
Joined
Feb 28, 2001
Messages
27,194
You have a useless line in two of your VBA event routines. The statement

Code:
DIM CboXX, CC, CB, CA As String

has two flaws. One, you only declared one string - CA. The other three variables are Variants. That's because you need that var AS datatype in EACH segment of the DIM statement. Any named element in a DIM that doesn't include the "AS" portion is a variant.

But that error actually doesn't hurt since you don't USE those variables anyway. Your code is using Me.CboXX, Me.CA, Me.CB, and Me.CC - all of which are valid controls on your form. Since you always use the Me. prefix, the variables never get referenced.

I can tell you that the reason the counters don't show in the LOAD routine is that IF statement that checks the value of Me.CboXX, which technically on a freshly loaded form isn't anything until a selection is made with the combo box. That is, unless I missed a default (I admit I didn't check), a combo has no value until it is used OR unless you use the long form of combo reference that lets you see the value in any row and column of the combo box.

When I use the combo box, it loads up a counter, though I noticed that the count it loaded wasn't correct. Haven't figured that out yet.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:08
Joined
May 7, 2009
Messages
19,247
you remove the If..End If and just count your table:
Code:
Private Sub CboXX_Change()
Dim CboXX, CA, CB, CC As String
CboXX = Me.CboXX & ""
Me.CA = DCount("cat", "atbl", "cat='" & CboXX & "'")
Me.CB = DCount("cat", "atbl", "cat='" & CboXX & "'")
Me.CC = DCount("cat", "atbl", "cat='" & CboXX & "'")
End Sub

Private Sub Form_Load()
Dim CboXX, CA, CB, CC As String
CboXX = Me.CboXX & ""
Me.CA = DCount("cat", "atbl", "cat='" & CboXX & "'")
Me.CB = DCount("cat", "atbl", "cat='" & CboXX & "'")
Me.CC = DCount("cat", "atbl", "cat='" & CboXX & "'")
End Sub
 

mohamedmatter

Registered User.
Local time
Today, 03:08
Joined
Oct 25, 2015
Messages
112
You have a useless line in two of your VBA event routines. The statement

Code:
DIM CboXX, CC, CB, CA As String

has two flaws. One, you only declared one string - CA. The other three variables are Variants. That's because you need that var AS datatype in EACH segment of the DIM statement. Any named element in a DIM that doesn't include the "AS" portion is a variant.

But that error actually doesn't hurt since you don't USE those variables anyway. Your code is using Me.CboXX, Me.CA, Me.CB, and Me.CC - all of which are valid controls on your form. Since you always use the Me. prefix, the variables never get referenced.

I can tell you that the reason the counters don't show in the LOAD routine is that IF statement that checks the value of Me.CboXX, which technically on a freshly loaded form isn't anything until a selection is made with the combo box. That is, unless I missed a default (I admit I didn't check), a combo has no value until it is used OR unless you use the long form of combo reference that lets you see the value in any row and column of the combo box.

When I use the combo box, it loads up a counter, though I noticed that the count it loaded wasn't correct. Haven't figured that out yet.
Thank you very much for your wonderful analysis and your convincing response. But I have used these counters and they were working fine on Office 2013. But I don't know what happened when I tried to repeat the experience on Office 2019. Do you have any idea to correct this error using the if function?
 

mohamedmatter

Registered User.
Local time
Today, 03:08
Joined
Oct 25, 2015
Messages
112
you remove the If..End If and just count your table:
Code:
Private Sub CboXX_Change()
Dim CboXX, CA, CB, CC As String
CboXX = Me.CboXX & ""
Me.CA = DCount("cat", "atbl", "cat='" & CboXX & "'")
Me.CB = DCount("cat", "atbl", "cat='" & CboXX & "'")
Me.CC = DCount("cat", "atbl", "cat='" & CboXX & "'")
End Sub

Private Sub Form_Load()
Dim CboXX, CA, CB, CC As String
CboXX = Me.CboXX & ""
Me.CA = DCount("cat", "atbl", "cat='" & CboXX & "'")
Me.CB = DCount("cat", "atbl", "cat='" & CboXX & "'")
Me.CC = DCount("cat", "atbl", "cat='" & CboXX & "'")
End Sub
Thank you for your great effort and your interest in answering my question. I would like you to help me correct this function. I am an amateur and not a professional like you, I have used my simple knowledge and I want you to give me the names of the sources that I rely on to write vba in an easy way. thank you very much
 

mohamedmatter

Registered User.
Local time
Today, 03:08
Joined
Oct 25, 2015
Messages
112
Thank you for your great effort and your interest in answering my question. I would like you to help me correct this function. I am an amateur and not a professional like you, I have used my simple knowledge and I want you to give me the names of the sources that I rely on to write vba in an easy way. thank you very much
you remove the If..End If and just count your table:
Code:
Private Sub CboXX_Change()
Dim CboXX, CA, CB, CC As String
CboXX = Me.CboXX & ""
Me.CA = DCount("cat", "atbl", "cat='" & CboXX & "'")
Me.CB = DCount("cat", "atbl", "cat='" & CboXX & "'")
Me.CC = DCount("cat", "atbl", "cat='" & CboXX & "'")
End Sub

Private Sub Form_Load()
Dim CboXX, CA, CB, CC As String
CboXX = Me.CboXX & ""
Me.CA = DCount("cat", "atbl", "cat='" & CboXX & "'")
Me.CB = DCount("cat", "atbl", "cat='" & CboXX & "'")
Me.CC = DCount("cat", "atbl", "cat='" & CboXX & "'")
End Sub
I noticed an error when using your code. Where I counters are the same number. I would like the first counter is the symbol "a", the second counter is the symbol "b" and the third counter is the symbol "c". As in the attached picture.
I misinterpreted my point, I sincerely apologize
counter ccounter bcounter a
342
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:08
Joined
Sep 21, 2011
Messages
14,319
Seems to work fine for me as you had it? I would have a common sub though and just call it from where you need it?
Why not just show all the counts regardless of combo? What is this trying to do?
Code:
Private Sub CboXX_AfterUpdate()
    Call CalcCat
End Sub

Private Sub CboXX_Change()
 Call CalcCat
End Sub

Private Sub Form_Load()
    'Call CalcCat
End Sub
Sub CalcCat()
        Me.CA = DCount("cat", "atbl", "cat='a'")
        Me.CB = DCount("cat", "atbl", "cat='b'")
        Me.CC = DCount("cat", "atbl", "cat='c'")
End Sub
 

mohamedmatter

Registered User.
Local time
Today, 03:08
Joined
Oct 25, 2015
Messages
112
Seems to work fine for me as you had it? I would have a common sub though and just call it from where you need it?
Why not just show all the counts regardless of combo? What is this trying to do?
Code:
Private Sub CboXX_AfterUpdate()
    Call CalcCat
End Sub

Private Sub CboXX_Change()
Call CalcCat
End Sub

Private Sub Form_Load()
    'Call CalcCat
End Sub
Sub CalcCat()
        Me.CA = DCount("cat", "atbl", "cat='a'")
        Me.CB = DCount("cat", "atbl", "cat='b'")
        Me.CC = DCount("cat", "atbl", "cat='c'")
End Sub
thanks.but i found problem
when close form and open it counter blank
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:08
Joined
Sep 21, 2011
Messages
14,319
thanks.but i found problem
when close form and open it counter blank
You have to start doing some of the work yourself? :(

Uncomment the line in the Form load then.
I did it that way, as when the form is opened, NO value is in the combo, so no value would be shown.?
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:08
Joined
Feb 28, 2001
Messages
27,194
But I have used these counters and they were working fine on Office 2013.

If they were used in the same exact code in Office 2013, they suffered the same exact problem. Because you prefixed them with Me. and because their names are exactly the same as four of the five controls on your form, they were never used.

Code:
DIM CboXX, CC, CB, CA As String
...
If Me.CboXX = "a" Then ...

The VARIABLE CboXX is NOT repeat NOT used by Me.CboXX because they are in totally different contexts. Me.CboXX refers to the CONTROL on the form, which happens to have the same name as the variable. They are TWO DIFFERENT THINGS. With the Me. prefix, you use the control. Without the prefix, you would use the variable. That prefix is called a "qualifier" in that it qualifies the location of something. You use it to differentiate between two things with identical names but different locations.

I'll agree that things worked the same way in Ac2013 and other versions of Access with regard to this declaration. Which is to say that the variables would not have made a difference then, either. But Gasman's comments and suggestions are spot-on. Arnelgp's comments are also correct.
 

mohamedmatter

Registered User.
Local time
Today, 03:08
Joined
Oct 25, 2015
Messages
112
You have to start doing some of the work yourself? :(

Uncomment the line in the Form load then.
I did it that way, as when the form is opened, NO value is in the combo, so no value would be shown.?
thanks
 

Users who are viewing this thread

Top Bottom