0

I have a userform which contains a number of checkboxes from 1 to 100. I have written some very simple code so that when you submit the form it creates a binary string that represents the state of those 100 checkboxes, where 0 is false and 1 is true. The code to do this is here:

Private Sub BusRulesSubmit_Click()
 Dim myBinaryString As String
 Dim nm As String
 Dim c As Control

 For busRuleIdx = 1 To 100
  nm = "CheckBox" & busRuleIdx
  Set c = Controls(nm)
  If c.Value = True Then
   myBinaryString = myBinaryString & "1"
  Else
   myBinaryString = myBinaryString & "0"
  End If
 Next

 msgBox myBinaryString
End Sub

I now want to open this Userform from another form, where I have a similar binary string, and use this string to set the values of the checkboxes to true or false as appropariate. However I am having issues when setting my control. The code is here:

Private Sub populateBusRules()
 Dim c As Control
 Dim myBRBinary As String
 myBRBinary =    "000000000011100000000000000000000000000000000000000000000000000000000010000000000000000000000000000"

 For busRuleIdx = 1 To 100
  nm = "BusinessRules.CheckBox" & busRuleIdx
  Set c = Controls(nm)
  If Mid(myBRBinary, buRuleIdx - 1, 1) = 1 Then
   c.Value = True
  Else
   c.Value = False
  End If
 Next
End Sub

When I run the above, I get a runtime error "Could not find the specified object" and when going to debug it highlights this problem where the code states "Set c = Controls(nm)" - and I can see that it is failing in the first round of the loop i.e. where nm = "BusinessRules.CheckBox1"

Interestingly if I run the code "Set c = Controls(BusinessRules.CheckBox1)" I get no such issue.

Any help would be much appreciated.

Thanks,

Paul.

4
  • Hi, how proficient are you in VBA? Does the original form stay open? Commented Jul 6, 2016 at 10:51
  • You will need global variables if the forms are not opened at the same time. Commented Jul 6, 2016 at 10:59
  • Yes The forms are open at the same time - though I use hide commands Commented Jul 6, 2016 at 11:19
  • Can you reference form to form frm2.control.value=frm1.control.value? Commented Jul 6, 2016 at 11:21

1 Answer 1

1

I think the BusinessRules is giving you the issue. In the Controls collection, there is no Control named "BusinessRules.CheckBox1", only one named "CheckBox1" within the BusinessRules.Controls collection. Assuming there aren't other issues mentioned in the comments above (like the form being closed before this is called), then this should fix your issue

Sign up to request clarification or add additional context in comments.

1 Comment

Great that makes perfect sense. So this gets resolved by using "Set c = BusinessRules.Controls(nm)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.