Object Variable Not Set Error

Object Variable Not Set Error

Anonymous
Not applicable
1,869 Views
26 Replies
Message 1 of 27

Object Variable Not Set Error

Anonymous
Not applicable
I'm making a VBA to make it easier and more user friendly to edit the attributes in our titleblock. I made a form with a bunch of textboxes and when it loads up, it fills in the textboxes with the attributes.

The program runs ok, but after it terminates, it gives the error message:

Run-time error '91':
Object variable or With block variable not set.

I'm not using a With block, so I assume I'm not setting something right.

Any ideas?



Private Sub UserForm_Initialize()

Dim oblkRef As AcadBlockReference
Dim objFnd As AcadEntity
Dim atts As Variant

For Each objFnd In ThisDrawing.PaperSpace

If TypeOf objFnd Is AcadBlockReference Then
Set oblkRef = objFnd
If StrComp(UCase(oblkRef.Name), "TITLEBLOCK", 1) = 0 Then
Exit For
End If
End If
Next objFnd

atts = oblkRef.GetAttributes
TextBox0.Text = atts(0).TextString
TextBox1.Text = atts(1).TextString
TextBox2.Text = atts(2).TextString

Me.Show

End Sub

I'm sure it's something "basic" (sorry) but I'm new to VBA.
Thanks.
0 Likes
1,870 Views
26 Replies
Replies (26)
Message 2 of 27

Anonymous
Not applicable
Hi Stevie
Try this instead
See comments inside the code

~'J'~
0 Likes
Message 3 of 27

Anonymous
Not applicable
I don't understand it, but it works.

I went thru and rearranged my code to match yours, but still got the error. So I just wiped mine out and pasted yours in and added some more stuff back in, and whatever it was choking on was gone.

Thanks again, Fatty.

Hey, can I make a control array of textboxes in vba?
0 Likes
Message 4 of 27

Anonymous
Not applicable
Yes, you can
Let me the time I will find an example
in my code library 🙂

~'J'~
0 Likes
Message 5 of 27

Anonymous
Not applicable
This will get you start on this way
I hope

~'J'~
0 Likes
Message 6 of 27

Anonymous
Not applicable
That's pretty cool, but can they be set up in design, so that there is an array of textboxes like text(0), text(1), etc, so that they could be cycled with a For/Next loop?

Normally in VB, when I copy a textbox, then paste it, it asks me if I want a control array. Normally I don't, but in some cases I do, and I'm not getting that option in VBA. Also, in VB, textboxes have an Index property, but I didn't see it in VBA. It would be helpful for pulling attributes, which is an array, into an array of textboxes. That kind of stuff.
0 Likes
Message 7 of 27

Anonymous
Not applicable
Hi Stevie
Sorry I can't to explain you good
Check out this thread I hope
it make sence:

http://vbaexpress.com/forum/showthread.php?t=13992&highlight=adding+controls

~'J'~
0 Likes
Message 8 of 27

Anonymous
Not applicable
unfortunately vba does not support control arrays
that doesn't prevent you from creating an array of control objects however.
hth
Mark

wrote in message news:5742116@discussion.autodesk.com...
That's pretty cool, but can they be set up in design, so that there is an
array of textboxes like text(0), text(1), etc, so that they could be cycled
with a For/Next loop?

Normally in VB, when I copy a textbox, then paste it, it asks me if I want a
control array. Normally I don't, but in some cases I do, and I'm not
getting that option in VBA. Also, in VB, textboxes have an Index property,
but I didn't see it in VBA. It would be helpful for pulling attributes,
which is an array, into an array of textboxes. That kind of stuff.
0 Likes
Message 9 of 27

Anonymous
Not applicable
Okay, now you're just teasing me:)
How would I go about creating an array of textboxes so that I could cycle thru them in a for/next loop?

My last program had 15 Textboxes, and I had to list them individually down the page, setting
Text1.Text = String(1)
Text2.Text = String(2)
Text3.Text = String(3)

If there's a better way, I could really use it.

Thanks.
0 Likes
Message 10 of 27

Anonymous
Not applicable
Maybe use TabIndex?

