Anda di halaman 1dari 14

Using the Immediate Pane

While you are creating and testing Visual Basic code, you may want to run a
function or check the value of a control, field, or property. You can do this in the
Immediate pane of the Debug window. The Immediate pane is a kind of scratch pad
window in which expressions are evaluated immediately.

Although you can use the Immediate pane to run any Visual Basic command, you'll
most commonly use it to:

Test your Function and Sub procedures.

Display the values of controls and properties.

Evaluate expressions and display their results.

Display debugging information from within your code.

Testing Procedures Using the Immediate Pane

The Immediate pane of the Debug window evaluates a Visual Basic expression or
statement, such as a call to a Sub or Function procedure. You can evaluate an
expression or a Function procedure by displaying the value it returns in the
Immediate pane. You can test the possible effect of a Sub procedure with any given
set of arguments by entering it as a statement in the Immediate pane, just as you
would in the Module window.

You can display a value in the Immediate pane by using the Print method of the
Debug object. The Debug object represents the Immediate pane in Visual Basic.

For example, suppose you have a function named DueDate, which takes an argument of
type Date and returns the date of the first day of the month following the date that is
passed to it:

Public Function DueDate(ByVal AnyDate As Date) As Variant


' This function calculates and returns the date of first
' day of month that follows the supplied date.
DueDate = DateSerial(Year(AnyDate), Month(AnyDate) + 1, 1)
End Function

You can test the DueDate function by entering the following code in the Immediate pane:

