ebook img

Microsoft Visual Basic for Applications Language Reference PDF

1081 Pages·1.857 MB·
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Microsoft Visual Basic for Applications Language Reference

Microsoft Visual Basic for Applications Language Reference Page 1 of 1081 Microsoft Visual Basic for Applications Language Reference – Operator Description Used to find the difference between two numbers or to indicate the negative value of a numeric expression. Syntax 1 result = number1–number2 Syntax 2 –number The – operator syntax has these parts Part Description result Required; any numeric variable. number Required; any numeric expression. number1 Required; any numeric expression. number2 Required; any numeric expression. Remarks In Syntax 1, the – operator is the arithmetic subtraction operator used to find the difference between two numbers. In Syntax 2, the – operator is used as the unary negation operator to indicate the negative value of an expression. The data type of result is usually the same as that of the most precise expression. The order of precision, from least to most precise, is Byte, Integer, Long, Single, Double, Currency, and Decimal. The following are exceptions to this order file://C:\temporary\~hhE561.htm 3/20/2000 Microsoft Visual Basic for Applications Language Reference Page 2 of 1081 If Then result is Subtraction involves a converted to a Double. Single and a Long, The data type of result is a converted to a Variant Long, Single, or Date containing a Double. variant that overflows its legal range, The data type of result is a converted to an Integer Byte variant that overflows variant. its legal range, The data type of result is converted to a Long an Integer variant that variant. overflows its legal range, Subtraction involves a Date a Date. and any other data type, Subtraction involves two a Double. Date expressions, One or both expressions are Null expressions, result is Null. If an expression is Empty, it is treated as 0. Note The order of precision used by addition and subtraction is not the same as the order of precision used by multiplication. See Also Operator precedence. Example This example uses the – operator to calculate the difference between two numbers. Dim MyResult MyResult = 4 - 2 ' Returns 2. MyResult = 459.35 - 334.90 ' Returns 124.45. #Const Directive file://C:\temporary\~hhE561.htm 3/20/2000 Microsoft Visual Basic for Applications Language Reference Page 3 of 1081 Description Used to define conditional compiler constants for Visual Basic. Syntax #Const constname = expression The #Const compiler directive syntax has these parts Part Description constname Required; Variant (String). Name of the constant; follows standard variable naming conventions. expression Required. Literal, other conditional compiler constant, or any combination that includes any or all arithmetic or logical operators except Is. Remarks Conditional compiler constants are always Private to the module in which they appear. It is not possible to create Public compiler constants using the #Const directive. Public compiler constants can only be created in the user interface. Only conditional compiler constants and literals can be used in expression. Using a standard constant defined with Const, or using a constant that is undefined, causes an error to occur. Conversely, constants defined using the #Const keyword can only be used for conditional compilation. Conditional compiler constants are always evaluated at the module level, regardless of their placement in code. See Also #If...Then...#Else directive, Const statement. Specifics (Microsoft Access) In Microsoft Access, you can define a public conditional compiler constant in the Declarations section of a module. A public compiler constant is applicable to all modules in the current database, but not in any other database. You can also declare a public compiler constant by clicking Options on the Tools menu, then clicking the Advanced tab. Enter the constant in the Conditional Compilation Arguments box. file://C:\temporary\~hhE561.htm 3/20/2000 Microsoft Visual Basic for Applications Language Reference Page 4 of 1081 For example, you might enter the following expression into the Conditional Compilation Arguments box. conDebug = 1 You can use a public compiler constant in code in any module. For example, you can create an #If...Then...#Else construct to optionally run debug code. #If conDebug = 1 Then . ' Run debug code. . . #Else . ' Run streamlined code. . . #End If To create multiple public conditional compilation constants, declare them on separate lines in the Declaration section. In the Conditional Compilation Arguments box, separate them with colons. For example, you can enter the following conditional compilation constants in the Conditional Compilation Arguments box. conActiveLanguage = 1 : conDebug = 1 You can use a logical operator to include both constants in an #If...Then...#Else construct. In the following example, the first segment of code runs only if both constants are currently equal to 1, so that the expression containing the constants is true. #If (conActiveLanguage = 1 And conDebug = 1) Then . ' Run debug code for the active language version. . . #Else . ' Run another code segment. . . #End If If either or both constants have a different value, then the code in the #Else portion of the construct runs. To change which block of code runs, you can simply change the values of the constants. Notes (cid:1) A conditional compilation constant is always evaluated with a text-based string comparison method. This evaluation is equivalent to having an Option Compare Text statement in the module and occurs even if the module contains an Option Compare Database statement. (cid:1) When writing code for conditional compilation, it may be less confusing to view one procedure at a time rather than all procedures in the module. To change how you view your code, click Options on the Tools menu, then click the Module tab. Under Code View, clear the Full Module View check box. Example This example uses the #Const directive to declare conditional compiler constants for use in #If...#Else...#End If constructs. file://C:\temporary\~hhE561.htm 3/20/2000 Microsoft Visual Basic for Applications Language Reference Page 5 of 1081 #Const DebugVersion = 1 ' Will evaluate true in #If block. #If...Then...#Else Directive Description Conditionally compiles selected blocks of Visual Basic code. Syntax #If expression Then statements [#ElseIf expression-n Then [elseifstatements]] [#Else [elsestatements]] #End If The #If...Then...#Else directive syntax has these parts Part Description expression Required. Any expression, consisting exclusively of one or more conditional compiler constants, literals, and operators, that evaluates to True or False. statements Required. Visual Basic program lines or compiler directives that are evaluated if the associated expression is True. expression-n Optional. Any expression, consisting exclusively of one or more conditional compiler constants, literals, and operators, that evaluates to True or False. file://C:\temporary\~hhE561.htm 3/20/2000 Microsoft Visual Basic for Applications Language Reference Page 6 of 1081 elseifstatements Optional. One or more program lines or compiler directives that are evaluated if expression-n is True. elsestatements Optional. One or more program lines or compiler directives that are evaluated if no previous expression or expression-n is True. Remarks The behavior of the #If...Then...#Else directive is the same as the If...Then...Else statement, except that there is no single-line form of the #If, #Else, #ElseIf, and #End If directives; that is, no other code can appear on the same line as any of the directives. Conditional compilation is typically used to compile the same program for different platforms. It is also used to prevent debugging code from appearing in an executable file. Code excluded during conditional compilation is completely omitted from the final executable file, so it has no size or performance effect. Regardless of the outcome of any evaluation, all expressions are evaluated. Therefore, all constants used in expressions must be defined — any undefined constant evaluates as Empty. Note The Option Compare statement does not affect expressions in #If and #ElseIf statements. Expressions in a conditional-compiler directive are always evaluated with Option Compare Text. See Also #Const directive, If...Then...Else statement. Example This example references conditional compiler constants in an #If...Then...#Else construct to determine whether to compile certain statements. ' If Mac evaluates as true, do the statements following the #If. #If Mac Then '. Place exclusively Mac statements here. '. '. ' Otherwise, if it is a 32-bit Windows program, do this: #ElseIf Win32 Then '. Place exclusively 32-bit Windows statements here. '. '. ' Otherwise, if it is neither, do this: #Else '. Place other platform statements here. '. '. #End If & Operator Description file://C:\temporary\~hhE561.htm 3/20/2000 Microsoft Visual Basic for Applications Language Reference Page 7 of 1081 Used to force string concatenation of two expressions. Syntax result = expression1 & expression2 The & operator syntax has these parts Part Description result Required; any String or Variant variable. expression1 Required; any expression. expression2 Required; any expression. Remarks If an expression is not a string, it is converted to a String variant. The data type of result is String if both expressions are string expressions; otherwise, result is a String variant. If both expressions are Null, result is Null. However, if only one expression is Null, that expression is treated as a zero-length string (" ") when concatenated with the other expression. Any expression that is Empty is also treated as a zero-length string. See Also Operator precedence. Example This example uses the & operator to force string concatenation. Dim MyStr MyStr = "Hello" & " World" ' Returns "Hello World". MyStr = "Check " & 123 & " Check" ' Returns "Check 123 Check". * Operator Description Used to multiply two numbers. Syntax result = number1*number2 file://C:\temporary\~hhE561.htm 3/20/2000 Microsoft Visual Basic for Applications Language Reference Page 8 of 1081 The * operator syntax has these parts Part Description result Required; any numeric variable. number1 Required; any numeric expression. number2 Required; any numeric expression. Remarks The data type of result is usually the same as that of the most precise expression. The order of precision, from least to most precise, is Byte, Integer, Long, Single, Currency, Double, and Decimal. The following are exceptions to this order If Then result is Multiplication involves a converted to a Double. Single and a Long, The data type of result is converted to a Variant a Long, Single, or Date containing a Double. variant that overflows its legal range, The data type of result is converted to an Integer a Byte variant that variant. overflows its legal range, the data type of result is converted to a Long an Integer variant that variant. overflows its legal range, If one or both expressions are Null expressions, result is Null. If an expression is Empty, it is treated as 0. Note The order of precision used by multiplication is not the same as the order of precision used by addition and subtraction. See Also file://C:\temporary\~hhE561.htm 3/20/2000 Microsoft Visual Basic for Applications Language Reference Page 9 of 1081 Operator precedence. Example This example uses the * operator to multiply two numbers. Dim MyValue MyValue = 2 * 2 ' Returns 4. MyValue = 459.35 * 334.90 ' Returns 153836.315. / Operator Description Used to divide two numbers and return a floating-point result. Syntax result = number1/number2 The / operator syntax has these parts Part Description result Required; any numeric variable. number1 Required; any numeric expression. number2 Required; any numeric expression. Remarks The data type of result is usually a Double or a Double variant. The following are exceptions to this rule If Then result is Both expressions are Byte, a Single unless it Integer, or Single overflows its legal range; expressions, in which case, an error occurs. file://C:\temporary\~hhE561.htm 3/20/2000 Microsoft Visual Basic for Applications Language Reference Page 10 of 1081 Both expressions are Byte, a Single variant unless it Integer, or Single overflows its legal range; variants, in which case, result is a Variant containing a Double. Division involves a a Decimal data type. Decimal and any other data type, One or both expressions are Null expressions, result is Null. Any expression that is Empty is treated as 0. See Also Operator precedence. Example This example uses the / operator to perform floating-point division. Dim MyValue MyValue = 10 / 4 ' Returns 2.5. MyValue = 10 / 3 ' Returns 3.333333. \ Operator Description Used to divide two numbers and return an integer result. Syntax result = number1\number2 The \ operator syntax has these parts Part Description result Required; any numeric variable. number1 Required; any numeric expression. file://C:\temporary\~hhE561.htm 3/20/2000

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.