To see how this works, insert two standard modules in a new project and draw three command buttons on a form.
One variable, intX, is declared in the first standard module, Module1. The Test procedure sets its value:Public intX As Integer ' Declare Module1's intX.
The second variable, which has the same name, intX, is declared in the second standard module, Module2. Again, a procedure named Test sets its value:Public intX As Integer ' Declare Module2's intX.
The third intX variable is declared in the form. module. And again, a procedure named Test sets its value.Public intX As Integer ' Declare the form's intX
Each of the three command buttons' Click event procedures calls the appropriate Test procedure and uses MsgBox to display the values of the three variables.Private Sub Command1_Click()
Run the application and click each of the three command buttons. You'll see the separate references to the three public variables. Notice in the third command button's Click event procedure, you don't need to specify Form1.Test when calling the form's Test procedure, or Form1.intX when calling the value of the form's Integer variable. If there are multiple procedures and variables with the same name, Visual Basic takes the value of the more local variable, which in this case, is the Form1 variable.
来自 “ ITPUB博客 ” ，链接：http://blog.itpub.net/11411056/viewspace-734287/，如需转载，请注明出处，否则将追究法律责任。