How To: Launch Visual Basic from ArcView


This example allows ArcView to start a Visual Basic application, pass a list of field names from the current table to the application, populate a list control, and as the user selects fields from the list, display the field definition for the selected field.


The following is an outline of what this example will do:

1. Launches the Visual Basic application.
2. Establishes a Dynamic Data Exchange(DDE) conversation with ArcView as the destination and the Visual Basic application as the source. (For information on DDE see the ArcView help Index tab and search for DDE)
3. Passes a list of field names from the active table to Visual Basic, populating the list control.
4. Terminates the DDE conversation.

Visual Basic-
1. Initiates another DDE conversation, this time with ArcView as the source and the Visual Basic application as the destination.
2. Sends Avenue requests to obtain the field type and width.
ArcView and Visual Basic must be installed to perform the following steps.

  1. Open a script, and add the following lines of code. This is the script that starts the Visual Basic application and sends the field names from ArcView through DDE.

    'FIELDDEF is what you'll name the Visual Basic application,
    'Transfer is the topic of conversation.
    aDDE = DDEClient.Make ("FIELDDEF","Transfer")
    if (aDDE.HasError) then
    'Substitute your pathname to the executable program here
    System.Execute ("C:\DDE\FIELDDEF")
    aDDE = DDEClient.Make ("FIELDDEF","Transfer")
    theVTab = av.GetActiveDoc.GetVtab
    theFields = theVTab.GetFields
    if (theFields.Count = 0) then
    MsgBox.Info("No fields in table","")
    for each f in theFields
    'Send each field name to Visual Basic
    aDDE.Execute (f.GetName)

    This script attempts to establish a DDE conversation with the Visual Basic application. If the application is not currently running, ArcView uses the System.Execute request to start the application and then initiates a conversation. Then, ArcView passes the field names, one at a time, to the Visual Basic application
  2. Compile the script and rename it Field.Def.
  3. Attach the script to the Click property of a button in the Table DocGUI.

    A. Activate the Project window.
    B. Select Project menu > Customize.
    C. In the Customize dialog box, select the desired GUI from the Type dropdown list.
    D. Select Category > Buttons.
    E. Click New.
    F. Double-click the Click property in the Customize dialog box.
    G. Enter the name of the desired script in the Script Manager and click Select.
    H. Close the Customize dialog box.

    For more information, see 'Customize dialog box' in ArcView Help.

  4. Save the project.

    The next task is to construct the Visual Basic application. Here are the steps to follow to construct the form:
  5. Start Visual Basic and create a form with a list box and two labels and one command button. Position and size the controls in a manner that is visually pleasing. The list box will contain the list of fields from ArcView, the labels will display the field type and width, and the command button will cancel the form.

    The following table lists the property settings for the controls on the form.

    Object		Property	Setting
    Form Caption Field Definition
    LinkMode 1 - Source
    LinkTopic Transfer
    List Box MultiSelect 0 - None
    Name lstFields
    Label 1 Caption (Empty)
    Name lblFldType
    LinkTimeout 500
    Label 2 Caption (Empty)
    Name lblFldWidth
    LinkTimeout 500
    Command Button Label Cancel
    Name cmdCancel

  6. Add the following code to the general declaration section of Form1:

    Const NONE = 0, MANUAL = 2

  7. Add the following code to the Form_LinkExecute event procedure. This event is executed when ArcView initiates a conversation with the Visual Basic application. ArcView sends a field name string to the Visual Basic form through DDE. The field name is then added to the list. Make sure you change the name of the string argument to FieldName.

    Sub Form_LinkExecute (FieldName As String, Cancel As Integer)
    lstFields.AddItem FieldName
    End Sub

  8. Add this code to the Form_Unload event procedure:

    Sub Form_Unload (Cancel As Integer)
    'This event procedure is executed from the Cancel button, or
    'when the user selects Close on the form's control menu.
    lblFldType.LinkMode = NONE 'Close DDE communication
    lblFldWidth.LinkMode = NONE
    End Sub

  9. Add this code to the lstFields_Click event procedure:

    Sub lstFields_Click()
    If lblFldType.LinkMode = NONE Then
    'Set up conversation with each label
    lblFldType.LinkTopic = "ArcView|System"
    lblFldType.LinkMode = MANUAL
    lblFldWidth.LinkTopic = "ArcView|System"
    lblFldWidth.LinkMode = MANUAL
    End If
    lblFldType.LinkItem = "av.GetActiveDoc.GetVtab.FindField(""" & lstFields & """).GetType "
    lblFldWidth.LinkItem = "av.GetActiveDoc.GetVtab.FindField(""" & lstFields & """).GetWidth "

    If lblFldType.Caption = "nil" Then
    'ArcView's table is no longer active doc
    Dim Msg
    Msg = "The original table is no longer the active document."
    MsgBox Msg, 48, "ArcView's environment has changed"
    Unload Form1
    End ' Terminate the application.
    End If
    End Sub

  10. Add the following code to the cmdCancel_Click event procedure:

    Sub cmdCancel_Click ()
    Unload Form1
    End Sub

  11. Save your Visual Basic Project and make an EXE file. The file name that you specify for the executable is the name that you'll need to remember to establish the DDE conversation with ArcView. Specify FIELDDEF.EXE as the file name to match the name specified above in the Avenue script.
  12. Start ArcView and open the project you created, if it's not already open.
  13. Add a table to the project and click on the button you created.
    This should launch the Visual Basic application and display the list of field names from the active table. Select a name from the list and its field type and width should appear.