Debug.Print DueDate(#4-12-96#)

Visual Basic runs the function and displays the result: 5/1/96.
Because the Debug object is the default object in the Immediate pane, you can use the
Print method in the Immediate pane without referring to the Debug object. For example,
you can test the DueDate function by entering the following statement in the Immediate
pane:

Print DueDate(#4-12-96#)

You can use a question mark (?) as shorthand for the Print method. The question mark
means precisely the same thing as Print and can be used in any context where Print is
used. For example, you can determine the day of the week for the date returned by the
DueDate function by entering the following code in the Immediate pane:

? Weekday(DueDate(#4-12-96#))

Visual Basic runs the function and displays the result: 4. This value indicates that May 1,
1996 is a Wednesday.

You can test a Sub procedure by entering it and its arguments in the Immediate pane. For
example, you can test a procedure named SizeIt by entering the following code in the
Immediate pane:

SizeIt 5000, 3000

You don't need to use the Print method because a Sub procedure doesn't return a value for
the Immediate pane to display.

Note In the Immediate pane, you can call procedures that are currently in scope
without qualifying the procedure name. For example, you can call any procedure in
the current module, and you can call any public procedure from any other module
in the database. However, to run a private procedure in another module, you must
qualify the name of the procedure with the name of the module that contains it. For
example, to call a private procedure named AddValues in the Utilities module when
the module is not open, you must type Utilities.AddValues in the Debug window.
For information on the scope of procedures and variables, see Chapter 2,
"Introducing Visual Basic" and Chapter 4, "Working with Variables, Data Types,
and Constants."

You can run any built-in function or statement in the Immediate pane. However, a control
structure is valid only if it can be completely expressed on one line. Because the For loop
in the following example is on one line, it can be run in the Immediate pane.

For intI = 1 To 20 : Print 2 * intI : Next intI

See Also For information on the Debug object or the Print method, search the Help
index for "Debug object" or "Print method."
Displaying Values of Controls and Properties

You can evaluate any valid expression in the Immediate pane, including expressions
involving the values and properties for Microsoft Access objects or Data Access Objects
(DAO). For example:

? Forms!SalesReps.RecordSource
Employees

In the preceding example, the first line is the expression to be evaluated. The second line
is what is displayed in the Immediate pane the value of the RecordSource property for
the open SalesReps form.

Displaying Values in the Immediate Pane from Code

While you're testing your code, you may want to display the results of expressions in
your code as it's running. You can use the Print method of the Debug object to display the
results in the Immediate pane. The syntax for the Print method of the Debug object is:

Debug.Print [outputlist]

In this syntax, you can use the optional outputlist argument to specify an expression or
list of expressions to print. For example, you can add the Print method of the Debug
object to the DueDate function as follows:

Public Function DueDate(ByVal AnyDate As Date) As Variant


' This function calculates and returns the date of first
' day of month that follows the supplied date.
Debug.Print "Year "; Year(AnyDate); "Month "; Month(AnyDate)
DueDate = DateSerial(Year(AnyDate), Month(AnyDate) + 1, 1)
End Function

Now whenever this function is called, it displays the the current year and month in the
Immediate pane, as well as the value returned by the DueDate function. For example, you
can call this function from the Immediate pane and provide the date 4-12-96 for the
AnyDate argument. If you're stepping through the code, as shown in the following
illustration, the function prints the value for the year (1996) and the value for the month
(4) to the Debug window once the line that contains the Debug.Print statement runs.
When you run the next line of code, the function evaluates the expression that determines
the date of the first day of the following month. This is the value returned by the DueDate
function. When the DueDate function has finished running, this value is also printed to
the Debug window. In this case, the return value of the function is 5-1-96.

Displaying values in the Immediate pane from code has a couple of advantages. First, you
don't have to suspend execution to get feedback on how your code is performing. You
can view data or other messages as you run your code. Second, the feedback is displayed
in a separate area (the Immediate pane), so it doesn't interfere with output you want users
to see.

Once you're sure your code is working correctly, you can remove Debug.Print statements.
Displaying values in the Immediate pane slows your code slightly, so you don't want to
leave Debug.Print statements in your code when you don't need to use them. You can
also use conditional compilation to specify when to include these statements and when to
ignore them during compilation and at run time.

See Also For more information on conditional compilation, see "Using Conditional
Compilation" later in this chapter.

Note The Debug window doesn't open automatically when Visual Basic encounters a
Debug.Print statement. If you don't have the Debug window open, you won't see the
values displayed by the Debug.Print statement. You can open the Debug window quickly
by pressing CTRL+G.

Tips on Using the Immediate Pane

You can use the following shortcuts in the Immediate pane:

Once you've run a statement in the Immediate pane, you can run it again by
putting the insertion point anywhere in the statement and pressing ENTER.

Before pressing ENTER, you can edit the statement in which the insertion point
appears.

You can use the mouse or the arrow keys to move the insertion point in the
Immediate pane. Press ENTER only if the insertion point is on a statement you want
to run.

You can use the PAGE UP and PAGE DOWN keys within the Immediate pane to
move through your code one page at a time. Pressing CTRL+ END moves the
insertion point to the end of the Immediate pane.

You can use the HOME key to move the insertion point to the beginning of the
current line and the END key to move the insertion point to the end of the current
line.

Takeaway: It's important for VB6 programmers to understand forms' well-defined life
cycle. Although the life cycle only consists of three stages, you need to comprehend the
complex process in which forms travel through these stages.

VB6 forms have a well-defined life cycle, which is important for programmers to
understand. The first stage in this cycle is when the form is created but is not yet loaded.
This stage begins when you call the form's Show method, or at the start of program
execution if the form is the startup form. During this state, the Form_Initialize event fires
and code in the event procedure (if there is any) executes. Normally when the
Form_Initialize event procedure has completed the form is loaded; however, code like
this can cause a form to be created and the Form_Initialize event to fire, without the form
being loaded:

Dim f As Form2

Set f = New Form2

A form can remain in the created but not loaded state even if you call a user-written form
procedure. If you set a form property, or call a built-in method such as Move, or set a
property of a control on the form, it will be loaded.
The second stage is when the form is loaded. At this point, the form's controls are created
and loaded, and the form has a window complete with a window handle (hWnd) and a
device context (hDC). The Form_Load event fires at this time, but the form is not
necessarily shown; it is in the loaded but not shown state.

While most forms move directly from this state to being shown, some programming
techniques leave a form in the loaded but not shown state in order to use the functionality
of controls on the form without displaying them (e.g., to use a Timer control to time
events, or to utilize DDE events). Most forms progress to being loaded and shown, most
often by a call to the form's Show method. In this state, the user can interact with the
form and its controls.

Calling a form's Hide method moves it from the loaded and shown state to the loaded but
not shown state. The form remains in memory and its properties (and its control
properties) remain intact. The form can be shown again, but it's important to know that
showing the form again does not trigger the Load event. This event procedure is executed
only once in a form's life cycle.

A form is finally unloaded when the Unload statement is called on it or when the program
terminates. If the form has been loaded, then calling Unload fires its QueryUnload and
Unload events in that order. If the form has been created but not loaded, you can call
Unload on it but the QueryUnload and Unload events will not fire.

When unloaded, a form is removed from the Forms collection. It is not actually
destroyed, however, until all references to it are set to Nothing. Before this is done, it
remains in the created but not loaded state. Once all references are set to Nothing, then
the form is destroyed, the Terminate event fires, and the memory and resources the form
used are released.

--

Differences between Stored procedures and User defined functions

Difference 1:

Stored procedure will be used for perform specific tasks

The stored procedure normally used to perform a speck task. The bulk of sql
statement that that will be complied and it uses the cached execution plans. It can
be return more than one result set.

Normally functions will be used for computing value

The functions are used to do the calculations instead of doing in the query. It can be
used for many places if we want the same operation.

Difference 2:
Stored procedures may or may not return values

The stored procedure based on query type it will do the operation. If we write any
select query then it will return the results. If we do only update, insert or delete then
it wont return any results. However if you want to check the confirmation of the
transaction then we can return the result. It is not compulsory to return the result
set.

But function should return value

The function must return the value. Based on the function type it will return the
results.

If we have written scalar function then it returns single value. If we have written
table valued function then it returns multiple rows. We cannot write the function
without return any value to the calling program.

Difference 3:

Stored procedure cannot be used in the select/where/having clause

The stored procedure cannot be called like the following.

SELECT * FROM Pr_RetrieveEmployees -- It will throws an error

It will throw an error. Similarly the stored procedure cannot be part the sql query
any where.

But function can be called from select/where/having clause

The function can be called using the select query.

It can be called from the select/where/having clause.

For instance SELECT [dbo].fn_EmployeeSalary (5) Ã it is scalar UDF. It returns


single value.

SELECT * FROM fn_EmployeeHistory (3) Ã its will return multi


value.

Difference 4:

Stored procedure can run independently. It can be executed using EXECUTE


or EXEC command

The stored procedure can run independently. Once the stored procedure is compiled
then it can be executed. It can be executed using the sql command statement
EXECUTE or EXEC.
EXECUTE proc_RetrieveEmployeeDetails EXEC proc_RetrieveEmployeeDetails
proc_RetrieveEmployeeDetails

But function cannot run independently

The function cannot run independently. It has to be the part of the SQL statement.

Difference 5:

Temporary table (derived) cannot be created on function.

The temporary table cannot be created in the function. As you know if you create a
temp table then it will be stored on the tempdb database. But the temp table won't
allow us to create with inside the function

There are two ways to create the temp table.

1. Create temp table

2. Derived table

SELECT * INTO #tmpEmployee FROM Employees

The above statement is derived table. It cannot create on function.

But it can be created in stored procedures

The stored procedure allows us to create the temp tables in the stored procedure.

Difference 6:

From sql server 2005 onwards, TRY CATCH statements can be used in the
stored procedures.

The TRY CATCH is one of the new features in the SQL server 2005 edition. It can be
used with inside the stored procedure. As you know it handles the error in the catch
block, whatever the statements written in the try block.

But it cannot be used in the function. But we can use raise error function.

The TRY CATCH block cannot be used with inside the functions. But we can use the
raiserror function to throw the exception.

Difference 7:

Stored procedure can call the user defined functions

The function can be called from the stored procedure.


CREATE PROC Pr_RetirveCustomers AS BEGIN SET NOCOUNT ON SET XACT_ABORT
ON SELECT * FROM Customers SELECT *
FROM [dbo].fn_GetOrderedCustomers (5) END

But the function cannot call the stored procedures.

The function cannot call the stored procedures like procedures. There are many
types of stored procedures in sql server.

• System Stored procedure


• User defined Stored procedure
• NET CLR stored procedure
• Extended stored procedure

Except extended stored procedures no one can call the user defined
functions.

Difference 8:

Stored procedures can have input and output parameters.

As you know, the input and output are the parameters which can return the results
through that variable. The output parameter can be only used to return the results
through the output variable. But the input parameter can be do the both input and
output operations.

But the function can have only input parameters.

This won't allow us to use the output parameters. But we can use input parameter.

Difference 9:

Stored procedures can have select and all DML operations.

The stored procedures can do all the DML operations like insert the new record,
update the records and delete the existing records.

But the function can do only select operation.

The function won't allow us to do the DML operations in the database tables like in
the stored procedure. It allows us to do only the select operation.

It will not allow to do the DML on existing tables. But still we can do the DML
operation only on the table variable inside the user defined functions.

Difference 10:

Function cannot have the transaction statements.


The transaction statement cannot be used in the function. Normally we won't do any
DML operations in the function.

Stored procedure can use transaction statements.

The transaction statement can be used inside the stored procedures.

Difference 11:

Stored procedures can use all the data types available in sql server.

The parameters for the stored procedures can be any data types which are available
on the sql server.

But the function cannot use the ntext, image and timestamp data types as
return type.

The function won't allow several data types of the sql server as a parameter.

Difference 12:

Stored procedures can create table variable and cannot return the table
variable.

The table variable is one of the performances tuning mechanism. Because it takes
minimum resources and it uses the memory location for store the data.
(Recommended for minimum rows)

It can be created and do the operations. But it cannot be the return type.

But the function can create, update and delete the table variable. It can
return table variable.

It can be created and can do all the DML operations and it can be the return type.
That is called the multi valued table function.

Difference 13:

Stored procedure can have the dynamic sql statement and which can be
executed using sp_executesql statement.

The stored procedure can have the dynamic sql statement for the complex decision
making operations which generated inside the stored procedures. It can be executed
using the sp_executesql statement.

But the function cannot execute the sp_executesql statement.

The function can generate the dynamic sql statement. But it cannot get execute. It
will not allow writing the sp_executesql command to execute the dynamically created
sql statement.
Difference 14:

Stored procedure allows getdate () or other non-deterministic functions can


be allowed.

The stored procedure will allow all the sql server built-in functions like
getdate(),DB_ID(),

DB_NAME (), etc..,

But the function won't allow the non-deterministic functions.

The function will not allow using non-deterministic functions like GETDATE ()
How is passing argument by value different from passing argument by reference?

Ans: Passing Arguments by Value and by Reference (Visual Basic)

In Visual Basic, you can pass an argument to a procedure by value or by reference. This is
known as the passing mechanism, and it determines whether the procedure can modify the
programming element underlying the argument in the calling code. The procedure declaration
determines the passing mechanism for each parameter by specifying the ByVal (Visual Basic) or
ByRef (Visual Basic) keyword.

Distinctions

When passing an argument to a procedure, be aware of several different distinctions that interact
with each other:

• Whether the underlying programming element is modifiable or nonmodifiable


• Whether the argument itself is modifiable or nonmodifiable
• Whether the argument is being passed by value or by reference
• Whether the argument data type is a value type or a reference type

Choice of Passing Mechanism

You should choose the passing mechanism carefully for each argument.

• Protection. In choosing between the two passing mechanisms, the most important
criterion is the exposure of calling variables to change. The advantage of passing an
argument ByRef is that the procedure can return a value to the calling code through that
argument. The advantage of passing an argument ByVal is that it protects a variable from
being changed by the procedure.
• Performance. Although the passing mechanism can affect the performance of your code,
the difference is usually insignificant. One exception to this is a value type passed ByVal.
In this case, Visual Basic copies the entire data contents of the argument. Therefore, for
a large value type such as a structure, it can be more efficient to pass it ByRef.

For reference types, only the pointer to the data is copied (four bytes on 32-bit platforms,
eight bytes on 64-bit platforms). Therefore, you can pass arguments of type String or
Object by value without harming performance.

Determination of the Passing Mechanism

The procedure declaration specifies the passing mechanism for each parameter. The calling code
cannot override a ByVal mechanism, but if a parameter is declared with ByRef, the calling code
can force the mechanism to ByVal by enclosing the argument name in parentheses in the call.

The default in Visual Basic is to pass arguments by value. You can make your code easier to
read by using the ByVal keyword. It is good programming practice to include either the ByVal or
ByRef keyword with every declared parameter.

When to Pass an Argument by Value


• If the calling code element underlying the argument is a nonmodifiable element, declare
the corresponding parameter ByVal (Visual Basic). No code can change the value of a
nonmodifiable element.
• If the underlying element is modifiable, but you do not want the procedure to be able to
change its value, declare the parameter ByVal. Only the calling code can change the
value of a modifiable element passed by value.

When to Pass an Argument by Reference

• If the procedure has a genuine need to change the underlying element in the calling
code, declare the corresponding parameter ByRef (Visual Basic).
• If the correct execution of the code depends on the procedure changing the underlying
element in the calling code, declare the parameter ByRef. If you pass it by value, or if the
calling code overrides the ByRef passing mechanism by enclosing the argument in
parentheses, the procedure call might produce unexpected results.

Differences Between Passing an Argument By Value and By Reference (Visual Basic)

When you pass one or more arguments to a procedure, each argument corresponds to an
underlying programming element in the calling code. You can pass either the value of this
underlying element, or a reference to it. This is known as the passing mechanism.

Passing by Value

You pass an argument by value by specifying the ByVal (Visual Basic) keyword for the
corresponding parameter in the procedure definition. When you use this passing mechanism,
Visual Basic copies the value of the underlying programming element into a local variable in the
procedure. The procedure code does not have any access to the underlying element in the calling
code.

Passing by Reference

You pass an argument by reference by specifying the ByRef (Visual Basic) keyword for the
corresponding parameter in the procedure definition. When you use this passing mechanism,
Visual Basic gives the procedure a direct reference to the underlying programming element in the
calling code.

Passing Mechanism and Element Type

The choice of passing mechanism is not the same as the classification of the underlying element
type. Passing by value or by reference refers to what Visual Basic supplies to the procedure
code. A value type or reference type refers to how a programming element is stored in memory.

However, the passing mechanism and element type are interrelated. The value of a reference
type is a pointer to the data elsewhere in memory. This means that when you pass a reference
type by value, the procedure code has a pointer to the underlying element's data, even though it
cannot access the underlying element itself. For example, if the element is an array variable, the
procedure code does not have access to the variable itself, but it can access the array members.

Ability to Modify
When you pass a nonmodifiable element as an argument, the procedure can never modify it in
the calling code, whether it is passed ByVal or ByRef.

For a modifiable element, the following table summarizes the interaction between the element
type and the passing mechanism.

Element type Passed ByVal Passed ByRef


Value type (contains only a The procedure cannot change the The procedure can change the
value) variable or any of its members. variable and its members.
Reference type (contains a The procedure cannot change the The procedure can change the
pointer to a class or variable but can change members of variable and members of the
structure instance) the instance to which it points. instance to which it points.

Anda mungkin juga menyukai