A Do…While loop is used when we want to repeat a set of statements as long as the condition is true. The condition may be checked at the beginning of the loop or at the end of the loop.
Following is the syntax of a Do…While loop in VBA.
On a VBA side, we go through the use of VBA interface implementation mechanism. Interface implementation Here is a small example on using interface implementation mechanism in VBA.
The following example uses Do…while loop to check the condition at the beginning of the loop. The statements inside the loop are executed, only if the condition becomes True.
When the above code is executed, it prints the following output in a message box.
There is also an alternate Syntax for Do…while loop which checks the condition at the end of the loop. The major difference between these two syntax is explained in the following example.
The following example uses Do…while loop to check the condition at the end of the loop. The Statements inside the loop are executed at least once, even if the condition is False.
When the above code is executed, it prints the following output in a message box.
-->Specifies an interface or class that will be implemented in the class module in which it appears.
Implements [ InterfaceName | Class ]
The required InterfaceName or Class is the name of an interface or class in a type library whose methods will be implemented by the corresponding methods in the Visual Basic class.
An interface is a collection of prototypes representing the members (methods and properties) that the interface encapsulates; that is, it contains only the declarations for the member procedures. A class provides an implementation of all the methods and properties of one or more interfaces. Classes provide the code used when each function is called by a controller of the class. All classes implement at least one interface, which is considered the default interface of the class. In Visual Basic, any member that isn't explicitly a member of an implemented interface is implicitly a member of the default interface.
When a Visual Basic class implements an interface, the Visual Basic class provides its own versions of all the Publicprocedures specified in the type library of the Interface. In addition to providing a mapping between the interface prototypes and your procedures, the Implements statement causes the class to accept COM QueryInterface calls for the specified interface ID.
Note
Visual Basic does not implement derived classes or interfaces.
When you implement an interface or class, you must include all the Public procedures involved. A missing member in an implementation of an interface or class causes an error. If you don't place code in one of the procedures in a class you are implementing, you can raise the appropriate error (Const E_NOTIMPL = &H80004001) so a user of the implementation understands that a member is not implemented.
The Implements statement can't appear in a standard module.
The following example shows how to use the Implements statement to make a set of declarations available to multiple classes. By sharing the declarations through the Implements statement, neither class has to make any declarations itself. The example also shows how use of an interface allows abstraction: a strongly-type variable can be declared by using the interface type. It can then be assigned objects of different class types that implement the interface.
Assume there are two forms, SelectorForm and DataEntryForm. The selector form has two buttons, Customer Data and Supplier Data. To enter name and address information for a customer or a supplier, the user clicks the customer button or the supplier button on the selector form, and then enters the name and address by using the data entry form. The data entry form has two text fields, Name and Address.
The following code for the interface declarations is in a class called PersonalData:
The code supporting the customer data is in a class module called Customer. Note that the PersonalData interface is implemented with members that are named with the interface name PersonalData_
as a prefix.
The code supporting the supplier data is in a class module called Supplier:
The following code supports the Selector form:
The following code supports the Data Entry form:
Note how, in the data entry form, the m_pd variable is declared by using the PersonalData interface, and it can be assigned objects of either the Customer or Supplier class because both classes implement the PersonalData interface.
Also note that the m_pd variable can only access the members of the PersonalData interface. If a Customer object is assigned to it, the Customer-specific member CustomerAgentId is not available. Similarly, if a Supplier object is assigned to it, the Supplier-specific member NumberOfProductLines is not available. Assigning an object to variables declared by using different interfaces provides a polymorphic behavior.
Also note that the Customer and Supplier classes, as defined earlier, do not expose the members of the PersonalData interface. The only way to access the PersonalData members is to assign a Customer or Supplier object to a variable declared as PersonalData. If an inheritance-like behavior is desired, with the Customer or Supplier class exposing the PersonalData members, public members must be added to the class. These can be implemented by delegating to the PersonalData interface implementations.
For example, the Customer class could be extended with the following:
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.