RMCLink Component

How to: Use from Visual Basic for Applications (VBA)

Visual Basic for Applications (VBA) is a form of Visual Basic that can be embedded into any application that chooses to embed it. For example, all newer versions of all Microsoft Office applications (Word, Excel, PowerPoint) include VBA. Many tools in the automation world do as well. The capabilities of VBA are somewhat more limited than Visual Basic 5.0 or 6.0, but significantly more powerful than VBScript.

Many different applications can include VBA. This topic will demonstrate Microsoft Excel as its sample host application, but the behavior should be similar from other VBA host applications.

VBA users should use the RMCLink COM Component interface. Specifics on using this component from VBA are described below, including a step-by-step walkthrough with Microsoft Excel. The project developed in the walk-through is included as a Microsoft Excel file (Sample.xlsm) in the RMCLink example projects.

Early Binding vs. Late Binding

VBA can reference COM components using one of two methods: early binding or late binding. With early binding, the compiler knows how the COM component is defined and can use this information to make the use of the COM component more efficient and is able to catch more errors at compile time instead of runtime. Late binding is only intended for use by languages that cannot do early binding (such as VBScript or JScript) or to use components that do not make their type information available. However, since RMCLink makes its type information available, early binding should always be used with the RMCLink COM component from VBA.

There are three things you must do to use early binding. First, you must add a reference to the COM components type library, which is described below. Second, you must declare your object variables of the proper types. Third, you should use the New keyword instead of the CreateObject function when creating an RMCLinkServer object. The following code demonstrates the last two points:

Dim srv As RMCLinkServer
Dim rmc As RMCLink
Set srv = New RMCLinkServer
Set rmc = srv.CreateEthernetLink(dtRMC70, "192.168.0.22")

This could be rewritten slightly more compactly as follows:

Dim srv As New RMCLinkServer
Dim rmc As RMCLink
Set rmc = srv.CreateEthernetLink(dtRMC70, "192.168.0.22")

Notice that the type of srv is RMCLinkServer, and the type of rmc is RMCLink. This style of declaration informs the compiler of the type so that it can do type checking and generate more efficient code throughout the program.

Here is an example of late binding for a comparison. DO NOT DO THIS!!!

Dim srv  ' DO NOT DO THIS!!! No type is specified.
Dim rmc  ' DO NOT DO THIS!!! No type is specified.
Set srv = CreateObject("RMCLink.RMCLinkServer")
Set rmc = srv.CreateEthernetLink(dtRMC70, "192.168.0.22")

Adding a Reference to the RMCLink Type Library

In order to use early binding, the Visual Basic compiler must know how the RMCLink COM component is defined. This is done by adding a reference to the RMCLink Type Library.

To add this reference:

  1. In your VBA host application, open the Visual Basic Editor. For example, in Microsoft Excel, on the Developer tab, click Visual Basic.

  2. In the Visual Basic Editor, on the Tools menu, click References.

  3. Under Available References, scroll down until you find the RMCLink Type Library.

  4. Check the box next to the RMCLink Type Library.

  5. Click OK.

To demonstrate that you correctly added the reference to RMCLink, check to see if you can see the RMCLink COM component in the Object Browser:

  1. On the View menu, click Object Browser.

  2. In the Classes list, you should be able to browse the classes and methods included in the RMCLink COM component.

  3. Close the Object Browser when you are done.

Creating an RMCLink Object

As is common for all languages, the first step is to create an instance of the RMCLink class. This is a two step process: first create an RMCLinkServer object, and then use that object to create the appropriate type of RMCLink object. The following lines demonstrate how this is done in Visual Basic:

Dim srv As New RMCLinkServer
Dim rmc As RMCLink
Set rmc = srv.CreateEthernetLink(dtRMC70, "192.168.0.22")

The first two lines use early binding to declare two typed object references. The first line declares the RMCLinkServer object, and the second line declares the RMCLink object. Notice that the New keyword in first line indicates that the first time srv is used the object will be created. Notice that this keyword cannot be used on the RMCLink declaration line since this object is not directly creatable.

The third line actually does the work. First, because it is the first use of srv, the RMCLinkServer object is created and its reference is stored in srv. Then, the CreateEthernetLink method is called to create a link for communicating with an RMC70 at IP address 192.168.0.22.

Notice that if the reference to the RMCLink Type Library has not been added to the project (described above), then these lines will fail, since the Visual Basic compiler will not recognize the RMCLink types.

ReadLFile/ReadFFile vs. ReadLFile_Script/ReadFFile_Script

