You are on page 1of 12

HÀM TỔNG:

Access: Sum
Function
In Access, the Sum function returns the sum of a set of values in a set of values in a select
query.

The syntax for the Sum function is:

Sum ( expression )

The expression argument represents a string expression identifying the field that contains the
numeric data you want to add or an expression that performs a calculation using the data in
that field. Operands in expr can include the name of a table field, a constant, or a function
(not one of the other SQL aggregate functions). The Sum function totals the values in a field.
It ignores records that contain Null fields.

SQL query
In Access, you can use the Sum function in the query design grid, in an SQL statement in
SQL view of the Query window, or in an SQL statement within Visual Basic code. It is used in
conjunction with the Group By clause.

EXAMPLE:

Select SellerID, Sum(Price) as TotalSum


From Antiques
Group by SellerID
Having Sum(Price)>57

HÀM ASC:

Access: Asc
Function
In Access, the Asc function returns an integer representing the ANSI code corresponding to
the first character in a string.

The syntax for the Asc function is:

Asc ( string )

The required string argument is any valid string expression. If the string contains no
characters, a run-time error occurs. If more than one character is entered, the function will
return the value for the first character and ignore all of the characters after the first.

Example
Asc ( "a" ) returns 97

Asc ( "A" ) returns 65

Asc ( "apple" ) returns 97


VBA Code
Dim MyNumber
MyNumber = Asc("A")
This example returns 65, the ANSI character code for an uppercase
letter A. Now the MyNumber variable would contain the value 65.

SQL query
You can also use the Asc function in a query.

SELECT Asc('A') AS Expr1

FROM Orders

HÀM ASB:

Access: Abs
Function
In Access, the Abs funtion returns the absolute value of a number.

The syntax for the Abs function is:

Abs( number )

The argument number can be any valid numeric expression. If number contains Null, Null is
returned. The absolute value of a number is its unsigned magnitude. For example, ABS(-1)
and ABS(1) both return 1.

Example
Abs (-1.45) returns 1,45

Abs (2.17) returns 2,17


VBA Code
Dim MyNumber
MyNumber = Abs(42.7)
MyNumber = Abs(-42.7)
This example uses the Abs function to compute the absolute value of
a number. Now the MyNumber variable would contain the value 42.7.

SQL query
You can also use the Abs function in a query.

SELECT Abs([Salary]) AS Expr1

FROM EmployeeStatisticsTable

HÀM ATN:

In Access, the Atn function returns a double containing the arctangent of a number.

The syntax for the Atn function is:

Atn( number )

The required number argument can be any valid numeric expression. The Atn function takes
the ratio of two sides of a right triangle (number) and returns the corresponding angle in
radians. The ratio is the length of the side opposite the angle divided by the length of the
side adjacent to the angle.

Example
Atn (15) returns 1,50422816301907

Atn (21) returns 1,52321322351791


VBA Code
Dim MyNumber As Double

MyNumber = Atn(2)
This example uses the Atn function to calculate the arctangent of a
number. Now the MyNumber variable would contain the value 1,10714871779409.

SQL query
You can also use the Atn function in a query.

SELECT Atn([OwnerID]) AS Expr1

FROM Orders

HÀM AVG:

Access: Avg
function
In Access, the Avg function calculates the arithmetic mean of a set of values in a select
query.

The syntax for the Avg function is:

Avg ( expression )

The expression argument represents a string expression identifying the field that contains
the numeric data you want to average. Operands in expression can include the name of a
table field, a constant, or a function (not one of the other SQL aggregate functions). The
average calculated by Avg is the arithmetic mean (the sum of the values divided by the
number of values). You could use Avg, for example, to calculate average freight cost.The Avg
function doesn't include any Null fields in the calculation.

SQL query
In Access, you can use the Avg function in the query design grid, in an SQL statement in
SQL view of the Query window, or in an SQL statement within Visual Basic code. It is used in
conjunction with the Group By clause.

SELECT Position, Avg(Salary) AS AvgSalary


FROM EmployeeStatisticsTable
GROUP BY Position

HÀM

CHOOSE FUNCTION:

Access: Choose
Function
In Access, the Choose function selects and returns a value from a list of arguments based on
a given position.

The syntax for the Choose function is:

Choose(index, choice-1[,choice-2, ... [, choice-n]] )

You can use Choose to look up a value in a list of possibilities. If index is 1, Choose returns
the first choice in the list; if index is 2, it returns the second choice, and so on. The Choose
function returns a Null if index is less than 1 or greater than the number of choices listed.

index is position number in the list of values to return.

choice is variant expression containing one of the possible choices.

Example
Choose(1, "Have", "a", "nice", "day") returns "Have"

Choose(3, "Have", "a", "nice", "day") returns "nice"

Choose(4, "Have", "a", "nice", "day") returns "day"


Choose(7, "Have", "a", "nice", "day") returns NULL
Choose(2.87, "Have", "a", "nice", "day") returns "a"
VBA Code
Dim MyChoice As String
MyChoice = Choose(2, "Apple", "Orange", "Strawberry")
This example uses the Choose
function to display a fruits name in response to an index. Now the MyChoice variable would
contain the value "Orange".

SQL query
You can also use the Choose function in a query.

SELECT Choose([OwnerID],'Table','Desc','Chair','Mirrow') AS Expr1


FROM Orders

HÀM Chr:

Access: Chr
Function
In Access the Chr function returns the character associated with the specified ANSI code.

