tags:

views:

38

answers:

1

In an attempt to clean up my code, I have tried to put all my functions and subroutines in a module. One of these subroutines populated a grid of RichTextBoxes with data from a MS Access database. It worked when the subroutine was in the form that it was being used on, but not in my Module. Is there any specific reason for this? I noticed while trying to use a RichTextBox in the code it said I needed to define it, so I went ahead and added this to the top of my Module:

Dim mon1 As New RichTextBox
Dim mon2 As New RichTextBox
Dim mon3 As New RichTextBox
Dim mon4 As New RichTextBox
Dim mon5 As New RichTextBox

It didn't actually put any text into the RichTextBoxes on my form and the subroutine did not throw back an error. I tried changing it from Dim to Public and it does the same thing.

Can anyone shed some light on this? Thanks.

A: 

You simply need to reference it, in your module, as Form1.RichTextBox1 (obviously replace those placeholders with the actual name properties of your form and richtextbox controls).

This is assuming you are referring to VB.NET.

Declaring a new RichTextBox control in your module is not going help you reference those you have already placed on your form with the Visual Studio editor or declared in the code of your form.

Is there any specific reason for this?

The reason the subroutine doesn't work in your module is simply that the RichTextBox variables are not visible in that scope.

Hi, thanks for the reply. How would I go about this? I've tried Dim `mon1 As New frmmain.RichTextBox` and a few alternatives. And yeah this is for VB.NET. Thanks.
Joseph
1. Remove all of the "Dim mon1 as new..." that you put in your module.
2. In the subroutine, where you see a richtextbox variable name, replace it with "frmMain.RichTextBox" where frmMain is the name of the form you have made, and RichTextBox is the name of the RichTextBox on the form.
It would appear that you would replace the "mon1" etc.. variable in the subroutine with "frmMain.mon1" etc..