开发者

How to reference a control from a module?

开发者 https://www.devze.com 2022-12-19 03:42 出处:网络
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 databas

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开发者_StackOverflow, thanks. :)


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 


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号