| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help with inclusion of variable into a field name in code
This is example code that I can not get to work that I have in a Access Form. There are several
text boxes on the form that that are named sequentially that I would like to have evaluated. (ie txtN1, txtN2 ...ect) Any suggestions would be greatly appreciated Greg Private Sub buttCode_Click() Dim varCount Select Case cbTest Case True varCount = 1 While varCount < 6 [txtN & varCount &].Value = varCount ' can not get this to work correctly varCount = varCount + 1 Wend Case False varCount = 1 While varCount < 6 [txtN & varCount &].Value = varCount * 2 ' can not get this to work correctly varCount = varCount + 1 Wend End Select End Sub |
|
#2
|
|||
|
|||
|
Hi,
Breakdown of your code:
Code:
Private Sub buttCode_Click() Dim lCounter As Long Select Case cbTest Case True For lCounter = txtValue.LBound To txtValue.UBound txtValue(lCounter).Text = lCounter Next Case False For lCounter = txtValue.LBound To txtValue.UBound txtValue(lCounter).Text = lCounter Next End Select End Sub To make this work, all you need to do is name all your textboxes the same (txtValue in this code) and make it a control array (should be automatic when you name two controls the same). Then use this code and it should work. Grtz.© M. |
|
#3
|
|||
|
|||
|
Access Form will not all text boxes with the same name
I tried to set up you code within my form and rename the all the text boxes to the same name but Access will not allow multiple textboxes with the same name. Any other thoughts?
Thanks for your comments. Greg |
|
#4
|
|||
|
|||
|
Hi,
I am not very familiar with programming in MS-Access. In fact, I try to avoid it at almost any cost. However, you might be able to use a for each control loop and check the type of the control. Basically this works just like the control array solution I presented in an earlier post. Please consult the MS-Access help on the correct syntax for looping through all the controls, basic idea is this: Code:
Dim ctlCurrentControl as Control For Each ctlCurrentControl in Me.Controls If (Typename(ctlCurrentControl) = "Textbox") Then ' Input relevant value in the .Text property of the textbox. ctlCurrentControl.Text = "Value x" End if Next Note however that I am not sure about the correct syntax when it comes to MS-Access forms and I did not include code to recognize what the current control number is. You can access the name property of the current control. That name you can use to see what control you are currently looking at. The Name property returns a string, thus you can inspect it against a "normal" string that you can construct. Grtz.© M. |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > Help with inclusion of variable into a field name in code |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|