Insert block Error "Key not found"

Insert block Error "Key not found"

Anonymous
Not applicable
1,535 Views
11 Replies
Message 1 of 12

Insert block Error "Key not found"

Anonymous
Not applicable
ACAD 2000i
I am trying to insert a block using this code:

'OLE Automation Object Calls
Set AcadObj = GetObject(, "AutoCAD.Application")
Set ThisDrawing = AcadObj.ActiveDocument
Dim insertionPnt(0 To 2) As Double
Dim blockObj As AcadBlock

' specify insertion points
insertionPnt(0) = 0
insertionPnt(1) = 0
insertionPnt(2) = 0

Dim docName, JobBlockName As String
docName = ThisDrawing.FullName
JobBlockName = "C:/Program Files/AutoCAD LT 2000/Sample PointTB.dwg"

Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt, JobBlockName, 1#, 1#, 1#, 0)

' Get the attributes for the block reference
Dim varAttributes As Variant
varAttributes = blockRefObj.GetAttributes

' Change the value of the attribute
' Note: There is no SetAttributes. Once you have the variant array, you have the objects.
' Changing them changes the objects in the drawing.
varAttributes(0).TextString = Me![ChainOfCustody Details subform].Form.SampleNum

Set AcadObj = Nothing
---------------
Error is on Set blockRefObj = ThisDrawing.ModelSpace...

Any Ideas
TIA
0 Likes
1,536 Views
11 Replies
Replies (11)
Message 2 of 12

Anonymous
Not applicable
There error is caused by:

JobBlockName = "C:/Program Files/AutoCAD LT 2000/Sample PointTB.dwg"

Folder separator in file path shoud be backslash "\", not "/" (as if in
LISP).


"dbp428" wrote in message news:[email protected]...
ACAD 2000i
I am trying to insert a block using this code:

'OLE Automation Object Calls
Set AcadObj = GetObject(, "AutoCAD.Application")
Set ThisDrawing = AcadObj.ActiveDocument
Dim insertionPnt(0 To 2) As Double
Dim blockObj As AcadBlock

' specify insertion points
insertionPnt(0) = 0
insertionPnt(1) = 0
insertionPnt(2) = 0

Dim docName, JobBlockName As String
docName = ThisDrawing.FullName
JobBlockName = "C:/Program Files/AutoCAD LT 2000/Sample PointTB.dwg"

Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt,
JobBlockName, 1#, 1#, 1#, 0)

' Get the attributes for the block reference
Dim varAttributes As Variant
varAttributes = blockRefObj.GetAttributes

' Change the value of the attribute
' Note: There is no SetAttributes. Once you have the variant array, you
have the objects.
' Changing them changes the objects in the drawing.
varAttributes(0).TextString = Me![ChainOfCustody Details
subform].Form.SampleNum

Set AcadObj = Nothing
---------------
Error is on Set blockRefObj = ThisDrawing.ModelSpace...

Any Ideas
TIA
0 Likes
Message 3 of 12

Anonymous
Not applicable
Oh Yes, I grabbed that path from a menu button I made.
Thank you it works great!!!!
0 Likes
Message 4 of 12

Anonymous
Not applicable
Now how can I pause it so I can pick an insertion point for the block?
0 Likes
Message 5 of 12

Anonymous
Not applicable
You use AcadUtility.GetPoint() to get user input for point:

Dim pt As Variant
pt=ThisDrawing.Utility.GetPoint(, vbCr & "Pick insertion point: ")

Of course you need to handle user input error, for example what if user
press Esc to cancel the pick.

However, your code seems implying that your project is run outside Acad as a
stand-alone app (hence the GetObject() thing). If that is the case, you need
to make sure your code transfer user from current Active Window of your app
to Acad window. Otherwise, user may not see the "Pick insertion point: "
prompt at Acad command line and the process would halt and appear frozen.
After user's pick, you have to bring back the focus to your application.

IMO, if you automate Acad from external application, it is VERY BAD design
that the user needs to switch back and forth between your application and
Acad. If the user need to interract with Acad, then the whole thng should be
done within AutoCAD, which provided rich APIs to let you do that (LISP, VBA,
ObjectARX, .NET API).



