How to reference textboxes on a form using textbox datatype??

wanye
10-21-2004, 05:03 AM
Hi all, how do i actually reference textboxes on a form using textbox datatype? I can do it with controls collections but is there any 'textbox' collections?

Eg.

dim tb as textbox

set tb = me.text0 ?? ===> reference only textbox "text0"

how to reference all the textboxes using textbox datatype using access 2002?

Dennis DVR
10-21-2004, 05:59 AM
you need to declare an array variable and loop through the form control and check for textboxes.

i.e.

Dim tb() As TextBox
Dim ct As Control
Dim i As Integer
For Each ct In Me.Controls
If TypeOf ct Is TextBox Then
ReDim Preserve tb(i)
Set tb(i) = ct
i = i + 1
End If
Next

wanye
10-21-2004, 09:33 AM
you need to declare an array variable and loop through the form control and check for textboxes.

i.e.

Dim tb() As TextBox
Dim ct As Control
Dim i As Integer
For Each ct In Me.Controls
If TypeOf ct Is TextBox Then
ReDim Preserve tb(i)
Set tb(i) = ct
i = i + 1
End If
Next


But it prompts an error "subscript out of range"??

MKoslof
10-21-2004, 07:18 PM
Or, just create a control array of text boxes :). Then, once you create your control array of text boxes you can just loop from lowerbound to upperbound, doing what you need. If you are new to control arrays, a quick first run is to create a text box on your form, then copy it and paste it on the same form...VB will ask you if you want to start a control array.

Dennis DVR
10-21-2004, 09:04 PM
But it prompts an error "subscript out of range"??

can we see your current code? because the code i've posted won't give you that error, but I just notice that you've mentioned Access 2000, are you running this within Access Application (VBA) or from VB because it woudn't work under Access Application you need to change the Me to Screen.ActiveForm.

could you try?

Dim tb() As TextBox
Dim ct As Control
Dim i As Integer
For Each ct In Screen.ActiveForm.Controls
If TypeOf ct Is TextBox Then
ReDim Preserve tb(i)
Set tb(i) = ct
i = i + 1
End If
Next

now to check the value of tb

For i = 0 To UBound(tb)
MsgBox tb(i).Name
Next

wanye
10-22-2004, 10:05 AM
Or, just create a control array of text boxes :). Then, once you create your control array of text boxes you can just loop from lowerbound to upperbound, doing what you need. If you are new to control arrays, a quick first run is to create a text box on your form, then copy it and paste it on the same form...VB will ask you if you want to start a control array.

I am putting the code in VBA, can show me an example of it? Btw, does control arrays works in .NET ??

Dennis DVR
10-22-2004, 10:32 AM
I am putting the code in VBA, can show me an example of it?

I don't think Access VBA supports control array (I could be wrong :) )


does control arrays works in .NET ??

VB.NET does support control array just look at the code library of VB.NET Forum, IcePlug made a sample program on how to create a control array in VB.NET

MKoslof
10-22-2004, 05:20 PM
Oh, so you are using VBA :)........

There are no control arrays in VBA. You can mimick the functionality by creating text boxes with standardized names (such as text1, text2, etc.) and loop until the highest number created, setting each text box to the focus and doing what you want.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum