Microsoft KB Archive/195087

= How To Enable Communications Between UserDocuments/ UserControls =

Article ID: 195087

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q195087



SUMMARY
This article covers how to enable communications between UserControls and UserDocuments and other programs. There are times when both UserControls and UserDocuments need to communicate with other forms that are part of the entire program. Making public functions and public subroutines only enables communications from parent forms and processes. Using GLOBAL instead of PUBLIC allows information to be shared between controls, classes, and forms.



MORE INFORMATION
The project code below is an example of Global Variables being shared:

 Create a Standard EXE project in Visual Basic. Form1 is created by default. Select ActiveX Document EXE.  Add two additional UserDocuments and one module to the project. Cut and paste the following code into the module's code pane: Option Explicit

Global MyNumber As Variant

  For Document1, add two commandbuttons, a label. and one textbox. Place the label above the textbox and clear text1.Text. Cut and paste the following code into its code pane: Private Sub UserDocument_Initialize Command2.Caption = ">>" Label1.Caption = "Set the number below" End Sub

Private Sub Command1_Click MyNumber = Text1.Text End Sub

Private Sub Command2_Click UserDocument.Hyperlink.NavigateTo App.Path & "\UserDocument2.Vbd" End Sub

  For Document2, add two command buttons, a label and one textbox. Place the label above the textbox and clear text1.Text. Cut and paste the following code into its code pane: Private Sub Command1_Click UserDocument.Hyperlink.GoBack End Sub

Private Sub Command2_Click UserDocument.Hyperlink.NavigateTo App.Path & "\UserDocument3.vbd" End Sub

Private Sub UserDocument_Initialize Command1.Caption = "<<" Command2.Caption = ">>" Label1.Caption = "The Magic Number Is" Text1.Text = MyNumber End Sub

  For Document3, add two command buttons, a label and one textbox. Place the label above the textbox and clear text1.Text. Cut and paste the following code into its code pane: Private Sub Command1_Click UserDocument.Hyperlink.GoBack End Sub

Private Sub UserDocument_Initialize Command1.Caption = "<<" Label1.Caption = "The Magic Number Is" Text1.Text = MyNumber End Sub

 Run the project. Type a number in the text box and click the Command1 button. Next, navigate to the other two documents and notice your number shows up. Navigate back. When you navigate back to UserDocument1, the number you placed in the text will be gone. Also notice that no matter how many times you navigate back and forth between the documents, the number you added to the Global variable is always the same and always available.</li></ol>

<div class="references_section">