I need a concise way to write code for similar scenarios with multiple textboxes in Userform

dellehurley

Board Regular
Joined
Sep 26, 2009
Messages
171
Office Version
  1. 365
Platform
  1. Windows
Hi
I need a shortcut as the code I am writing is very long and bulky. I'm sure there is a shortcut but I cannot find the answer online.
I have a database with a userform, a number is places in the box circled in red, this represents the number of entries there are for this particular file. I want to close the fields which do not apply.
Currently I write the code like this
VBA Code:
If Me.txtRecs.Value = 1 Then
        Me.txtRIN1.Enabled = True
        Me.txtSurname1.Enabled = True
        Me.txtFirstName1.Enabled = True
        Me.txtYOB1.Enabled = True
        Me.cmdAdd1.Enabled = True
        Me.txt1.Value = "a"
        Me.txtRIN2.Enabled = False
        Me.txtSurname2.Enabled = False
        Me.txtFirstName2.Enabled = False
        Me.txtYOB2.Enabled = False
        Me.txtRIN3.Enabled = False
        Me.txtSurname3.Enabled = False
        Me.txtFirstName3.Enabled = False
        Me.txtYOB3.Enabled = False
        Me.txtRIN4.Enabled = False
        Me.txtSurname4.Enabled = False
        Me.txtFirstName4.Enabled = False
        Me.txtYOB4.Enabled = False 'etc

       If Me.txrRecs.Value=2 then  'etc
this continues until I have up to each set of boxes listed then I move on to if Me.texRecs.Value =2
I know there must be a shortcut so I don't have to write out each line for each scenario.
Any advice?
Thanks
Dannielle
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    112.7 KB · Views: 9
Hi - it's good to hear from you - I do actually remember helping out - I even had a flashback when I saw the notification! Those were the days... back when I was a young whippersnapper! ;)

Anyway, my personal rule in life is that: if it's working, back away from it slowly so as not to disrupt anything, and then when you're a good distance clear of it, turn around and run like a bat-outta-hell and hopefully, if something goes wrong, it'll be someone else's problem.

Be that as it may, is there anything you'd like help with? Looking at the code you opted to go with, I note that the controls starting txt will never actually be visible. Is that the intention, or am I misreading it? I see that you've devised a number of logic puzzles to keep me on my toes - nice try, but I'm as dense as ever. I tend to find when it comes to trying to divine WTH VBA is doing with logic gates like these is to simply Debug.Print out the results for each loop. That way I can keep track of what's going on, and see if my logic and VBA's logic are in accord (it rarely is).

So in your initial code above, I would probably write something like:

VBA Code:
                If LCase(ctrl.Name) = LCase(ControlArray(counter) & TargetNumber) Then
                    Debug.Print counter, " If [" & LCase(ctrl.Name) & "] = [" & LCase(ControlArray(counter) & TargetNumber) "] Then = TRUE"
                    ctrl.Enabled = True
                    ctrl.Visible = True            
                ElseIf LCase(ctrl.Name) Like LCase(ControlArray(counter) & "?") Then
                     Debug.Print counter, "ElseIf [" & LCase(ctrl.Name) & "] Like [" & LCase(ControlArray(counter) & "?") & "] Then = TRUE"

This way I can see what variable values and logic permutations triggered a TRUE result, and shows me were in the loop it happened. Does that make sense? Give me a shout if there is anything I can do to help.
Oh yes we have aged over the past months, it must be this pandemic, but I assure you I look as youthful as ever ;) (the lies we can tell when no photos are involved).
As for the txt always being hidden yes that is correct a fact I lost sight of in all my mucking about. When I got it to work I realised and hid it again.
Anyway I will definitely give the above a go. I have seen people use the debug print function on YouTube but have never used it myself.

Here is another quick question. Is it possible write one code like below BUT for it to cover all items named something like this eg cmbFullName1, cmbFullName2, cmbFullName3 etc?
VBA Code:
Private Sub cmbFullName1_Enter()
Me.cmbFullName1.BackColor = RGB(255, 255, 204)
End Sub
Thanks
Dannielle
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,215,635
Messages
6,125,940
Members
449,275
Latest member
jacob_mcbride

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top