Search code examples
vbadatabasems-accessvariablesms-access-forms

Append Strings and values together to target a form control in VBA


I'm so close to getting this code working, I just need a little push please. I would like to take the name of a combo box and then add a string to the end, But then get the value of a textbox with that string. This is to create a dynamic function instead of pasting the same code over and over.

Here's what I have so far, after you select something in the dropdown, the data is then pulled to populate the boxes next to it. I have about 8 drop downs so far so that's why I need this to work.

'Combobox after update
Call GrabData(Me, Me.ActiveControl)

Then

Private Sub GrabData(ctl As Control)

  'name of ctl/combobox is "Kitchen"
  data1 = (ctl.Name & "Size") '"KitchenSize"

  'Here is where it all goes wrong 
   data1.Value = size.value 

  'size.value is just a textbox for example
    End Sub

I can debug this with:

msgbox(data1) 
   'outputs "KitchenSize"

But I cannot get the value of kitchensize's textbox with data1.value Error: Object Required

I have also added Dim As String / Dim As Control.

I will be assigning the variable to some other stuff in this 50 line code I wrote so please don't take the above example as exactly what I intend to do, I just need help appending the ctl.name to a string, then use that to reference another control and so on.


EDIT


For anyone who wants to know, I figured it out.

Dim Ctrl As Control
Dim CtrlName As String

CtrlName = ctl.Name & "Size"
Set Ctrl = Me.Controls(CtrlName)
Ctrl.Value = 'Wherever you want to send the values to 

Solution

  • See the edit.

    You need to dim it as a string, then use Set Ctrl