"dbp428" wrote in message news:[email protected]...
Now how can I pause it so I can pick an insertion point for the block?
0 Likes
Message 6 of 12

Anonymous
Not applicable
Yes that is whats happening, appears frozen.
This is creating bubble pointers for Samples, I have a lot of samples in an Access database that all need to be located on the drawing. when I click the "Go" button in Access it will stay in AutoCAD until all of the data is picked and placed. I'm making a loop to read all records then insert a block for each one where the user (me) picks.
0 Likes
Message 7 of 12

Anonymous
Not applicable
You can easily write VBA in Acad to read data from MS Access database and to
the block insertion accordingly inside Acad. In this case, your user does
not need MS Access installed, and you can avoid to automate Acad from
external application (MS Access application, in your case). Not to mention
the speed gain if you have many data record to loop through. Again, with
your situation, doing it by automating Acad from external app is by no means
a good choice and results in poor user experience.

"dbp428" wrote in message news:[email protected]...
Yes that is whats happening, appears frozen.
This is creating bubble pointers for Samples, I have a lot of samples in an
Access database that all need to be located on the drawing. when I click the
"Go" button in Access it will stay in AutoCAD until all of the data is
picked and placed. I'm making a loop to read all records then insert a block
for each one where the user (me) picks.
0 Likes
Message 8 of 12

Anonymous
Not applicable
I'm using 2000i I thought it did not connect to Jet or ADO? DAO? how can I do it?
0 Likes
Message 9 of 12

Anonymous
Not applicable
Hi dbp428,

In the VBAIDE select [Tools][References] and load the latest ADO library
you find on your computer.

If will be called "Microsoft ActiveX Data Objects 2.x Library"


Regards

Laurie

dbp428 wrote:
> I'm using 2000i I thought it did not connect to Jet or ADO? DAO? how can I do it?
0 Likes
Message 10 of 12

Anonymous
Not applicable
Thanks for that, Laurie,
But the form objects are spase to none in AutoCAD, I have subforms and all kind of stuff in my Access app. I think that's the best way to go since it's all already done. I just need to finish Acad bubble thingy. here is my code so far.
-----------
' OLE Automation Object Calls for AutoCAD 2000i
Set AcadObj = GetObject(, "AutoCAD.Application")
Set ThisDrawing = AcadObj.ActiveDocument

Dim blockObj As AcadBlock
Dim returnPntDonut, returnPntBlock As Variant
Dim docName, JobBlockName As String
Dim varAttributes As Variant
Dim Donut As AcadLWPolyline

' Bubble Pointer
JobBlockName = "C:\Program Files\AutoCAD LT 2000\Sample PointTB.dwg"

' pick insertion point on screen for donut
returnPntDonut = ThisDrawing.Utility.GetPoint(, "Pick point for dot: ")

'place donut at point above

Set Donut = ThisDrawing.ModelSpace.AddLightWeightPolyline(returnPntDonut)
Donut.SetBulge 0, 1
Donut.SetBulge 1, 1

' pick insertion point on screen for Sample point Bubble pointer
returnPntBlock = ThisDrawing.Utility.GetPoint(, "Pick point for Sample Point: ")

