473,395 Members | 1,558 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.

How do I code a command button

Michael Adams
I have a form that is attached to a query. I have a drop down combo box that has employees, a text box that should do a count of "incidents" from the Type field in the query when I click the count button. what is the code needed for this to work?
Jul 14 '10 #1
15 5038
jbt007
40
@Michael Adams
I would look into using myCount = DCount([What], [Where], [Criteria]). Then set the textbox value to myCount. Put the code on the button's "On Click" event.
Jul 14 '10 #2
OK, maybe I am not doing this right. I entered the code you told me to on the buttons "OnClick" then in the text box default value I set to myCount. When I click the button I get this error "The expression On Click you entered as the event property setting produced the following error: The object doesn't contain the Automation object 'mycount'.
Jul 14 '10 #3
jbt007
40
@Michael Adams
Michael,
Sorry, I'll be more specific. Here's the code for the on click event:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdGetCount_Click()
  2.     Dim myCount As Integer
  3.     myCount = DCount("[fldNameToCount]", "tableName")
  4.     Me.txtMyCount.Value = myCount
  5.     Me.Refresh
  6. End Sub
The code above assumes you have a textbox on the form named "txtMyCount" and a command button on the form named "cmdGetCount". It also assumes your table is in the same database as the form. You will need to substitute your field name into "fldNameToCount", leaving the brackets [], and substitute your table name in place of "tableName". If your text box and command buttons are named differently, you will also need to make appropriate substitutions.

For more details about "DCount" click on the word "DCount" in the VBA Editor and hit F1.

Hope this helps...
Jul 14 '10 #4
That works out great, now I would like to separate the count according to the employee in the field "Who"
Jul 14 '10 #5
jbt007
40
What you want to use is the "criteria" part of the DCount. It can be kind of problematic if depending on what you are counting. If you are counting employee's names DCount will count all employees with the same name. (John Smith - for example may be several different people.) It's best if you can count an employee ID filed so you are sure you are only counting entries for a specific employee.

Having said that, (And because you haven't posted any specific information about your table structure or field names) I'll give an example on how to count an employee's name matching a form's field.

Expand|Select|Wrap|Line Numbers
  1. DCount("[fldToCount]", "TableName", _
  2.    "[EmployeeID] = '" & Me.cmbxEmpID.Value & "'")
The above is air code so you may need to tweak it a bit. The idea is that the end result Access is looking for is "DCount(... EmployeeID = 'THO2314'). So if a person used ComboBox cmbxEmpID to pick the ID "THO2314" and then clicked the count command button, you would only count records where the EmployeeID field was "THO2314".

You should make allowance for what if the combo box is never clicked?

Do this by setting a default value "All" and then use an If, then, else statement that evaluates the value of the combobox before you try to count the data. If it has "All" selected, do the DCount without any criteria, otherwise, use the combobox value to filter the data.

Another way would be to disable the "Count Data" button until the combobox is updated. On the "Update" event of the combo box, put code that enables the command button.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmbxEmpID_Update()
  2.     Me.CmdCountEmpID.Enabled = True
  3. End Sub
Hope this helps...
Jul 14 '10 #6
nico5038
3,080 Expert 2GB
Hmm, guess your form query hold both employees and incidents.
When you want a count of the incidents from the Type field per employee you can create a GroupBy query like:
Expand|Select|Wrap|Line Numbers
  1. select employeeID, sum(Type) from qryYourForm group by employeeID
  2.  
Now you can add a subform for this query and accept the wizard option to link the subform.
Access will now synchronize the subform with the employee of the mainform.

Getting the idea ?

Nic;o)
Jul 14 '10 #7
jbt007
40
@nico5038
Nic;o) - Another way to skin the cat... I wasn't sure if he wanted to change his form or not...
Jul 14 '10 #8
There is still one problem, I have a query with the following fields: employee (this is the long name), CUDate(this is the date entered) WorkOrderNumber, type(this is the type of Work Order), WorkedBy (this is the intials of the employee).

I have used this code to list the count of three different types into text boxes.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Sub_Cmd_Click()
  2.     Dim typeCount As Integer
  3.     typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[Type]= 'Incident'")
  4.     Me.Inc_Text.Value = typeCount
  5.     Me.Refresh
  6.     typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[Type]= 'Request'")
  7.     Me.Req_Text.Value = typeCount
  8.     Me.Refresh
  9.     typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[Type]= 'Change Order'")
  10.     Me.CO_Text.Value = typeCount
  11.     Me.Refresh
  12. End Sub
