Đếm Record theo mã(trong Access)

Liên hệ QC

ontopoftheworld

Am a Peasant Guy
Tham gia
17/3/07
Bài viết
109
Được thích
182
Trong Excel để đếm data theo mã hiệu cho trước thì ta dùng hàm countif. Tương tự trong Access ta có thể dùng cách nào để đếm số record theo mã quy ước cho trước(giống như mã hàng, mã chứng từ).
Nhờ các bác chỉ giúp.
 
Trong Excel để đếm data theo mã hiệu cho trước thì ta dùng hàm countif. Tương tự trong Access ta có thể dùng cách nào để đếm số record theo mã quy ước cho trước(giống như mã hàng, mã chứng từ).
Nhờ các bác chỉ giúp.
Muốn đến số record thì bạn dùng SQL (trong access là select query), cú pháp như sau:

PHP:
Select count(Tên cột/Mã quy ước) from tbBangDuLieu where (điều kiện gì đó)
 
Đây là phần Help của DCount(expr, domain, [criteria])

DCount Function
See AlsoApplies ToExampleSpecificsYou can use the DCount function to determine the number of records that are in a specified set of records (a domain). Use the DCount function in Visual Basic, a macro, a query expression, or a calculated control.

For example, you could use the DCount function in a module to return the number of records in an Orders table that correspond to orders placed on a particular date.

DCount(expr, domain, [criteria])
The DCount function has the following arguments.

Argument Description
expr An expression that identifies the field for which you want to count records. It can be a string expression identifying a field in a table or query, or it can be an expression that performs a calculation on data in that field. In expr, you can include the name of a field in a table, a control on a form, a constant, or a function. If expr includes a function, it can be either built-in or user-defined, but not another domain aggregate or SQL aggregate function.
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.
criteria An optional string expression used to restrict the range of data on which the DCount function is performed. For example, criteria is often equivalent to the WHERE clause in an SQL expression, without the word WHERE. If criteria is omitted, the DCount function evaluates expr against the entire domain. Any field that is included in criteria must also be a field in domain; otherwise the DCount function returns a Null.


Remarks
Use the DCount function to count the number of records in a domain when you don't need to know their particular values. Although the expr argument can perform a calculation on a field, the DCount function simply tallies the number of records. The value of any calculation performed by expr is unavailable.

Use the DCount function in a calculated control when you need to specify criteria to restrict the range of data on which the function is performed. For example, to display the number of orders to be shipped to California, set the ControlSource property of a text box to the following expression:

=DCount("[OrderID]", "Orders", "[ShipRegion] = 'CA'")

If you simply want to count all records in domain without specifying any restrictions, use the Count function.


Tip

The Count function has been optimized to speed counting of records in queries. Use the Count function in a query expression instead of the DCount function, and set optional criteria to enforce any restrictions on the results. Use the DCount function when you must count records in a domain from within a code module or macro, or in a calculated control.

You can use the DCount function to count the number of records containing a particular field that isn't in the record source on which your form or report is based. For example, you could display the number of orders in the Orders table in a calculated control on a form based on the Products table.

The DCount function doesn't count records that contain Null values in the field referenced by expr unless expr is the asterisk (*) wildcard character. If you use an asterisk, the DCount function calculates the total number of records, including those that contain Null fields. The following example calculates the number of records in an Orders table.

intX = DCount("*", "Orders")

If domain is a table with a primary key, you can also count the total number of records by setting expr to the primary key field, since there will never be a Null in the primary key field.

If expr identifies multiple fields, separate the field names with a concatenation operator, either an ampersand (&) or the addition operator (+). If you use an ampersand to separate the fields, the DCount function returns the number of records containing data in any of the listed fields. If you use the addition operator, the DCount function returns only the number of records containing data in all of the listed fields. The following example demonstrates the effects of each operator when used with a field that contains data in all records (ShipName) and a field that contains no data (ShipRegion).

intW = DCount("[ShipName]", "Orders")
intX = DCount("[ShipRegion]", "Orders")
intY = DCount("[ShipName] + [ShipRegion]", "Orders")
intZ = DCount("[ShipName] & [ShipRegion]", "Orders")

Note The ampersand is the preferred operator for performing string concatenation. You should avoid using the addition operator for anything other than numeric addition, unless you specifically wish to propagate Nulls through an expression.

Unsaved changes to records in domain aren't included when you use this function. If you want the DCount function to be based on the changed values, you must first save the changes by clicking Save Record on the Records menu, moving the focus to another record, or by using the Update method.

Example
The following function returns the number of orders shipped to a specified country after a specified ship date. The domain is an Orders table.

PHP:
Public Function OrdersCount(ByVal strCountry As String, _
                            ByVal dteShipDate As Date) As Integer

    OrdersCount = DCount("[ShippedDate]", "Orders", _
                  "[ShipCountry] = '" & strCountry & _
                  "' AND [ShippedDate] > #" & dteShipDate & "#")
End Function
To call the function, use the following line of code in the Immediate window:

:OrdersCount "UK", #1/1/96#
 
