Would someone please help explain what this simple code does...

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
918
Office Version
  1. 365
Platform
  1. Windows
BOOKTABLE is a Userform that has 66 labels with label names Genesis to Revelation.
A user clicks on any Label. The label name is immediately inserted into Textbox1.
Code:
Textbox1.value = Daniel
The click event then displays BOOKCHAPTER userform in front. BOOKCHAPTER has 80 labels with their text names as numbers (one = 1, etc)
The user clicks any number. The code displays 1 space followed by the label number. Textbox1 now displays
Code:
Textbox1.value = Daniel 7:
The BOOKCHAPTER click event displays the CHAPTERVERSE userform in front of the other two. This form has 100 labels with label text names and numbers as the entry
(seven = 7)
Textbox1 now finally displays
Code:
Textbox1.value = Textbox1.value + BOOKCHAPTER.value = "Daniel 7:20" (of course w/out quotes)
That is it.  My limited experience has me hardcoding every single label item on every userform to give the correct result  300 times and about 8 hours of work
There must be an easier way to construct code for a generic form that would easily do this.
A fellow member very graciously helped me with this code:

[code]
'Add a class module called LabelClicker:
Public tb As msforms.TextBox  - tb is a special forms variable?
Public WithEvents lb As msforms.Label lb the same?

Private Sub lb_Click()
   tb.Text = lb.Name - what does this do ? 
End Sub

then in your userform you need:
VBA Code:
Dim colHandler As Collection  - if colHandler is a variable , what is a Collection?  
Private Sub UserForm_Initialize()
Dim ctl As msforms.Control  - assigning a variable to a control type?
Dim oHandler As LabelClicker
Set colHandler = New Collection
For Each ctl In Me.Controls
If TypeName(ctl) = "Label" Then
Set oHandler = New LabelClicker
Set oHandler.lb = ctl
Set oHandler.tb = Me.TextBox1 '

The bottom line is, if I want to use this code for any new textboxes, can I just copy it?
The question is, where do the changes in this code need to be made to work for any userform ?
I don't really understand how this ocde works, but it does work very well. Problem is, I need to do this for the two other userforms
BOOKCHAPTER with number names as text = one - "1"(entered number), two = "2", etc.
as with
CHAPTERVERSE, the end result being in te xtbox1 any combinaton of these

Code:
Textbox1.value = "Daniel 7:20"  or "Genesis 12:3", etc.

Can anyone please help me with this.  I want to learn what each  code line does, not just copy code.
(Feel a bit guilty for having to rely on the Message Board experts for what seemingly is very simple - if you know the correct code)
Pictured below is everything I've explained above>

Thanks, cr
 

Attachments

  • SEQUENCE OF FORMS OPENING AS SOON AS PREVIOUS LABEL IS CLICKED.png
    SEQUENCE OF FORMS OPENING AS SOON AS PREVIOUS LABEL IS CLICKED.png
    140.8 KB · Views: 13

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I don't know if you're willing to do this some other way but it seems to me that cascading combos would have been a lot smarter/efficient. You'd pick "Daniel" from the first one. Second combo would present only the chapters (or whatever is a child of the parent combo) that are applicable. Third, only the verses that pertain to the first two. Then you probably don't even need the textbox and certainly don't need multiple forms to present the options. To layout all the data as objects is just weird to me but maybe there was a valid reason to do so. Can't imagine what that would be though. Perhaps research "Excel vba cascading combos" and see what you think.
 
Upvote 0
Hi Micron - thx for replying - yeah - I did it this way with much pain only for visual effects - it looks "cool" to me. However
your idea of one combo acting on another then acting on a third is interesting and I'm going to try that as soon as I post this. The traditional
way most apps I've seen do this is with a tree structure shown below - one click enters the Book, the second the chapter, the third
the verse until the textbox is completed correctly.

Thx again for replying.
cr
 

Attachments

  • TREE STRUCTURE.png
    TREE STRUCTURE.png
    66.1 KB · Views: 7
Upvote 0
Quite easy to do in Access. Lotsa google results for Excel but can't say I looked them over.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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