Create combo box list of blocks in a drawing which begin with "type1"

Create combo box list of blocks in a drawing which begin with "type1"

Anonymous
Not applicable
2,964 Views
16 Replies
Message 1 of 17

Create combo box list of blocks in a drawing which begin with "type1"

Anonymous
Not applicable

I would like to get a code taht creates a combo box list of all the blocks in a drawings whihc begin with "type1" in the name.

 

This is code I have at the moment whihc i thought would create a combo box off all the blocks, but it does not populate the box for some reason.

 

Private Sub ComboBox2_Change()
Dim oBlocks As AcadBlocks
For Each oBlocks In ThisDrawing.Blocks
ComboBox2.ListIndex = 0
End Sub

 

I need help fixing my current code and then altering to search for blocks which begin with "type1"

 

Thanks,

 

Sandy

0 Likes
Accepted solutions (1)
2,965 Views
16 Replies
Replies (16)
Message 2 of 17

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

not tested ...

 

Dim oBlock As AcadBlock
For Each oBlock In ThisDrawing.Blocks
   If ucase(oBlock.Name) like "TYPE1*" then
      Call ComboBox2.AddItem(oBlock.Name)
   End If
Next
If ComboBox2.ListCount > 0 then
   ComboBox2.ListIndex = 0
End If

Just for info for your next questions, there is a separate forum for AutoCAD & VBA >>>click<<<

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 17

Anonymous
Not applicable

Hi Alfred,

 

Thanks for your help, I will post my next question on the correct forum.

 

I have inserted your code into my form and it works great - thanks!

 

I would like to modify the code so that the list in the combo box depends on the selection of a previous combo box.

 

I.e, If the value of combo1 is "TYPE1" then combo box 2 lists all the blocks with the names containing "TYPE1"

 

This is where I am -  I can select the value of the first combo box but then it fails to populate the second combo box.

 

This is my code so far;

 

 

 

Private Sub UserForm_Initialize()

ComboBox3.AddItem ("RISERS")
ComboBox3.AddItem ("BOP'S")
ComboBox3.AddItem ("VALVES")


If ComboBox3.Value = "RISERS" Then

Dim oBlock As AcadBlock
For Each oBlock In ThisDrawing.Blocks
   If UCase(oBlock.Name) Like "Riser*" Then
      Call ComboBox2.AddItem(oBlock.Name)
   End If
Next
If ComboBox2.ListCount > 0 Then
   ComboBox2.ListIndex = 0
End If


End If
End Sub

 

 

 

Any help would be much appreciated,

 

Thanks,

 

Sandy,

0 Likes
Message 4 of 17

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

Dim oBlock As AcadBlock
For Each oBlock In ThisDrawing.Blocks
   If ucase(oBlock.Name) like (combobox3 & "*") then
      Call ComboBox2.AddItem(oBlock.Name)
   End If
Next
If ComboBox2.ListCount > 0 then
   ComboBox2.ListIndex = 0
End If

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 5 of 17

Anonymous
Not applicable

Not working properly for some reason... :-s

 

When I run the code below, I can select an option for combo box3 but combox 2 remains blank. If i remove "Combobox3.listIndex = 0" then when I run the code, combobox2 just lists all the blocks in the dwg file before i have selected an option for combobox3.

 


Private Sub UserForm_Initialize()

ComboBox3.AddItem ("Select Catagory")
ComboBox3.AddItem ("Risers")
ComboBox3.AddItem ("B.O.P.'S")
ComboBox3.AddItem ("Valves")
ComboBox3.ListIndex = 0

Dim oBlock As AcadBlock
For Each oBlock In ThisDrawing.Blocks
   If UCase(oBlock.Name) Like (ComboBox3 & "*") Then
      Call ComboBox2.AddItem(oBlock.Name)
   End If
Next
If ComboBox2.ListCount > 0 Then
   ComboBox2.ListIndex = 0
End If



End Sub

 

 

These are the Names of the blocks i have in the dwg file so it should detect these names: "Risers - 1","Risers - 2", "B.O.P.'s - 1", "B.O.P.'s - 2"

 

 

 

 

I have attached a snap of my form for clarity,

 

Thanks,

 

SandyCapture.JPG

0 Likes
Message 6 of 17

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> When I run the code below, I can select an option for combo box3 but combox 2 remains blank

Well, in ComboBox3 your screenshot shows "Select Category" ... that means that ComboBox2 only lists layers with names starting with "Select Category" ... do you have such layers?

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 7 of 17

Anonymous
Not applicable

No Alfred, there are no blocks that contain "Select Catagory" this was just the first option I put into the list to prompt the user. (Perhaps there is a better way to do this)

 

 

I understand that with no blocks starting with "Select Catagory" in the name, the following combo box will remain blank. However when I change the selection from "Select Catagory" to "Risers" say, the following combobox still remains blank even thought I have multiple blocks which contain "Risers" in the name.

 

Does the form need to refresh/regen when a new option is selected in the first combobox? If so, are you able to help me achieve this?

 

Thanks again for all your assistance.

 

Sandy

0 Likes
Message 8 of 17

h_s_walker
Mentor
Mentor

@Anonymous

 

@Alfred.NESWADBA Just forgot to add the qualifier after combobox3 It should be combobox3.text &"*"

 

Hopefully it should work now.

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 9 of 17

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> Just forgot to add the qualifier after combobox3 It should be combobox3.text

In VBA the .TEXT property is the default one, so it's returning the text-content as default.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 10 of 17

Anonymous
Not applicable