Phù phù, hix dịch cái này xong, rồi bắt tay mò mẫm chắc chít quá Bác SA ơi. Cám ơn Bác, em sẽ bắt tay vào "mò"--=0 rồi sẽ hỏi tiếp ạ.
 
Mình đưa 1 ví dụ đây

Vô ngăn form; Xem hàm DCOUNT ở chế độ thiết kế
 

File đính kèm

  • DCount.rar
    9.5 KB · Đọc: 1,032
Được voi đòi tiên, nhờ bác SA giúp em luôn ạ.
Vậy thì khi kết nối giữa EX & Acess thì để đếm số record trong Access Database rồi trả về Ex thì phải cần câu lệnh SQL như thế nào được ạ. em mò cả đêm mà chưa được. Nói chung là SQL em không rành lắm.
Cám ơn Bác đã chỉ Dcount trong Access.
 
Được voi đòi tiên, nhờ bác SA giúp em luôn ạ.
Vậy thì khi kết nối giữa EX & Acess thì để đếm số record trong Access Database rồi trả về Ex thì phải cần câu lệnh SQL như thế nào được ạ. em mò cả đêm mà chưa được. Nói chung là SQL em không rành lắm.
Cám ơn Bác đã chỉ Dcount trong Access.

Bạn dùng câu lệnh ở bài #2
 
Muốn đến số record thì bạn dùng SQL (trong access là select query), cú pháp như sau:


Select count(Tên cột/Mã quy ước) from tbBangDuLieu where (điều kiện gì đó)
Câu lệnh của Bác là chỉ Count cho cái đoạn đo đỏ thôi phải không Bác, tức là bao gồm cả tên Field và Mã Quy Ước mình muốn count, em đã test nhưng không được. bác có thể chỉ rõ được không ạ.
Cám ơn Bác ca_dafi
 
Lần chỉnh sửa cuối:
Chỉnh sửa câu lệnh SQL trong excel

Câu lệnh của Bác là chỉ Count cho cái đoạn đo đỏ thôi phải không Bác, tức là bao gồm cả tên Field và Mã Quy Ước mình muốn count, em đã test nhưng không được. bác có thể chỉ rõ được không ạ.
Cám ơn Bác ca_dafi


Bạn xem file đính kèm mính gửi. Bạn unzip folder này ra ổ đĩa D nhé.
Trước tiên bạn import data từ access vào một sheet bất kỳ.
Click chuột phải lên vùng data vừa query ra chọn edit query. Bạn sửa phần command text thành
PHP:
Select count(tên cột gì đó/Mã quy ước gì đó) from Table where [đk gì đó]
 

File đính kèm

  • Vidu1.rar
    408.1 KB · Đọc: 625
Anh ơi, cho em hỏi với cái hàm này của anh chỉ:

DCount(expr, domain, [criteria])

PHP:
Public Function OrdersCount(ByVal strCountry As String, _
                            ByVal dteShipDate As Date) As Integer

    OrdersCount = DCount("[ShippedDate]", "Orders", _
                  "[ShipCountry] = '" & strCountry & _
                  "' AND [ShippedDate] > #" & dteShipDate & "#")
End Function
Em muốn đếm trường manv trong bảng canbo thì em sẽ tạo hàm OrdersCount như sau :OrdersCount = DCount("[manv]", "canbo")
và sau đó trong form em gọi hàm này và gán cho một biến t. làm như thế có đúng ko anh? sao em không làm được ạ? Anh xem giúp em đúng không, nếu ko thì cách sửa thế nào anh nhé?
 

File đính kèm

  • test.7z
    10 KB · Đọc: 110
Nếu là TextBox thì gán ControlSource: =DCount("[manv]","canbo","manv like '*'")
(Đặt con trỏ vào TextBox rồi gõ vào cũng được)
Bạn lưu ý trong hàm các đoạn mình tô đỏ là 1 chuỗi nên phải đặt trong dấu ngoạc kép. Có thể kết hợp chuỗi và biến nhưng kết quả phải là 1 chuỗi.
Nôm na giải nghĩa:
-Đếm cái gì: "[manv]"
-Đếm ở đâu:
"canbo"
-Điều kiện thế nào:
"manv like '*'"

Còn đây là Code cho cái nút trên Form của bạn:

Mã:
Private Sub Toggle2_Click()
MsgBox IIf(DCount("[manv]", "canbo", "manv like '*'") > 0, _
"Tong so Nhan vien Cty la: " & DCount("[manv]", "canbo", "manv like '*'"), _
"Hien Cong ty khong co nhan vien nao !!!")
End Sub



P/s: Thấy bài mình lạc hậu lại mừng. Tải câu hỏi về đang lọ mọ thì có mấy ông khách ngân hàng tới tặng hoa SN. Giờ quay lại đã thấy bài tới tận đâu rồi. Thật mừng thấy anh em quan tâm nhau như vậy. Thế nên, bài mình không Update có gì mà không vui?
 
Lần chỉnh sửa cuối:
Web KT
Back
Top Bottom