the problem that I am running into is that before this code I need to sort the query by the field "workedby" then separte the count into the three fields listed in the above code.

What would I code first? I have tried
Expand|Select|Wrap|Line Numbers
  1. [Private Sub Sub02cmd_Click()
  2.     Dim empcount As Integer
  3.     empcount = DCount("[WorkedBy]", "Emp_Lst_Frm_Query", "[workedby] = '" & Me.empcmb.Value & "'")
  4.     Me.Sub02txt.Value = empcount
  5.     Me.Refresh
  6. End Sub]
But it errors out. The code is correct until it gets to the criteria part of it. by the way, I am getting the data in empcmb from the table employee with the field name of initials.

What am I doing wrong?
Jul 15 '10 #9
nico5038
3,080 Expert 2GB
The easy way would be a group by query as posted before, like:
Expand|Select|Wrap|Line Numbers
  1. select workedby, Type, Count(*) 
  2. from Emp_Lst_Frm_Query
  3. group by workedby, Type
  4. orde by 1, 2
  5.  
Using this new query in a subform linked with the workedby field to the mainform based on Emp_Lst_Frm_Query, will give the three needed counts. When another Type is added, this won't effect your code, as the groupby will handle this :-)

Nic;o)
Jul 15 '10 #10
jbt007
40
@Michael Adams
Michael,

Your code looks ok from what I can see. Try some of the following:
  1. What's the error message?
  2. Are you sure the value in Me.empcmb combo box is in the db?
  3. The integer type only goes up to about 32k, if there are more than 32k records try "Dim empcount As Long".
  4. All the "D" functions can be tested in the immediate window. Place a pause in your VBA code on the DCount line and then run the form. When you click the Sub02cmd button the program will stop on that line. You may then type:
    Expand|Select|Wrap|Line Numbers
    1. ?DCount("[WorkedBy]", "Emp_Lst_Frm_Query", "[workedby] = '" & Me.empcmb.Value & "'")
    in the immediate window and it should tell you the count.

    You may also type:
    Expand|Select|Wrap|Line Numbers
    1. ?Me.empcmb.Value
    and hit enter to see what the value is in the combo box.
  5. Try trimming both the field value and the combo box value. It could be they are not the same length. This would look like:
    Expand|Select|Wrap|Line Numbers
    1. DCount("[WorkedBy]", "Emp_Lst_Frm_Query", "Trim([workedby]) = '" & Trim(Me.empcmb.Value) & "'")
  6. Finally - try buying VBA For Dummies by John Paul Mueller. Good basic book to get you started. I still refer to mine from time to time.

Hope this helps...
Jul 16 '10 #11
Ok to answer your question, when I used the me.value the return was a number representing the line on which the Intials were stored in the table. I then changed that ot a me.empcmb.text and that returned the correct Intials of the combo box. However, what is happening is the query is returning the correct number of results regarding the .text but when I go to sort those results the code sorts the whole query not the sorted query based on the .text result.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Sub02cmd_Click()
  2.     Dim typeCount As Integer
  3.     Dim empcount As Integer
  4.  
  5.     empcount = DCount("[WorkedBy]", "Emp_Lst_Frm_Query", "[WorkedBy]= ' " & Me.empcmb.Value & " '")
  6.     Me.Inc_Text.Value = empcount
  7.     Me.Refresh
  8.  
  9. empcmb=typeCount
  10. typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[Type]= 'Incident'")
  11.     Me.Inc_Text.Value = typeCount
  12.     Me.Refresh
  13. End Sub
  14.  
The net count should be 7; however, the code returns 8 because that is the total in the query.
Jul 16 '10 #12
jbt007
40
@Michael Adams
So list for me the objects on your form and what they are. From the name empcmb I was assuming this was a combo box. Is that true or is it a text box?

I am not following line 9 in your code: "empcmb=typeCount". What are you doing here?