My "select block" combobox still remains blank for some reason. It will not show the list of blocks in the drawing starting with the selection in the first combobox i.e "Risers", "B.O.P's" or "Valves"

 

My complete Code & Form:

 

Private Sub INSERTBLOCK_Click()
 ' Define the block
 Dim blockObj As AcadBlock
 Dim insertionPnt(0 To 2) As Double
 insertionPnt(0) = 0
 insertionPnt(1) = 0
 insertionPnt(2) = 0
 Set blockObj = ThisDrawing.Blocks.Add _
 (insertionPnt, "ComboBox2.Name")


 ' Insert the block
 Dim blockRefObj As AcadBlockReference
 insertionPnt(0) = 0
 insertionPnt(1) = 0
 insertionPnt(2) = 0
 Set blockRefObj = ThisDrawing.ModelSpace.INSERTBLOCK _
 (insertionPnt, "ComboBox2.Name", 1#, 1#, 1#, 0)
 'ZoomAll
 MsgBox "Block Inserted" '& blockRefObj.ObjectName
 ThisDrawing.Regen acActiveViewport



End Sub

Private Sub REFRESHFORM_Click()
ComboBox3.ListIndex = 0

End Sub

Private Sub UserForm_Initialize()

ComboBox3.AddItem ("Select Catagory")
ComboBox3.AddItem ("Risers")
ComboBox3.AddItem ("B.O.P.'S")
ComboBox3.AddItem ("Valves")
ComboBox3.ListIndex = 0




Dim oBlock As AcadBlock
For Each oBlock In ThisDrawing.Blocks
   If UCase(oBlock.Name) Like (ComboBox3.Text & "*") Then
      Call ComboBox2.AddItem(oBlock.Name)
   End If
Next
If ComboBox2.ListCount > 0 Then
   ComboBox2.ListIndex = 0
End If

End Sub

 

 

 

Capture.JPG

 

 

Anything you guys can see that I am doing wrong?

 

Block Names that exist in the drawing;

 

"Risers -1"

"Risers -2"

"B.O.P's -1"

"B.O.P's -2"

 

Thanks,

 

Sandy

 

P.s. - please ignore the "Catagory" Typo!

0 Likes
Message 11 of 17

h_s_walker
Mentor
Mentor

You do not use Call Combobox2.Additem. It's just Combobox2.Additem

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 12 of 17

Anonymous
Not applicable

I have removed the "call" but unfortunately that has not solved the problem.

 

The "block" combobox is still blank no matter what i select in the "category" combobox.

 

The only way I can get the "block" combo box to list anything is if:

 

I remove "ComboBox3.AddItem ("Select Catagory")" and "ComboBox3.ListIndex = 0"

 

or change   

 

"If UCase(oBlock.Name) Like (ComboBox3.Text & "*") Then" to "   If UCase(oBlock.Name) Like (ComboBox2.Text & "*") Then"

 

even then, it just lists all the blocks in the drawing file and does not filter as I want with the category combobox selection.

 

Sandy

 

0 Likes
Message 13 of 17

h_s_walker
Mentor
Mentor
Accepted solution

Here is a quick crude program put together in VB. Note to change the numbers in the combo2 box. You need to use the CLICK value for Combo1

Private Sub Form_Load()
Combo1.AddItem "1"
Combo1.AddItem "2"
Combo1.AddItem "3"
Combo1.AddItem "4"
Combo1.ListIndex = 0
End Sub


Private Sub Combo1_Click()
If Combo1.Text = "1" Then
 Combo2.Clear
 Combo2.AddItem "1"
 Combo2.AddItem "2"
 Combo2.AddItem "3"
 Combo2.AddItem "4"
 Combo2.ListIndex = 0
ElseIf Combo1.Text = "2" Then
 Combo2.Clear
 Combo2.AddItem "2"
 Combo2.AddItem "4"
 Combo2.AddItem "6"
 Combo2.AddItem "8"
 Combo2.ListIndex = 0
ElseIf Combo1.Text = "3" Then
 Combo2.Clear
 Combo2.AddItem "3"
 Combo2.AddItem "6"
 Combo2.AddItem "9"
 Combo2.AddItem "12"
 Combo2.ListIndex = 0
ElseIf Combo1.Text = "4" Then
 Combo2.Clear
 Combo2.AddItem "4"
 Combo2.AddItem "8"
 Combo2.AddItem "12"
 Combo2.AddItem "16"
 Combo2.ListIndex = 0
End If
End Sub

 

 

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 14 of 17

Anonymous
Not applicable

Thanks, That works fine in VBA,

 

I will try and incorporate into my original form,

 

Thanks,

 

Sandy

0 Likes
Message 15 of 17

Anonymous
Not applicable

hwalker,

 

This is not exactly a solution to my original query but I have managed a pretty good work around with the code you wrote above.

 

Basically, instead of the VBA searching for blocks that start with certain characters and inserting, the VBA is just selecting text which matches the names of blocks that are in the drawing so they still insert.

 

Sandy

0 Likes
Message 16 of 17

h_s_walker
Mentor
Mentor

Glad I managed to help

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 17 of 17

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> You do not use Call Combobox2.Additem. It's just Combobox2.Additem

Jut one additional info about "Call" or "not Call" within VBA:

You can use "Call", but then the parameters have to be embedded in brackets, without "Call" the parameters can't be used within brackets.

 

So both of that lines are valid

Call ComboBox2.AddItem("AAA")
ComboBox2.AddItem "AAA"

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes