Documentos de Académico
Documentos de Profesional
Documentos de Cultura
To declare a variable is to tell the program about it in advance. You declare a variable with the Dim
statement, supplying a name for the variable:
Dim variablename [As type]
Variables declared with the Dim statement within a procedure exist only as long as the procedure is
executing. When the procedure finishes, the value of the variable disappears. In addition, the value of a
variable in a procedure is local to that procedure — that is, you can't access a variable in one procedure
from another procedure. These characteristics allow you to use the same variable names in different
procedures without worrying about conflicts or accidental changes.
3. What is the difference between Exec and ExecSQL? (wouldnt this be dependent on data access? Ie ADO
use different method than DAO)
Executes a system procedure, a user-defined stored procedure, or an extended stored procedure.
Also supports the execution of a character string within a Transact-SQL batch.
Statement Description
Program execution resumes with the statement that caused the error or the
most recently executed call out of the procedure containing the error-
Resume [0]
handling routine. Use it to repeat an operation after correcting the
condition that caused the error.
Resumes program execution at the statement immediately following the
one that caused the error. If the error occurred outside the procedure that
Resume Next contains the error handler, execution resumes at the statement
immediately following the call to the procedure wherein the error
occurred, if the called procedure does not have an enabled error handler.
Resumes program execution at the label specified by line, where line is a
Resume line line label (or nonzero line number) that must be in the same procedure as
the error handler.
Triggers a run-time error. When this statement is executed within the
error-handling routine, Visual Basic searches the calls list for another
Err.Raise Number:= number error-handling routine. (The calls list is the chain of procedures invoked to
arrive at the current point of execution. See the section, "Error-Handling
Hierarchy," later in this chapter.)
5. What is the difference between a property a method and an event? Give an example of each.
1. Text1.color ------ is a property (Mostly to change the appearance )
2. Text1.text ------- is Method (Mostly to set values which r not graphical)
3. button 1. Click -- is event (Event is the result of user interaction with application)
6. What is a variant data type and when would you use it?
Variant will store all the different possible VB data received at one place
7. Why is it not good to use a variant data type?
Using variant rather than a specific data type is slower because of conversions needed and takes up more
memory because additional over head
8. What is the difference between a Dynaset and Snapshot and how would you create one? (we dont use
snapshot in VB5+)
A dynaset-type Recordset object is a dynamic set of records that can contain fields from one or more tables
or queries in a database and may be updatable. In an ODBCDirect database, a dynaset-type Recordset
object corresponds to an ODBC keyset cursor.
Remarks
A dynaset-type Recordset object is a type of Recordset object you can use to manipulate data in an
underlying database table or tables.
It differs from a snapshot-type Recordset object because the dynaset stores only the primary key for each
record, instead of actual data. As a result, a dynaset is updated with changes made to the source data, while
the snapshot is not. Like the table-type Recordset object, a dynaset retrieves the full record only when it's
needed for editing or display purposes.
A snapshot-type Recordset object is a static set of records that you can use to examine data in an
underlying table or tables. In an ODBCDirect database, a snapshot-type Recordset object corresponds to a
static cursor.
Remarks
To create a snapshot-type Recordset object, use the OpenRecordset method on an open database, on
another dynaset- or snapshot-type Recordset object, or on a QueryDef object.
10.How would you define and use a Boolean in VB? (Integer in VB3 Boolean in VB6+)
Dim [Private][Public] x as Boolean
Default is false
11. What is the difference between Dim nCount as Integer vs Dim nCount% and when would you use each?
No difference
12.What is the difference between <F8> and <Shift-F8> when debugging?
F8- will take u through all the executable statements
<SHIFT> f8 – will skip the calls to subroutines
13.What is the statement to extract 4 characters from the middle of a string?
Mid(string, start [, length] )
14. What is the error message that you would get if you try to assign "Null" to an integer variable?
Invalid use if null
16. What is "Option Explicit"?
It mandates the programmer to declare the variables before using
17. What other options are there is VB?
Option private
Option compare text,binary
Option base 0,1
Early-Bound Declarations
Early binding allows you to declare an object variable as a programmatic identifier, or
class name, rather than as an Object or a Variant data type. The programmatic identifier
of an application is stored in the Windows registry as a subkey below the
\HKEY_CLASSES_ROOT subtree. For example, the programmatic identifier for Access
is "Access.Application"; for Excel it is "Excel.Application."
When you are using early binding, you can initialize the object variable by using the
CreateObject or GetObject function or by using the New keyword if the application
supports it. All Office 2000 applications can be initialized by using the New keyword.
Because the Outlook 2000 programming environment for Outlook items supports only
scripting, you can't use early binding declarations of any sort in its VBScript
programming environment; however, you can use early binding in VBA code in a local
Outlook VBA project or COM add-in, or in Automation code that works with Outlook
from another host application.
Early binding is the friendly name for what C programmers call virtual function table
binding, or vtable binding. In order to use early binding, the host application must
establish a reference to a type library (.tlb) or an object library (.olb), or an .exe, .dll, or
.ocx file that contains type information about the objects, methods, properties, and events
of the application or service you want to automate.
In the following code fragment, an Application variable is declared by using the
programmatic identifier for Word (Word.Application) and a new instance of Word is
created by using the Set statement with the New keyword:
Dim wdApp As Word.Application
Set wdApp = New Word.Application
If the code following these lines doesn't set the Application object's Visible property to
True, the new instance of Word will be hidden. All Office applications are hidden by
default when they are automated from another application.
Use early binding whenever possible. Early binding has the following advantages:
Syntax checking When you use early binding, VBA checks the syntax of your
statements against the syntax stored in the object library during compilation rather than
checking it at run time, so that you can catch and address errors at design time. For
example, VBA can determine if you are using valid properties or methods of an object,
and if you are passing valid arguments to those properties and methods.
Support for statement-building tools When you use early binding, the Visual Basic
Editor supports features that make writing code much easier and less prone to errors, such
as automatic listing of an object's properties and methods, and pop-up tips for named
arguments.
Support for built-in constants When you use early binding, your code can refer to the
built-in constants for method arguments and property settings because this information is
available from the type library at design time. If you use late binding, you must define
these constants in your code by looking up the values in the application's documentation.
Better performance Performance is significantly faster with early binding than with
late binding.
Late-Bound Declarations
Late binding allows you to declare a variable as an Object or a Variant data type. The
variable is initialized by calling the GetObject or CreateObject function and specifying
the application's programmatic identifier. For example, in the following code fragment,
an Object variable is declared and then set to an instance of Access by using the
CreateObject function:
Dim objApp As Object
Set objApp = CreateObject("Access.Application")
Late binding is the friendly name for what C programmers used to call IDispatch binding,
and was the first method of binding implemented in applications that can control other
applications through Automation. For this reason, you can use late binding to maintain
backward compatibility with older applications. However, late binding uses a lot of
overhead; it is faster than dynamic data exchange (DDE), but slower than early binding.
Tip DDE is a protocol that was established before OLE for exchanging data between
Windows applications. There is no need to use DDE to exchange data between Office
applications because of their support for Automation. However, you may have to use
DDE from some other application that doesn't support Automation code in order to work
with data from an Office application. For more information about using DDE, search the
Visual Basic Reference Help for the Office application you want to work with.
The CreateObject function must also be used to work with objects from any Automation
component from script. This is because scripting has no method of establishing references
to type libraries to support early binding.
• In syntax, named arguments are shown as bold and italic. All other arguments are shown in italic
only.
Important You cannot use named arguments to avoid entering required arguments. You
can omit only the optional arguments. For Visual Basic (VB) and Visual Basic for
applications (VBA) object libraries, the Object Browser encloses optional arguments with
square brackets [ ].
For More Information See "ByVal," "ByRef," "Optional," and "ParamArray" in the
Language Reference
• The sub, function, or property you call with AddressOf must be in the same project as the related
declarations and procedures.
• You can only use AddressOf with user-defined subs, functions, or properties — you cannot use it
with external functions declared with the Declare statement, or with functions referenced from
type libraries.
• You can pass a function pointer to an argument that is typed As Any or As Long in a declared Sub,
Function, or user-defined type definition.
Note You can create your own call-back function prototypes in DLLs compiled with
Visual C++ (or similar tools). To work with AddressOf, your prototype must use the
__stdcall calling convention. The default calling convention (_cdecl) will not work with
AddressOf
26. How will you retain the values of existing elements in a dynamic array when you want to alter the array
size?
Redim with preserve keyword
27. How will you define a function to accept variable number of arguments ?
USING PARAMARRAY
28. How will you define a function to accept optional arguments?
Using optional key word
29. How does the following statements work? On Error Goto Err_Handler
control switches to err handler
30. How does the following statements work? On Error Resume Next
control switches to next executable statement
31 .How does the following statements work? On Error Goto 0
---control switches to same statement again
32 .What will happen if you issue a Resume statement after handling an error?
control switches to same statement again
As mentioned in "Showing Forms from the CoffeeManager Class," modal and modeless
forms displayed by an out-of-process component have a different relationship to a client
application’s forms than they would if displayed by an in-process component. Running
CoffeeWatch will demonstrate this.
Note This topic is part of a series that walks you through creating a sample ActiveX
EXE. It begins with the topic Creating an ActiveX EXE Component.
To demonstrate modal and modeless form behavior with the out-of-process Coffee
component
1. Press F5 to run the CoffeeWatch test program.
2. Click Show Modal Form to display a modal form from the Coffee component.
Depending on your system configuration, the order programs were started, and so
on, the modal form may come up in front of CoffeeWatch — or you may see
something like this:
3. TestForm is not really modal with respect to the CoffeeWatch form. To see this, click anywhere on
the CoffeeWatch form. The Component Request Pending dialog box appears, as shown here:
Note Depending on your system configuration and the order in which programs
are loaded, the copy of Visual Basic in which Coffee is running may come to the
front along with TestForm, obscuring CoffeeWatch.
5. Click on CoffeeWatch again, to bring it to the front and display the Component Request
Pending dialog box again.
Note If you can’t see CoffeeWatch (the form, not the project), use the task bar
(or press ALT+TAB) to bring it to the front.
In the strict sense of the word, TestForm is modal to CoffeeWatch. That is, you
can’t do anything with CoffeeWatch until TestForm is dismissed. However,
because the two forms are in different processes, CoffeeWatch can appear on top
of TestForm.
6. Click Switch To, to bring TestForm to the front, and then click TestForm’s close box to dismiss
the modal form.
TestForm behaves like a modeless form that’s not owned by CoffeeWatch. That is,
it doesn’t stay on top of CoffeeWatch. You can verify this by clicking on each of
the two forms, to bring them alternately to the front.
Important Because TestForm is in a different process from CoffeeWatch, you
cannot make CoffeeWatch the owner of TestForm as you would if the forms were
in the same process — that is, by passing a reference to CoffeeWatch in the
OwnerForm argument of TestForm. For details see "Displaying Forms from Code
Components," in Chapter 8, "Building Code Components."For details see
"Displaying Forms from Code Components," in "Building Code Components."
8. DO NOT dismiss the modeless TestForm. Instead, close CoffeeWatch by clicking its close box.
TestForm doesn’t close. (It may be hidden behind the instance of Visual Basic
containing the CoffeeWatch project — use the task bar or ALT+TAB to bring it to
the front.)
This illustrates two important points: First, a form displayed by an out-of-process
component is not dependent on the client application. Nor is its lifetime limited by
the client’s lifetime.
Second, a loaded form can keep an out-of-process component’s executable from
unloading. For details, see "Starting and Ending a Component" in Chapter 6,
"General Principles of Component Design."For details, see "Starting and Ending a
Component" in "General Principles of Component Design."
In its Terminate event, CoffeeMonitor should unload any forms it has shown.
9. Dismiss TestForm by clicking its close box. The Coffee component remains in run mode. To
return Coffee to design mode, click the End button, or select End from the Run menu.
Once you put an ActiveX EXE project in run mode, it remains in run mode. This
is convenient for testing, but it’s different from the behavior of the made .exe. The
executable for an out-of-process component unloads when the last client releases
its last reference to an object provided by the component, as discussed in "Starting
and Ending a Component."
Note The only way to test the shutdown behavior of an out-of-process
component is to test with the made executable.
The lesson to take away from all this is that out-of-process code components are
generally not the best way to show forms.
Step by Step
This topic is part of a series that walks you through creating a sample ActiveX EXE.
To See
Go to the next step Providing an Asynchronous Notification Event
Start from the beginning Creating an ActiveX EXE Component
37. What are the different cursor types that can we create using ADO Recordset?
Difference between these cursor types?
Visual Basic Concepts
In Visual Basic, three data access interfaces are available to you: ActiveX Data Objects
(ADO), Remote Data Objects (RDO), and Data Access Objects (DAO). A data access
interface is an object model that represents various facets of accessing data. Using Visual
Basic, you can programmatically control the connection, statement builders, and returned
data for use in any application.
Why are there three data access interfaces in Visual Basic? Data access technology is
constantly evolving, and each of the three interfaces represent a different state of the art.
The latest is ADO, which features a simpler — yet more flexible — object model than
either RDO or DAO. For new projects, you should use ADO as your data access
interface.
The ActiveX Data Objects (ADO) cursor provider, part of the Remote Data Service
(RDS) technology, provides several types of cursors as shown in the following table.
Cursor type Constant
Forward-only cursor adOpenForwardOnly
Keyset-driven cursor adOpenKeyset
Dynamic cursor adOpenDynamic
Static cursor adOpenStatic
You can control how the data source and the chosen ADO cursor library manage
concurrency with the locking options in the following table.
Locking type Constant
Pessimistic concurrency. adLockPessimistic
Optimistic concurrency using row values. adLockOptimistic
Read-only. Changes are not permitted. adLockReadOnly
All updates are deferred until the batch update is finished. To adLockBatchOptimistic
do batch updating, you should select either a keyset or static
cursor.
For More Information For more information on ADO cursor options, search online for
"CursorType Property" in MSDN Library Visual Studio 6.0. For more information on
Remote Data Service (RDS) and how it provides cursor support with ActiveX Data
Objects, search online for "Remote Data Service Developer's Guide" and "Understanding
Remote Data Service Applications" in MSDN Library Visual Studio 6.0. For more
information on using locks to handle multiuser concurrency situations, see Managing
Concurrency with Cursor Locks in this chapter.
39. What is the difference between a ActiveX DLL and a ActiveX EXE?
ActivexDLL is executes in in process
ActivexExe executes in outprocess
40. What are the types of Instancing property that can be set for a Class in a ActiveX DLL and ActiveX
EXE?
Visual Basic Concepts
The value of the Instancing property determines whether your class is private — that is,
for use only within your component — or available for other applications to use.
As its name suggests, the Instancing property also determines how other applications
create instances of the class. The property values have the following meanings.
• Private means that other applications aren’t allowed access to type library information about the
class, and cannot create instances of it. Private objects are only for use within your component.
• PublicNotCreatable means that other applications can use objects of this class only if your
component creates the objects first. Other applications cannot use the CreateObject function or the
New operator to create objects from the class.
• MultiUse allows other applications to create objects from the class. One instance of your
component can provide any number of objects created in this fashion.
• SingleUse allows other applications to create objects from the class, but every object of this class
that a client creates starts a new instance of your component. Not allowed in ActiveX DLL
projects.
• GlobalSingleUse is like SingleUse, except that properties and methods of the class can be invoked
as if they were simply global functions. Not allowed in ActiveX DLL projects.
Global Objects
Frequently it’s useful to have utility functions that users of your component can employ
without first creating an instance of one of your objects. In out-of-process components,
such functions are frequently implemented as properties or methods of the Application
object.
If the Instancing property for a class is marked GlobalMultiUse or GlobalSingleUse, then
properties and methods of the class can be invoked without explicitly creating an instance
of the object.
For example, suppose you want your SmallMechanicals component to provide a
GlobalUtility object whose methods are general-purpose functions. You can add a class
module to the SmallMechanicals project, set the Name property to GlobalUtility, and set
the Instancing property to GlobalMultiUse.
Now you can add properties and methods to the class module. For example, you might
implement a ReversePolarity method and a read-only WidgetCount property:
Public Sub ReversePolarity()
' (Code to reverse polarity on all Widgets.)
End Sub
In the client application, the ReversePolarity method can be invoked without first
creating a GlobalUtility object:
Private Sub Command1_Click()
' No object variable is required.
ReversePolarity
End Sub
Note The properties and methods of a GlobalMultiUse object are not part of the global
name space of the component that provides the object. For example, within a project that
contains the GlobalUtility class, you must explicitly create an instance of GlobalUtility in
order to use the object's properties and methods. Other limitations of global objects are
listed in "Global Objects and Code Libraries," in Chapter 8, "Building Code
Components."Other limitations of global objects are listed in "Global Objects and Code
Libraries," in "Building Code Components."
Be careful when choosing names for the properties and methods of global objects. Using
common or obvious names may result in name collisions with other components. Name
conflicts must be resolved by qualifying the property or method with the type library
name:
Private Sub Command1_Click()
SmallMechanicals.ReversePolarity
Esalen.ReversePolarity
End Sub
Important The "global" in global objects refers to the fact that all of the object’s
properties and methods are available in the global name space of your project. It does not
mean that one object is automatically shared by all clients. Each client that uses your
component gets its own global object.
For More Information "Providing Named Constants for Your Component," later in this
chapter, discusses the use of global objects to provide string constants and non-integer
constants. Code components are discussed in depth in Chapter 8, "Building Code
Components."Code components are discussed in depth in "Building Code Components."
41. What are the different levels of compatibilty that can be set for a ActiveX component in VB and what
do they imply?
Visual Basic Concepts
• Version compatible means that objects and/or methods have been added to the type library, but no
changes were made to existing properties or methods. Both old and new client applications can
use the component.
• Version incompatible means that at least one property or method that existed in the old type library
has been changed or removed. Existing client applications that have references to the component
cannot use the new version.
Version-Identical Interfaces
Once your component has been distributed as part of an application, there are several
situations that might cause you to release an update. You might want to optimize the
performance of a method that had turned out to be a bottleneck for users. You might also
need to change the internal implementation of an object’s method to reflect changes in the
business rule on which the method was based.
You can change the code in existing Property procedures or methods, and still have a
version-identical interface, as long as you do not change the names or data types of their
parameters, the order of the parameters, the name of the property or method, or the data
type of the return value.
When you create the executable for a version-identical upgrade, you can use the same file
name for the executable. Visual Basic uses the same version number for the type library.
Important When you release a new version of your component with a version-identical
or version-compatible interface, and retain the same file name for the executable, you
should always use the Make tab of the Project Properties dialog box to increment the file
version number. This ensures that the setup programs for applications that use your
component will replace old versions during setup.
Version-Compatible Interfaces
When you enhance your component by adding new classes, or new properties and
methods to existing classes, you can continue to use the same name for your executable.
As long as you make no changes to existing properties and methods, Visual Basic updates
the version number of the type library but keeps it compatible with the old version
number.
Client applications that are built using the new version of your component will compile
with the new version number, and can make use of all the new features. They cannot be
used with earlier versions of your component, however, because type library versions are
only upward-compatible.
As with version-identical releases, remember to increment the file version number of the
executable.
Version-Incompatible Interfaces
Sometimes design decisions made in an earlier version of a component fail to anticipate
future needs. If you want the code in the component to be useful in new development
projects, you have to change the interface.
For example, the CupsPerAnnum parameter of the Coffee method might be implemented
as an Integer in the first version of a component. It may become apparent, after the
component has been in use for some time, that some clients need to pass a larger value
than can be contained in an Integer.
Changing the declaration of a method is only one of several actions that will cause Visual
Basic to make the version number of the type library incompatible, rendering the new
version unusable with client applications compiled with earlier versions. The following
changes will cause a version incompatibility:
• Changing the Project Name field on the General tab of the Project Properties dialog box.
• Changing the Name property of any class module whose Public property is True (controls), or
whose Instancing property is not Private (class modules).
• Deleting a public variable, procedure, or Property procedure from a public class module or
control, or changing it to Private or Friend.
• Changing the name or data type of a public variable, procedure, or Property procedure in a public
class module or control.
• Changing the names, data types, or order of the parameters of a public procedure or Property
procedure in a public class module or control.
• Changing the Procedure ID (DispID) or other parameters in the Procedure Attributes dialog box.
• Maintaining Binary Compatibility Describes the versioning system Visual Basic uses to prevent
compatibility problems.
• Levels of Binary Version Compatibility Describes the degrees of binary compatibility Visual
Basic measures.
• Providing a Reference Point for Binary Version Compatibility Describes when and how to
specify a version of your component as a reference point for version compatibility.
• Using Binary Version Compatibility Describes when and how to use the feature, problems you
may encounter, and messages you may get from Visual Basic.
For More Information See "Polymorphism, Interfaces, Type Libraries, and GUIDs," in
Chapter 6, "General Principles of Component Design" for background information and
concepts.See "Polymorphism, Interfaces, Type Libraries, and GUIDs" in "General
Principles of Component Design" for background information and concepts.
An object that raises events is called an event source. To handle the events raised by an
event source, you can declare a variable of the object's class using the WithEvents
keyword.
This topic continues the Widget object example begun in "Declaring and Raising Events."
To handle the PercentDone event of a Widget, place the following code in the
Declarations section of Form1:
To handle the PercentDone event of a Widget, place the following code in the
Declarations section of Form1:
Option Explicit
Private WithEvents mWidget As Widget
Private mblnCancel As Boolean
The WithEvents keyword specifies that the variable mWidget will be used to handle an
object's events. You specify the kind of object by supplying the name of the class from
which the object will be created.
The variable mWidget is declared in the Declarations section of Form1 because
WithEvents variables must be module-level variables. This is true regardless of the type
of module you place them in.
The variable mblnCancel will be used to cancel the LongTask method.
• You cannot declare a WithEvents variable As New. The event source object must be explicitly
created and assigned to the WithEvents variable.
• You cannot declare WithEvents variables in a standard module. You can declare them only in class
modules, form modules, and other modules that define classes.