Quantcast
Channel: Indirect Referencing in VB
Viewing all articles
Browse latest Browse all 5

Indirect Referencing in VB

$
0
0
Hi All!
  I am new to VB so please bear with me if the solution is obvious to you all as it is not to me!

Basically, is there a way of perfoming an indirect reference to a variable, or building up a variable name as the program runs?

To explain what I mean, I'll go through an example of the kind of thing I am trying to achieve:

I have painted two grids on a form which are both 9 by 9 and made up of text boxes. Users are able to enter numeric values into each of the positions in the grids.

I have named each of the text boxes in the format txtGridXColumnYRowZ, so for the top left position in the first grid, the text box has the name txtGrid1Column1Row1 and for the bottom right the text box takes the name txtGrid1Column9Row9. For values in the second grid the value X is substituted by 2. Hence the bottom right position in grid 2 is named txtGrid2Column9Row9.

After the users have entered some values, one of the things that I need to do is to clear down the grids if the user clicks on a reset button.

I have started off by writing a nested loop as below.

  Dim varGrid As Integer
  Dim varCol As Integer
  Dim varRow As Integer
 
  ' Clear down grids before new entry

  ' Loop through grid 1 fields followed by grid 2
  For varGrid = 1 To 2
    ' Loop through columns
    For varCol = 1 To 9
      ' Loop through Rows
      For varRow = 1 To 9
     
        ' Reset current position in grid to a blank value
        ' Each cell name is in the form txtGridXColumnYRowZ
        ' e.g. txtGrid1Column1Row1 for grid 1, column1, row 1

       ' What needs to go here????!
 
    Next varCol
  Next varGrid


But as you can see, I don't know how to dynamically set the variable name. In other languages I have used features such as indirect referencing, but having searched through VB books I cannot find any mention of how to implement it. I'm sure there is an easy solution as I shudder to think I have to reset each cell individually (txtGrid1Column1Row1.text="" ....etc)!

I also need to process ranges within the grids for other functions which cannot be hard coded so being able to substitute variable names at runtime is absolutely essential. (e.g. all cells in grid 2 for row 3).

Thanks in advance!
  GuinnessHead

Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images