I'd still need to cycle thru the whole bunch of them each time to match up the tabindex with the increment number.

I'll think about it...
0 Likes
Message 11 of 27

Anonymous
Not applicable
start a new project
add a userform
add a bunch of textboxes(add one, copy, paste paste paste, :-))
paste this into form code window

[code]
Private Sub UserForm_Initialize()
Dim octl As Control, lCount As Long
Dim aTbs() As TextBox
ReDim aTbs(0)

'create array of textboxes
'in practice Redim Preserve is the worst way of creating an array
'this is just to show the concept
':-)
For Each octl In Me.Controls
If TypeOf octl Is TextBox Then

'just an arbitrary identifier to read later
octl.Tag = "Txt_" & CStr(lCount)

ReDim Preserve aTbs(lCount)
Set aTbs(lCount) = octl
End If 'type
lCount = lCount + 1
Next octl

'did it work?
Dim lIdx As Long
For lIdx = 0 To UBound(aTbs)
Debug.Print aTbs(lIdx).Tag
Next lIdx
End Sub
[code]
hth
Mark

wrote in message news:5742590@discussion.autodesk.com...
Maybe use TabIndex?

I'd still need to cycle thru the whole bunch of them each time to match up
the tabindex with the increment number.

I'll think about it...
0 Likes
Message 12 of 27

Anonymous
Not applicable
Hi Stevie
Try this instead hope it will
make a sense

~'J'~
0 Likes
Message 13 of 27

Anonymous
Not applicable
Okay, that gives me some ideas. Thanks.

Is there a way to iterate thru preset controls? I could set the TabIndex, or put a number in the Tag value.

How 'bout this:
Using an Array of Attribute Strings called Att(X)

Private Sub CommandButton1_Click()
Dim ctlObj As Control, tbxObj As Control
Dim indx as Long
For Each ctlObj In Me.Controls
If TypeOf ctlObj Is TextBox Then
Set tbxObj = ctlObj
tbxObj.Text = Att(Val(tbxObj.Tag))
End If
End Sub

Does that make sense? I'm gonna try it...

Thanks for the help, Fatty.
0 Likes
Message 14 of 27

Anonymous
Not applicable
Hi Stevie
I see you're still going right way, glad if so
Sorry I have not have a time to help with it
right now, perhaps I could be do it tomorrow
Just on the quick glance the attribute iteration
is zero-based
May be:
tbxObj.Text = Att(CInt(tbxObj.Tag-1))'<-- where first textbox tag is 1, next 2 etc
Not tested though...

Happy coding,

~'J'~
0 Likes
Message 15 of 27

Anonymous
Not applicable
What I had in mind with the above code is that I know what order the attributes are arranged in, and if I match the textbox's Tag with the attribute that goes in it, I can load em up without having to go one by one, and it shouldn't matter what order the textboxes come up, because the Tag is the important factor.
0 Likes
Message 16 of 27

Anonymous
Not applicable
Stevie,
I think there is no need to watch for
an order of attributes because it
will be the same as the order of textboxes
See quick example

~'J'~
0 Likes
Message 17 of 27

Anonymous
Not applicable
I've created a preset form, which mimics our titleblock, and I wanted to fill those textboxes with info. I didn't want to create them on the fly.

I do have a couple of projects that will require something like this, so this will be going n my ever increasing stack of source code.

I attached a jpg of my form.
0 Likes
Message 18 of 27

Anonymous
Not applicable
Hi again
Let me quess
Is there are all labels on form identical
to tag attributes in your title block?

~'J'~
0 Likes
Message 19 of 27

Anonymous
Not applicable
Yeah, pretty much, though not quite Identical. I just want a way to load up a series of info into a series of textboxes without having to create the textboxes at runtime.

I haven't gotten to test my code yet, but I'm hoping I can use the Tag value to set up an index reference.
0 Likes
Message 20 of 27

Anonymous
Not applicable
Ugh, no warranty but I'll try
to invent something 🙂
Say, how about to enumerate textboxes
by tags relatively to its very own label tag?
Then iterate through all of the controls...
Just an idea at the moment...
See ya

~'J'~ Spell checking
Message was edited by: Fatty
0 Likes