As far as I know, sorting a query has no effect on the number of items in it. Why would your sort make a difference? I am not following this either. How are you sorting the query? Post the SQL statement for Emp_Lst_Frm_Query. (From the edit mode, click View, SQL.)
Jul 16 '10 #13
I finally got it to work the way I wanted to. I ended up using a case statement based on the value of [workedby] = me.empcmb.value. Here is the final code for the button.
Expand|Select|Wrap|Line Numbers
  1.     Select Case typeCount = DCount("[workedby]", "Emp_Lst_Frm_Query", "[workedby]= '" & Me.empcmb.Value & "'")
  2.     Case Me.empcmb.Value = "1"
  3.         typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[workedby] = 'MJA'")
  4.         Me.TotalTxt.Value = typeCount
  5.         Me.Repaint
  6.         typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[Type] = 'Incident' And [workedby] = 'MJA'")
  7.         Me.Inc_Text.Value = typeCount
  8.         Me.Repaint
  9.         typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[Type] = 'Request' and [workedby] = 'MJA'")
  10.         Me.Req_Text.Value = typeCount
  11.         Me.Repaint
  12.         typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[Type] = 'Change Order' and [workedby]= 'MJA'")
  13.         Me.CO_Text.Value = typeCount
  14.         Me.Repaint
  15.     Case Me.empcmb.Value = "2"
  16.         typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[workedby] = 'MOB'")
  17.         Me.TotalTxt.Value = typeCount
  18.         Me.Repaint
  19.         typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[Type] = 'Incident' And [workedby] = 'MOB'")
  20.         Me.Inc_Text.Value = typeCount
  21.         Me.Repaint
  22.         typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[Type] = 'Request' and [workedby] = 'MOB'")
  23.         Me.Req_Text.Value = typeCount
  24.         Me.Repaint
  25.         typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[Type] = 'Change Order' and [workedby]= 'MOB'")
  26.         Me.CO_Text.Value = typeCount
  27.         Me.Repaint
  28.     Case Else
  29.         typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[workedby] = 'SLT'")
  30.         Me.TotalTxt.Value = typeCount
  31.         Me.Repaint
  32.         typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[Type] = 'Incident' And [workedby] = 'SLT'")
  33.         Me.Inc_Text.Value = typeCount
  34.         Me.Repaint
  35.         typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[Type] = 'Request' and [workedby] = 'SLT'")
  36.         Me.Req_Text.Value = typeCount
  37.         Me.Repaint
  38.         typeCount = DCount("[type]", "Emp_Lst_Frm_Query", "[Type] = 'Change Order' and [workedby]= 'SLT'")
  39.         Me.CO_Text.Value = typeCount
  40.         Me.Repaint
  41.     End Select
Jul 19 '10 #14
jbt007
40
@Michael Adams
Great! Hope I helped...
Jul 19 '10 #15
Everyone helped out great. Thanks so much for all the replies.
Jul 19 '10 #16

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

Similar topics

13
by: Samantha Smit | last post by:
Hi, I am trying to create a simple asp page that has one command button that updates a database. The URL of the page is like this: http://MyServer.com/Update.asp?UserName=Tom My asp code is...
4
by: MJW | last post by:
Is there a way for me to know if or which command button was just clicked that triggers the LostFocus event for the current control on a Form? I have a form that has many types of users who each...
9
by: Melissa | last post by:
What is the code to delete a command button from a form? Can the code be run from the click event of the button to be deleted? Thanks! Melissa
0
by: Lauren Quantrell | last post by:
I use the following code to create text edit fields and command buttons in my toolbars: Function CreateToolbarObject(myObjectType as integer) Dim newObject Select Case myObjectType Case 1...
14
by: Kevin | last post by:
A couple of easy questions here hopefully. I've been working on two different database projects which make use of multiple forms. 1. Where's the best/recommended placement for command buttons...
2
by: Darren | last post by:
Hi, I have a command button which has a macro running in it. The macro on the click event, runs to ensure that certain values in a form are valid, (e.g. the textboxes are not null) and...
4
by: Alex Chun | last post by:
How can I program a command button on a form to bring up the standard "Find and Replace" dialog on click? Separate but related: how do you run menu commands (e.g. "Edit" "Find") from VB? ...
4
by: John Smith | last post by:
I have a continuous form. there is a command button with the following code for the OnClick event: DoCmd.OpenForm "frmPlants", , , "PlantsID =" & Me!PlantsID I click the button and frmPlants...
2
by: Senthil | last post by:
Hi All I need to create an Excel report and create a command button and have to run a macro on the click event that will print all the pages in the Excel workbook. I am able to create the report...
1
by: Chris | last post by:
I need a search form to perform. I have a Form and a subform. The subform is based off a query and the main form is unbound. The query runs perfect, however, I have a command button in the main...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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...

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.