The syntax for the Chr function is:

Chr ( charcode )

The required charcode argument is the NUMBER used to retrieve the character. The
argument charcode is an integer between 0 and 255, inclusive. Applications for Microsoft
Windows use the ANSI character set. ANSI character codes in the range 0 to 31, inclusive,
are the same as the standard, nonprintable ASCII codes. For example, Chr(13) returns a
carriage-return character, and Chr(10) returns a linefeed character. Together they can be
used to force a new line when message strings are formatted with MsgBox or InputBox.

Example
Chr( 65 ) returns "A"

Chr( 97 ) returns "a"


VBA Code
Dim MyChar
MyChar = Chr(100)
This example returns "d", the character associated with the specified
character code. Now the MyChar variable would contain the value "d".

SQL query
You can also use the Chr function in a query.

SELECT Chr(37) AS Expr1, Chr(100) AS Expr2

FROM Orders

HÀM COS:

Access: Cos
Function
In Access, the Cos function returns a double containing the cosine of an angle.

The syntax for the Cos function is:

Cos ( number )

The argument number can be any valid numeric expression that expresses an angle in
radians. The Cos function takes an angle and returns the ratio of two sides of a right
triangle. The ratio is the length of the side adjacent to the angle divided by the length of the
hypotenuse.

Cos(15) returns -0,759687912858821

Cos (2) returns -0,416146836547142


VBA Code
Dim MyNumber
MyNumber = Cos(2.5)
This example uses the Cos function to return the cosine of an angle.
Now the MyNumber variable would contain the value -0.80708818.

SQL query
You can also use the Cos function in a query.

SELECT Cos([OwnerId]) AS Expr1

FROM Orders

HÀM COUNT:

Access: Count
Function
In Access, the Count function calculates the number of records in a select query.

The syntax for the Count function is:

Count ( expression )

The required expression argument represents a string expression identifying the field that
contains the data you want to count or an expression that performs a calculation using the
data in the field. Operands in expression can include the name of a table field or function
(not other SQL aggregate functions). You can count any kind of data, including text. The
Count function doesn't count records that have Null fields unless expression is the asterisk
(*) wildcard character. If you use an asterisk, Count calculates the total number of records,
including those that contain Null fields.

SQL query
In Access, you can use the Count function in the query design grid, in an SQL statement in
SQL view of the Query window, or in an SQL statement within Visual Basic code. It is used in
conjunction with the Group By clause.

SELECT SellerID, Count(Item) AS Quantity


FROM Antiques
GROUP BY SellerID

HÀM CurDir:

Access: CurDir
Function
In Access, the CurDir function returns a string containing the full path of the specified drive.

The syntax for the CurDir function is:

CurDir ( drive )

The optional drive argument is a string expression that specifies an existing drive. If this
parameter is omitted or it is a zero-length string (""), CurDir returns the path for the current
drive.

Example
CurDir () returns "C:\Documents and Settings\UserName\My Documents"

CurDir ( "H" ) returns "H:\"


VBA
Code
Dim MyPath As String
MyPath = CurDir ()
This example uses the CurDir function to return the current
path. Now the MyPath variable would contain the value "C:\Documents and
Settings\UserName\My Documents".

SQL query
You can also use the CurDir function in a query.

SELECT CurDir() AS Expr1


FROM Orders
Note
This function cannot be used or referenced in expressions when Access is operating in
sandbox mode, but they can be used in Microsoft Visual Basic for Applications (VBA) code. To
enable the CurDir function in expressions or SQL statements, add the following code to a
code module in the Visual Basic Editor in Access:

Public Function CurDir(Optional Drive As String)

CurDir = VBA.CurDir(Drive)
End Function

HÀM DATE:

In Access, the Date function returns the current system date.

The syntax for the Date function is:


Date ( )

Date returns a variant (date) containing a date stored internally as a Double.

Examples
Date() returns a value such as '05.05.2005'
VBA Code
Dim MyDate
MyDate = Date
Now the MyDate variable would contain the current system date.

SQL query
You can also use the Date function in a query.

SELECT Date() AS Expr1

FROM Orders

HÀM DATEADD:

In Access, the DateAdd function Adds a specified time interval to a date.

The syntax for the DateAdd function is:

DateAdd ( interval, number, date )

You can use the DateAdd function to add a specified time interval to or subtract a specified
time interval from a date. For example, you can use DateAdd to calculate a date 15 days
from today or a time that is 30 minutes from now.

interval is the interval of time you want to add to date. The following is a list of valid interval
values.
Interval Description

yyyy Year

q Quater

m Month

y Day of year

w Weekday

ww Week

h Hour

n Minute

s Second
number is the number of
intervals you want to add to date. It can be positive (to get dates in the future) or negative
(to get dates in the past).

date is the date to which the interval is added.

Examples
DateAdd ('m', 4, #01/22/2004# ) returns 05/22/2004

DateAdd ('yyyy',3, #11/10/2003# ) returns 11/10/2006


VBA Code
Dim MyNumber As Integer
MyNumber = DateAdd('d',45,#12/09/1998#)
This example takes a date and, using the DateAdd
function, displays a corresponding date a specified number of days in the future. Now the
MyNumber variable would contain the value of '01.23.1999'.

SQL query
You can also use the DateAdd function in a query.

SELECT DateAdd('d',45, Now()) AS Expr1

FROM Orders

You might also like