473,395 Members | 2,006 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.

count table records based on selected items in combo box as condition using VBA code

7
I have a combo box with three items that link to the field in a table. What i want is to count the the total table records when i select each of the items in combo box. Example, when i select "Employed" in the combo box then all the records with the status Employed will be counted.
Mar 14 '13 #1

✓ answered by Seth Schrock

It is possible to do it the way that you have setup (after a few errors are fixed), however there is an easier way. Using the Domain functions that Z suggested. Personally, I would have a message box pop up when you selected which one you wanted from the combo box. This way you could easily find out how many unemployed students you have without making any design changes. In the combo box's AfterUpdate event, I would put
Expand|Select|Wrap|Line Numbers
  1. Dim intCount as Integer
  2. intCount = DCount("*", "Student", "EmpStatus = " & Me.CboEmpStatus)
  3. MsgBox "Student Count: " & intCount

11 3856
Seth Schrock
2,965 Expert 2GB
You would use the Count(*) to count the records and then have the WHERE clause filter the results based on your selections. For example, if your field is EmployedStatus and your combo box was named MyCombo on a form named MyForm, your WHERE clause would look like this:
Expand|Select|Wrap|Line Numbers
  1. WHERE EmployedStatus = Forms!MyForm!MyCombo
Mar 14 '13 #2
Coucou
7
I am beginner with this, Could you be a little more detail of that, would be better if you can give lines of sample code with that. Thanks
Mar 14 '13 #3
Seth Schrock
2,965 Expert 2GB
To do that I need more information from you. What is the table structure for the table that you are searching in? What is the row source for the combo box? What have you tried and what were the results?
Mar 14 '13 #4
zmbd
5,501 Expert Mod 4TB
Coucou:
There are also the domain functions and in this case, they may be suitable.
For what you want, open the form in design mode.
Right click the combobox that you have your list in and show the properties box.
One way: is to set the Dcount() function as the control source in a textbox on your form. (I'd probably use an IIF to keep an error message out of the box while the CBO is empty.)

Examples of expressions

Another method is to use vba:
Click on the events tab
Click on the after update event
Select the "code builder" option
This will open the VBA editor to your form's code module and insert the private sub key phrases.
You will then need to write your vba code...
In this case, as I said, the DCount() function should work (type it in the vba window, select, press [F1] for syntax) to return your record count and then you just need to display that information in a text box value or label caption


Which option is best depends on your form and database design, and the size of your dataset.
Mar 14 '13 #5
Coucou
7
I created a table Student(ID,Name,...,EmpStatus) where EmpStatus tells if a student is employed or unemployed. So I like to have a form with combo box so that i just need to select the combo box and then it returns with the total number of those who employed or unemployed. What i have tried but it didnt work was:

Expand|Select|Wrap|Line Numbers
  1. For i = 1 To CboEmpStatus.ListCount - 1
  2. If CboStatus.Selected(i) = True Then
  3. str = "select * from Student where EmpStatus=" & CboEmpStatus.Column(0, i)
  4. Set db = CurrentDb
  5. Set rst = db.OpenRecordset(str)
  6. TxtCount = rst.RecordCount
Mar 14 '13 #6
Seth Schrock
2,965 Expert 2GB
It is possible to do it the way that you have setup (after a few errors are fixed), however there is an easier way. Using the Domain functions that Z suggested. Personally, I would have a message box pop up when you selected which one you wanted from the combo box. This way you could easily find out how many unemployed students you have without making any design changes. In the combo box's AfterUpdate event, I would put
Expand|Select|Wrap|Line Numbers
  1. Dim intCount as Integer
  2. intCount = DCount("*", "Student", "EmpStatus = " & Me.CboEmpStatus)
  3. MsgBox "Student Count: " & intCount
Mar 14 '13 #7
zmbd
5,501 Expert Mod 4TB
In post #6 where you are using the RecordCount method. I can only assume that you are using the DAO record method; thus, you must do somthing along the lines of (please note: I am entering the following from memory therefor the syntax may be subject to error):
Expand|Select|Wrap|Line Numbers
  1. With rs
  2.     .movelast
  3.     .movefirst
  4.     lngRecordCount = .recordcount
  5. end with
It's a fluke. The recordcount will always return 1 if there is at least 1 record; however, until you move to the last record and then back to the first record you will not receive the true count.

Why do this the hard way with the record set?
Mar 14 '13 #8
Coucou
7
Well tried with dcount function but it worked well with the StudID field but not the EmpStatus field which datatype is Text. Is it a problem with datatype?
Mar 15 '13 #9
Seth Schrock
2,965 Expert 2GB
There isn't any issue with the text data type, but you should look at the combo box to see if the field that it is bound to is a number field or a text field. If you look at the row source, it should be the name of a table, query, or it could have just some SQL code (which would start with SELECT). If the first field in the row source is a number field, then we would need to change the DCount() function to look at the text column instead of the number column of the textbox. Line 2 of my code block in post 7 would end with
Expand|Select|Wrap|Line Numbers
  1. Me.CboEmpStatus.Column(1)
The column numbers start at 0, so the second column has an index of 1.
Mar 15 '13 #10
Coucou
7
Finally i got it now! Thanks for your help on this!
Mar 18 '13 #11
Seth Schrock
2,965 Expert 2GB
Glad to be able to help.
Mar 18 '13 #12

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

Similar topics

5
by: Terri | last post by:
I have a form with a multi-select combo. I dynamically build a SELECT statement, open a report, and set the recordsource to my dynamic SELECT statement. I count the records returned in the report...
1
by: JRT | last post by:
I have a continuous form with 3 comboxes, combo1, combo2 combo3. The choices in combo2 are dtermined the what is selected in combo1 by using a where = combo1 statement. The combo is refreshed...
2
by: Geir Baardsen | last post by:
Hi! From a listbox I'd like to send only selected items to a report. Items will include: OrderNr,Date,EmployeeNr from tblOrders ZipCode,City from tblZipCodes Name,Adr,ZipID from...
5
by: Keith | last post by:
I've tried various combinations of quotation marks and asterisks but I can't see to get the syntax right. Could someone post a working example? Many thanks. Keith.
0
by: henry fung | last post by:
Hi, How do you create a shoutcut to a table in an existing group using code only in Access 2003. The closest I can do is to create a shoutcut to a selected object to a new group using:...
0
by: lj | last post by:
How do I get an asp.net datagrid to set certain records as tedittemplate based on a certain condition. It is for a simple workflow. The user will have permission to see a list of records. He...
6
by: Kazza | last post by:
Hi I'm using Access 2003 and I need to figure out how to display the record details based on a combo box selection. For example, when I select a company name in a combo box, how do I get the related...
4
by: datasec23 | last post by:
I have read through the threads and have been unable to find an answer to my simple question and this newbie is stuck. Here is what I am trying to do. I have a combo box that contains a list of...
3
by: Michael R | last post by:
Hi all. I'm writing a report that utilizes a query called qryRecords SELECT * FROM tblRecordsORG1; however, there is tblRecordsORG1, tblRecordsORG2, etc. qryRecords should be based on...
6
by: BEETHOVEN | last post by:
I have an option group called Issue_Type on my main form F1_Member_Demographics_Main. When I select one of the 3 options on the main form from the option group Issue_Type I want to limit the sub...
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.