Declarations
دستور نوشتاری فرمان(Syntax) به نحو زیر است:
Function Procedures
Declares the name, arguments, and code that form the body of a Function procedure.
Syntax
[Public | Private | Friend] [Static] Function name [(arglist)] [As type]
[statements]
[name = expression]
[Exit Function]
[statements]
[name = expression]
End Function
The Function statement syntax has these parts:
Part Description
Public Optional. Indicates that the Function procedure is accessible to all other procedures in all
modules. If used in a module that contains an Option Private, the procedure is not
available outside the project.
Private Optional. Indicates that the Function procedure is accessible only to other procedures in
the module where it is declared.
Friend Optional. Used only in a class module. Indicates that the Function procedure is visible
throughout the project, but not visible to a controller of an instance of an object.
Static Optional. Indicates that the Function procedure's local variables are preserved between
calls. The Static attribute doesn't affect variables that are declared outside the Function,
even if they are used in the procedure.
name Required. Name of the Function; follows standard variable naming conventions.
arglist Optional. List of variables representing arguments that are passed to the Function
procedure when it is called. Multiple variables are separated by commas.
type Optional. Data type of the value returned by the Function procedure; may be Byte,
Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported),
Date, String, or (except fixed length), Object, Variant, or any user-defined type.
statements Optional. Any group of statements to be executed within the Function procedure.
expression Optional. Return value of the Function.
The arglist argument has the following syntax and parts:
[Optional] [ByVal | ByRef] [ParamArray] varname[( )] [As type] [= defaultvalue]
Part Description
Optional Optional. Indicates that an argument is not required. If used, all subsequent arguments in
arglist must also be optional and declared using the Optional keyword. Optional can't be
used for any argument if ParamArray is used.
ByVal Optional. Indicates that the argument is passed by value.
ByRef Optional. Indicates that the argument is passed by reference. ByRef is the default in
Visual Basic.
ParamArray Optional. Used only as the last argument in arglist to indicate that the final argument is
an Optional array of Variant elements. The ParamArray keyword allows you to
provide an arbitrary number of arguments. It may not be used with ByVal, ByRef, or
Optional.
varname Required. Name of the variable representing the argument; follows standard variable
naming conventions.
type Optional. Data type of the argument passed to the procedure; may be Byte, Boolean,
Integer, Long, Currency, Single, Double, Decimal (not currently supported) Date,
String (variable length only), Object, Variant, or a specific object type. If the parameter
is not Optional, a user-defined type may also be specified.
defaultvalue Optional. Any constant or constant expression. Valid for Optional parameters only. If the
type is an Object, an explicit default value can only be Nothing.
Remarks
If not explicitly specified using Public, Private, or Friend, Function procedures are public by default. If Static
isn't used, the value of local variables is not preserved between calls. The Friend keyword can only be used in
class modules. However, Friend procedures can be accessed by procedures in any module of a project. A
Friend procedure does't appear in the type library of its parent class, nor can a Friend procedure be late bound.
دستور نوشتاری فرمان(Syntax) به نحو زیر است:
Const
Declares constants for use in place of literal values.
Syntax
[Public | Private] Const constname [As type] = expression
The Const statement syntax has these parts:
Part Description
Public Optional. Keyword used at module level to declare constants that are available to all
procedures in all modules. Not allowed in procedures.
Private Optional. Keyword used at module level to declare constants that are available only within
the module where the declaration is made. Not allowed in procedures.
constname Required. Name of the constant; follows standard variable naming conventions.
type Optional. Data type of the constant; may be Byte, Boolean, Integer, Long, Currency,
Single, Double, Decimal (not currently supported), Date, String, or Variant. Use a separate
As type clause for each constant being declared.
expression Required. Literal, other constant, or any combination that includes all arithmetic or logical
operators except Is.
Remarks
Constants are private by default. Within procedures, constants are always private; their visibility can't be
changed. In standard modules, the default visibility of module-level constants can be changed using the Public
keyword. In class modules, however, constants can only be private and their visibility can't be changed using
the Public keyword.
Example:
' Constants are Private by default.
Const MyVar = 459
' Declare Public constant.
Public Const MyString = "HELP"
' Declare Private Integer constant.
Private Const MyInt As Integer = 5
' Declare multiple constants on same line.
Const MyStr = "Hello", MyDouble As Double = 3.4567
دستور نوشتاری فرمان(Syntax) به نحو زیر است:
Call Statement
Transfers control to a Sub procedure, Function procedure, or dynamic-link library (DLL) procedure.
Syntax
[Call] name [argumentlist]
The Call statement syntax has these parts:
Part Description
Call Optional; keyword. If specified, you must enclose argumentlist in parentheses. For
example:
Call MyProc(0)
name Required. Name of the procedure to call.
argumentlist Optional. Comma-delimited list of variables, arrays, or expressions to pass to the
procedure. Components of argumentlist may include the keywords ByVal or ByRef to
describe how the arguments are treated by the called procedure. However, ByVal and
ByRef can be used with Call only when calling a DLL procedure. On the Macintosh,
ByVal and ByRef can be used with Call when making a call to a Macintosh code
resource.
Remarks
You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword
to call a procedure that requires arguments, argumentlist must be enclosed in parentheses.
Example:
Call an intrinsic function. The return value of the function is
' discarded.
Call Shell(AppName, 1) ' AppName contains the path of the
' executable file.
CallByName
Executes a method of an object, or sets or returns a property of an object.
Syntax
CallByName(object, procname, calltype,[args()])
The CallByName function syntax has these named arguments:
Part Description
object Required; Variant (Object). The name of the object on which the function will be
executed.
procname Required; Variant (String). A string expression containing the name of a property or
method of the object.
calltype Required; Constant. A constant of type vbCallType representing the type of
procedure being called.
args() Optional: Variant (Array).
Remarks
The CallByName function is used to get or set a property, or invoke a method at run time using a string name.
Example:
دستور نوشتاری فرمان(Syntax) به نحو زیر است:
CallByName Text1, "MousePointer", vbLet, vbCrosshair
Result = CallByName (Text1, "MousePointer", vbGet)
CallByName Text1, "Move", vbMethod, 100, 100
Option Explicit
Used at module level to force explicit declaration of all variables in that module.
Syntax
دستور نوشتاری فرمان(Syntax) به نحو زیر است:
Option Explicit
Remarks
If used, the Option Explicit statement must appear in a module before any procedures.
When Option Explicit appears in a module, you must explicitly declare all variables using the Dim, Private,
Public, ReDim, or Static statements. If you attempt to use an undeclared variable name, an error occurs at
compile time.
If you don't use the Option Explicit statement, all undeclared variables are of Variant type unless the default
type is otherwise specified with a Deftype statement.
Example:
Option explicit ' Force explicit variable declaration.
Dim MyVar ' Declare variable.
MyInt = 10 ' Undeclared variable generates error.
MyVar = 10 ' Declared variable does not generate error.
دستور نوشتاری فرمان(Syntax) به نحو زیر است:
Option Private
When used in host applications that allow references across multiple projects, Option Private Module prevents
a module’s contents from being referenced outside its project. In host applications that don’t permit such
references, for example, standalone versions of Visual Basic, Option Private has no effect.
Syntax
Option Private Module
Remarks
If used, the Option Private statement must appear at module level, before any procedures.
When a module contains Option Private Module, the public parts, for example, variables, objects, and userdefined types declared at module level, are still available within the project containing the module, but they are
not available to other applications or projects.
Example:
Option private Module ' Indicates that module is private.
دستور نوشتاری فرمان(Syntax) به نحو زیر است:
Option Compare
Used at module level to declare the default comparison method to use when string data is compared.
Syntax
Option Compare {Binary | Text | Database}
Remarks
If used, the Option Compare statement must appear in a module before any procedures.
The Option Compare statement specifies the string comparison method (Binary, Text, or Database) for a
module. If a module doesn't include an Option Compare statement, the default text comparison method is
Binary.
Option Compare Binary results in string comparisons based on a sort order derived from the internal binary
representations of the characters. In Microsoft Windows, sort order is determined by the code page. A typical
binary sort order is shown in the following example:
A < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø
Option Compare Text results in string comparisons based on a case-insensitive text sort order determined by
your system's locale. When the same characters are sorted using Option Compare Text, the following text sort
order is produced:
(A=a) < ( À=à) < (B=b) < (E=e) < (Ê=ê) < (Z=z) < (Ø=ø)
Example:
Set the string comparison method to Binary.
Aaron Wirth
35
Option compare Binary ' That is, "AAA" is less than "aaa".
' Set the string comparison method to Text.
Option compare Text ' That is, "AAA" is equal to "aaa".
دستور نوشتاری فرمان(Syntax) به نحو زیر است:
Type…End Type
Used at module level to define a user-defined data thype containing one or more elements.
Syntax
[Private | Public] Type varname
elementname [([subscripts])] As type
[elementname [([subscripts])] As type]
. . .
End Type
The Type statement syntax has these parts:
Part Description
Public Optional. Used to declare user-defined htypes that are available to all procedures in all
modules in all projects.
Private Optional. Used to declare user-defined types that are available only within the module
where the declaration is made.
varname Required. Name of the user-defined type; follows standard variable naming
conventions.
elementname Required. Name of an element of the user-defined type. Element names also follow
standard variable naming conventions, except that keywords can be used.
subscripts When not explicitly stated in lower, the lower bound of an array is controlled by the
Option Base statement. The lower bound is zero if no Option Base statement is
present.
type Required. Data type of the element; may be Byte, Boolean, Integer, Long, Currency,
Single, Double, Decimal (not currently supported), Date, String (for variable-length
strings), String * length (for fixed-length strings), Object, Variant, another userdefined type, or an object type.
Remarks
The Type statement can be used only at module level. Once you have declared a user-defined type using the
Type statement, you can declare a variable of that type anywhere within the scope of the declaration. Use Dim,
Private, Public, ReDim, or Static to declare a variable of a user-defined type.
Example:
Type StateData
CityCode (1 To 100) As Integer ' Declare a static array.
County As String * 30
End Type
Dim Washington(1 To 100) As StateData
دستور نوشتاری فرمان(Syntax) به نحو زیر است:
GetObject
Returns a reference to an object provided by an ActiveX component.
Syntax
GetObject([pathname] [, class])
The GetObject function syntax has these named arguments:
Part Description
pathname Optional; Variant (String). The full path and name of the file containing the object to
retrieve. If pathname is omitted, class is required.
class Optional; Variant (String). A string representing the class of the object.
The class argument uses the syntax appname.objecttype and has these parts:
Part Description
appname Required; Variant (String). The name of the application providing the object.
objecttype Required; Variant (String). The type or class of object to create.
Remarks
Use the GetObject function to access an ActiveX object from a file and assign the object to an object variable.
Use the Set statement to assign the object returned by GetObject to the object variable.
Example:
Dim CADObject As Object
Set CADObject = GetObject("C:\CAD\SCHEMA.CAD")
دستور نوشتاری فرمان(Syntax) به نحو زیر است:
CreateObject
Creates and returns a reference to an ActiveX object.
Syntax
CreateObject(class,[servername])
The CreateObject function syntax has these parts:
Part Description
class Required; Variant (String). The application name and class of the object to create.
servername Optional; Variant (String). The name of the network server where the object will be
created. If servername is an empty string (""), the local machine is used.
The class argument uses the syntax appname.objecttype and has these parts:
Part Description
appname Required; Variant (String). The name of the application providing the object.
objecttype Required; Variant (String). The type or class of object to create.
Remarks
Every application that supports Automation provides at least one type of object. For example, a word processing
application may provide an Application object, a Document object, and a Toolbar object.
Example:
Dim ExcelSheet As Object
Set ExcelSheet = CreateObject("Excel.Sheet")
دستور نوشتاری فرمان(Syntax) به نحو زیر است:
Let Statement
Assigns the value of an expression to a variable or property.
Syntax
[Let] varname = expression
The Let statement syntax has these parts:
Part Description
Let Optional. Explicit use of the Let keyword is a matter of style, but it is usually omitted.
varname Required. Name of the variable or property; follows standard variable naming conventions.
expression Required. Value assigned to the variable or property.
Remarks
A value expression can be assigned to a variable or property only if it is of a data type that is compatible with
the variable. You can't assign string expressions to numeric variables, and you can't assign numeric expressions
to string variables. If you do, an error occurs at compile time.
Variant variables can be assigned either string or numeric expressions. However, the reverse is not always true.
Any Variant except a Null can be assigned to a string variable, but only a Variant whose value can be
interpreted as a number can be assigned to a numeric variable. Use the IsNumeric function to determine if the
Variant can be converted to a number.
Example:
Dim MyStr, MyInt
' The following variable assignments use the Let statement.
Let MyStr = "Hello World"
Let MyInt = 5
دستور نوشتاری فرمان(Syntax) به نحو زیر است:
TypeName
Returns a String that provides information about a variable.
Syntax
TypeName(varname)
The required varname argument is a Variant containing any variable except a variable of a user-defined type.
Remarks
The string returned by TypeName can be any one of the following:
String returned Variable
object thype An object whose type is objecttype
Byte Byte value
Integer Integer
Long Long integer
Single Single-precision floating-point number
Double Double-precision floating-point number
Currency Currency value
Decimal Decimal value
Date Date value
String String
Boolean Boolean value
Error An error value
Empty Uninitialized
Null No valid data
Object An object
Unknown An object whose type is unknown
Nothing Object variable that doesn't refer to an object
If varname is an array, the returned string can be any one of the possible returned strings (or Variant) with
empty parentheses appended. For example, if varname is an array of integers, TypeName returns
"Integer()".
Example:
Dim MyType
MyType = TypeName(StrVar) ' Returns "String".
MyType = TypeName(IntVar) ' Returns "Integer".
MyType = TypeName(CurVar) ' Returns "Currency".
MyType = TypeName(NullVar) ' Returns "Null".
MyType = TypeName(ArrayVar) ' Returns "Integer()".
دستور نوشتاری فرمان(Syntax) به نحو زیر است:
VarType
Returns an Integer indicating the subtype of a variable.
Syntax
VarType(varname)
The required varname argument is a Variant containing any variable except a variable of a user-defined type.
Return Values
Constant Value Description
vbEmpty 0 Empty (uninitialized)
vbNull 1 Null (no valid data)
vbInteger 2 Integer
vbLong 3 Long integer
vbSingle 4 Single-precision floating-point number
vbDouble 5 Double-precision floating-point number
vbCurrency 6 Currency value
vbDate 7 Date value
vbString 8 String
vbObject 9 Object
vbError 10 Error value
vbBoolean 11 Boolean value
vbVariant 12 Variant (used only with arrays of variants)
vbDataObject 13 A data access object
vbDecimal 14 Decimal value
vbByte 17 Byte value
vbUserDefinedType 36 Variants that contain user-defined types
vbArray 8192 Array
Note These constants are specified by Visual Basic for Applications. The names can be used anywhere in your
code in place of the actual values.
Remarks
The VarType function never returns the value for vbArray by itself. It is always added to some other value to
indicate an array of a particular type. The constant vbVariant is only returned in conjunction with vbArray to
indicate that the argument to the VarType function is an array of type Variant. For example, the value returned
for an array of integers is calculated as vbInteger + vbArray, or 8194. If an object has a default property,
VarType (object) returns the type of the object's default property.
Example:
Dim IntVar, StrVar, DateVar, MyCheck
' Initialize variables.
IntVar = 459: StrVar = "Hello World": DateVar = #2/12/69#
MyCheck = VarType(IntVar) ' Returns 2.
MyCheck = VarType(DateVar) ' Returns 7.
MyCheck = VarType(StrVar) ' Returns 8.
دستور نوشتاری فرمان(Syntax) به نحو زیر است:
DefType
Used at module level to set the default data type for variables, arguments passed to procedures, and the return
type for Function and Property Get procedures whose names start with the specified characters.
Syntax
DefBool letterrange[, letterrange] . . .
DefByte letterrange[, letterrange] . . .
DefInt letterrange[, letterrange] . . .
DefLng letterrange[, letterrange] . . .
DefCur letterrange[, letterrange] . . .
DefSng letterrange[, letterrange] . . .
DefDbl letterrange[, letterrange] . . .
DefDec letterrange[, letterrange] . . .
DefDate letterrange[, letterrange] . . .
DefStr letterrange[, letterrange] . . .
DefObj letterrange[, letterrange] . . .
DefVar letterrange[, letterrange] . . .
The required letterrange argument has the following syntax:
letter1[-letter2]
The letter1 and letter2 arguments specify the name range for which you can set a default data type. Each
argument represents the first letter of the variable, argument, Function procedure, or Property Get procedure
name and can be any letter of the alphabet. The case of letters in letterrange isn't significant.
Remarks
The statement name determines the data type:
Statement Data Type
DefBool Boolean
DefByte Byte
DefInt Integer
DefLng Long
DefCur Currency
DefSng Single
DefDbl Double
DefDec Decimal (not currently supported)
DefDate Date
DefStr String
DefObj Object
DefVar Variant
A Deftype statement affects only the module where it is used. For example, a DefInt statement in one module
affects only the default data type of variables, arguments passed to procedures, and the return type for Function
and Property Get procedures declared in that module; the default data type of variables, arguments, and return
types in other modules is unaffected. If not explicitly declared with a Deftype statement, the default data type
for all variables, all arguments, all Function procedures, and all Property Get procedures is Variant.