' insert Block
Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(returnPntBlock, JobBlockName, 1#, 1#, 1#, 0)

' Get the attributes for the block reference
varAttributes = blockRefObj.GetAttributes

' Change the value of the attribute
' Note: There is no SetAttributes. Once you have the variant array, you have the objects.
' Changing them changes the objects in the drawing.
SampleNumber = Right(Me![ChainOfCustody Details subform].Form.SampleNum, 4)
varAttributes(0).TextString = SampleNumber

' pick points for line from donut to bubble pointer
Dim lineObj As AcadLine
Set lineObj = ThisDrawing.ModelSpace.AddLine(basePnt, returnPnt)

Set blockRefObj = Nothing
Set ThisDrawing = Nothing
Set AcadObj = Nothing
0 Likes
Message 11 of 12

Anonymous
Not applicable
Hi dbp428,

You are wrong about the form objects in AutoCAD. As I don't program in
Access - I don't even have it on my computer - I'm unable to say if
Access has a wider range of controls, but I would not expect that it does.

It may have more elegant methods for moving data between the forms and
the database, but I can't comment on that without the software to test.


Regards

Laurie

dbp428 wrote:
> Thanks for that, Laurie,
> But the form objects are spase to none in AutoCAD, I have subforms and all kind of stuff in my Access app. I think that's the best way to go since it's all already done. I just need to finish Acad bubble thingy. here is my code so far.
> -----------
> ' OLE Automation Object Calls for AutoCAD 2000i
> Set AcadObj = GetObject(, "AutoCAD.Application")
> Set ThisDrawing = AcadObj.ActiveDocument
>
> Dim blockObj As AcadBlock
> Dim returnPntDonut, returnPntBlock As Variant
> Dim docName, JobBlockName As String
> Dim varAttributes As Variant
> Dim Donut As AcadLWPolyline
>
> ' Bubble Pointer
> JobBlockName = "C:\Program Files\AutoCAD LT 2000\Sample PointTB.dwg"
>
> ' pick insertion point on screen for donut
> returnPntDonut = ThisDrawing.Utility.GetPoint(, "Pick point for dot: ")
>
> 'place donut at point above
>
> Set Donut = ThisDrawing.ModelSpace.AddLightWeightPolyline(returnPntDonut)
> Donut.SetBulge 0, 1
> Donut.SetBulge 1, 1
>
> ' pick insertion point on screen for Sample point Bubble pointer
> returnPntBlock = ThisDrawing.Utility.GetPoint(, "Pick point for Sample Point: ")
>
> ' insert Block
> Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(returnPntBlock, JobBlockName, 1#, 1#, 1#, 0)
>
> ' Get the attributes for the block reference
> varAttributes = blockRefObj.GetAttributes
>
> ' Change the value of the attribute
> ' Note: There is no SetAttributes. Once you have the variant array, you have the objects.
> ' Changing them changes the objects in the drawing.
> SampleNumber = Right(Me![ChainOfCustody Details subform].Form.SampleNum, 4)
> varAttributes(0).TextString = SampleNumber
>
> ' pick points for line from donut to bubble pointer
> Dim lineObj As AcadLine
> Set lineObj = ThisDrawing.ModelSpace.AddLine(basePnt, returnPnt)
>
> Set blockRefObj = Nothing
> Set ThisDrawing = Nothing
> Set AcadObj = Nothing
0 Likes
Message 12 of 12

Anonymous
Not applicable
In case anyone is interested: I needed to get to an Access database in my AutoCAD form. here is the code to do that.
You have to add a reference in Microsoft ActiveX Data Objects 2.0 Library, In the VBA IDE click tools menu References, and go way down the list to find ones that start with Microsoft, then look for Microsoft ActiveX Data Objects 2.0 Library. Check the box next to it and click OK.
I created a command button and created this code. Then I added a text box for my project number.
------
Dim cnn As New ADODB.Connection, rec As ADODB.Recordset
Dim SQLString As String
Set cnn = New ADODB.Connection
Set rec = New ADODB.Recordset
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Task Order\Facility Description_be.mdb;Persist Security Info=False"
cnn.Open
SQLString = "Select * From [ChainOfCustody Details] Where " & _
"[Project Number] = '" & Me.txtProjectNumber & "'"
rec.Open (SQLString), cnn, adOpenStatic, adLockReadOnly
TotalRecs = rec.RecordCount
For a = 1 To TotalRecs
Debug.Print rec.Fields("SampleNum")
rec.MoveNext
Next

Set rec = Nothing
Set cnn = Nothing
----------
Now I have Microsoft Access (actually Office Professional 2003) installed on my computer so the reference may be there because of that. But this code does work I just finished it.

Notes:
F:\Task Order\Facility Description_be.mdb is the location of the Access database on our server. It could be anywhere.
ChainOfCustody Details is the Table in Access database I want to open
SampleNum is the field in ChainOfCustody Details. I am interested in getting the sample number. At some point will go into a bubble pointer (Wblock) Message was edited by: dbp428
0 Likes