views:

18

answers:

2

Forgive me about the title, I have no idea what this is called.

I have a MS Access database set up, with a Period field that has either values 1, 2, 3, 4 or 5. I retrieve these values using a database connection and I would like to reference a particular control based on what period was grabbed from the database.

Here's example code, pseudo of course.

TextBox(dr(3)).Text = dr(0)

dr(3) contains the period, and dr(0) contains the content I would like to put into the text box. I have these text boxes on my form: TextBox1, TextBox2, TextBox3, TextBox4 and TextBox5.

So if dr(3) contained 2 then I would want to reference TextBox2.

I hope I've made myself clear, any help would be greatly appreciated, thanks. :)

A: 

I'm assuming your textboxes are on the same control (ie-they have the same parent object). You could use:

Dim txtbox as TextBox = CType(parentControl.FindControl("TextBox" & dr(3).ToString()), TextBox)
txtBox.Text = dr(0)

Edit: For groupbox

For each ctrl as Control in myGB.Controls
    If ctrl.ID = "TextBox" & dr(3).ToString() Then
        CType(ctrl, TextBox).Text = dr(0)
        'EDIT: add exit to stop iteration once the textbox is found
        Exit For  
    End If
Next 
Joel Etherton
They're all grouped in a GroupBox yeah, I'll give this a go now and get back to you :D
James
After using this code: `CType(Main.gridHolder.FindControl`, I get the erorr: `'FindControl' is not a member of 'System.Windows.Forms.GroupBox'.` is it okay to just try and find it through the entire form? It is unlikely that there will be any more textboxes with the same kind of name.
James
Check out the edit.
Joel Etherton
Worked, thanks.
James
A: 

I'm a bit rusty in VBA but you probably want to use a control array. When a control is set up as a control array you can index that array using dr(3).

Peter Lillevold