Search code examples
ms-accessif-statementtextboxvba

Loop through textboxes on access form and change the data


I am trying to change data when an access form opens. At the minute data says True and I would like to change that to say Yes instead. I have given it a go but I am new to vba and don't really know what I am doing. I have given all the textboxes the same Tag of 'QtTxtBox' hoping this would help but it hasn't. Below is what I've got so far, can anyone help me?

      Dim ctlVar As Control
      For Each ctlVar In Me.Controls
        If ctlVar.ControlType = acTextBox Then
            If acTextBox.text = "True" Then
               acTextBox.text = "yes"
            End If
      End If

Solution

  • Your problem is in your textbox reference. acTextBox is not a textbox. It's just a value showing that a control type is a textbox.
    So, when you say 'If ctlVar.ControlType = acTextBox Then', that is correct.
    But when you say 'If acTextBox.text = "True" Then', that is incorrect. You're not referencing the control any longer. It should be 'If ctlVar.text = "True" Then'. ctlVar is your reference to the control.

    Also, you need to set the focus onto the textbox before changing the value. Here's what your code should look like:

    For Each ctlVar In Me.Controls
      If ctlVar.ControlType = acTextBox Then
          If ctlVar.Value = "True" Then
             ctlVar.Value = "yes"
          End If
    End If