Notice that although your program work the same whether you call ReadLFile or ReadLFile_Script, the latter is much less efficient and should be only used by languages that cannot use ReadLFile (e.g. VBScript and JScript). Similarly, Visual Basic should use ReadFFile instead of ReadFFile_Script.

Lifetime of the RMCLink Object

Notice that although most Visual Basic samples in this documentation control the entire lifetime of the RMCLink object within a single function, this is often not the most efficient way of using the RMCLink control. Generally, when you first want to connect to a controller, you will create the RMCLink object and call its Connect method. This RMCLink object should be located outside of the function or subroutine that creates and connects it, so that it can be used by other functions and subroutines to read or write from it. The walkthrough in the How to: Use from Visual Basic 5.0/6.0 topic demonstrates how this is done.

Walkthrough

This sample will use Microsoft Excel 2010 to set up a button to read the Actual Position of an RMC70 located at IP address "192.168.0.22". The Actual Position will be displayed in a cell in the spreadsheet and be updated each time the Update button is clicked. The project developed in this walk-through is included as a Microsoft Excel file in the RMCLink example projects.

The final application will look like this:

Step 1: Create a New Workbook

  1. Start with a new blank workbook. Microsoft Excel usually starts up with a blank workbook, but if it does not, then, on the File tab, click New and follow the instructions for creating a Blank Workbook.

  2. Save the file as a macro-enabled Excel file:

    1. On the File tab, click Save As.

    2. In the File Name box, type a name.

    3. In the Save as Type box, choose Excel Macro-Enable Workbook (*.xlsm).

    4. Click OK.

Step 2: Set up the Cells

  1. Select cell A1.

  2. Type "Actual Position:" and press Enter.

  3. Resize column A to fit the text in A1.

  4. Select cell B1.

  5. Type "0" (the number zero) and press Enter.

  6. Select cell B1 again.

  7. On the Format menu, click Cells.

  8. On the Number tab, under Category, click Number.

  9. Under Decimal places, type "3".

  10. Click OK.

Step 3: Add the RMCLink Reference

Before we can use the RMCLink COM component, we need to add a reference to the RMCLink Type Library:

  1. If the Developer tab is not visible, make it visible:

    1. On the File tab, click Options.

    2. Click Customize Ribbon.

    3. In the Customize the Ribbon box, choose Main Tabs.

    4. In the list, check the box next to Developer.

    5. Click OK.

  2. On the Developer tab, click Visual Basic.

  3. In the Visual Basic Editor, on the Tools menu, click References.

  4. Under Available References, scroll down until you find the RMCLink Type Library.

  5. Check the box next to the RMCLink Type Library.

  6. Click OK.

Step 4: Add the Update Button

  1. On the Developer tab, click Design Mode.

  2. On the Developer tab, click Insert and select the Command Button (Active X) control.

  3. Drag to create a button in the location and of the size shown above.

  4. Right-click the button and choose Properties.

  5. Change the (Name) property to "updateButton".

  6. Change the Caption property to "Update".

  7. Close the Properties window.

Step 5: Add the Button Handler

Double-click on the Update button you have just created. This should re-open the Microsoft Visual Studio window an empty click handler subroutine for this button. Enter the text below for the body of this subroutine. Change the IP address to the IP address of your controller.

updateButton_Click Handler

Private Sub updateButton_Click()
    On Error GoTo HandleError
    Dim srv As New RMCLinkServer
    Dim rmc As RMCLink
    Set rmc = srv.CreateEthernetLink(dtRMC70, "192.168.0.22")
 
    ' Connect to the controller.
    rmc.Connect
 
    ' Read the Actual Position from Axis 0 of the RMC70.
    Dim data As Variant
    data = rmc.ReadFFile(fn70StatusAxis0, 8, 1)
 
    ' Copy the Actual Position we read into cell 'B1'.
    Cells(1, 2) = data(0)
 
     ' Disconnect from the controller.
    rmc.Disconnect
    Exit Sub
 
HandleError:
    MsgBox "Communication Error #" & Err.Number & ": " & Err.Description
End Sub

Step 6: Run the Application

  1. On the Run menu, click Exit Design Mode.

  2. Close the Microsoft Visual Basic window.

  3. You should now be able to click our custom Update button to automatically read the current Actual Position and display it in cell B1.

See Also

RMCLink COM Component | How Do I Overview | RMCLink Component


Copyright (c) 2023 by Delta Computer Systems, Inc.