I've absorbed the very helpful FAQ on here into a program I'm trying to write which would spit out the 3 attributes of a specific block (selected by the user) into a database table, one line per block. The code was to the point where I was getting one line per block, but the rows were blank. Now suddenly this morning, without making any changes I can recall, I'm getting an error on the
rstAttribs.Open , , , , adCmdText
line when I run the code (Command text was not set for the command object)...? Here's all the code;
Public AcadDoc As AcadDocument 'Current AutoCAD drawing document.
Public cnnDataBse As ADODB.Connection 'ADO connection to database.
Public rstAttribs As ADODB.Recordset 'ADO recordset to update.Global SS As AcadSelectionSet
Global BLKS As AcadBlocks
Global BLK As AcadBlockReference
Global BLK2 As AcadBlock
Global Grps(0 To 1) As Integer
Global Dats(0 To 1) As Variant
Global Filter1, Filter2 As Variant
Public Function Connect(strDatabase As String, strTableName As String)
' 1) strDatabase - The database file location.
' 2) strTableName - Table within the database to open.
' Connect "Drawings.mdb", strTblName
Set cnnDataBse = New ADODB.Connection
With cnnDataBse
.CursorLocation = adUseServer
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Properties("Data Source").Value = strDatabase
.Open
End With
Set rstAttribs = New ADODB.Recordset
With rstAttribs
.LockType = adLockPessimistic
.ActiveConnection = cnnDataBse
.CursorType = adOpenKeyset
.CursorLocation = adUseServer
.Source = strSQL
End With
rstAttribs.Open , , , , adCmdText
If rstAttribs.RecordCount <> 0 Then
rstAttribs.MoveFirst
End If
End Function
Sub BlockCounter()
Grps(0) = 0: Dats(0) = "INSERT"
Grps(1) = 2: Dats(1) = ""
On Error Resume Next
Set SS = ThisDrawing.SelectionSets.Add("SS")
If Err.Number <> 1 Then
Set SS = ThisDrawing.SelectionSets.Item("SS")
End If
SS.Clear
Set BLKS = ThisDrawing.Blocks
J = BLKS.Count - 1
For I = 2 To J
Set BLK2 = BLKS.Item(I)
Dats(1) = BLK2.Name
Filter1 = Grps
Filter2 = Dats
SS.Select acSelectionSetAll, , , Filter1, Filter2
OUT$ = BLK2.Name
BlockCount.ListBox2.AddItem OUT$
SS.Clear
Next I
While 1 = 1
BlockCount.Show
Wend
End Sub
Private Sub CommandButton1_Click()
End
End Sub
Private Sub CommandButton2_Click()
BlockCount.ListBox1.Clear
Dim SS As AcadSelectionSet
Dim BLKS As AcadBlocks
Dim BLK As AcadBlockReference
Dim BLK2 As AcadBlock
Dim Grps(0 To 1) As Integer
Dim Dats(0 To 1) As Variant
Dim Filter1, Filter2 As Variant
Grps(0) = 0: Dats(0) = "INSERT"
Grps(1) = 2: Dats(1) = BlockCount.ListBox2.Value
On Error Resume Next
Set SS = ThisDrawing.SelectionSets.Add("SS")
If Err.Number <> 1 Then
Set SS = ThisDrawing.SelectionSets.Item("SS")
End If
SS.Clear
Filter1 = Grps
Filter2 = Dats
SS.Select acSelectionSetAll, , , Filter1, Filter2
Dim J As Integer
J = SS.Count - 1
Dim varAttributes As Variant
Dim N As Integer
For I = 1 To J
Set BLK = SS.Item(I)
varAttributes = BLK.GetAttributes
For N = LBound(varAttributes) To UBound(varAttributes)
strAttributes = strAttributes & " Tag: " & varAttributes(N).TagString & _
" Value: " & varAttributes(N).TextString & " "
Next
OUT$ = SS.Item(I).Name & " " & strAttributes
strAttributes = ""
BlockCount.ListBox1.AddItem OUT$
Next I
SS.Clear
End Sub
Private Sub CommandButton3_Click()
Connect "c:\temp\hls.mdb", "import-temp"
BlockCount.ListBox1.Clear
Dim SS As AcadSelectionSet
Dim BLKS As AcadBlocks
Dim BLK As AcadBlockReference
Dim BLK2 As AcadBlock
Dim fldAttribs As ADODB.Field
Dim Grps(0 To 1) As Integer
Dim Dats(0 To 1) As Variant
Dim Filter1, Filter2 As Variant
Grps(0) = 0: Dats(0) = "INSERT"
Grps(1) = 2: Dats(1) = BlockCount.ListBox2.Value
On Error Resume Next
Set SS = ThisDrawing.SelectionSets.Add("SS")
If Err.Number <> 1 Then
Set SS = ThisDrawing.SelectionSets.Item("SS")
End If
SS.Clear
Filter1 = Grps
Filter2 = Dats
SS.Select acSelectionSetAll, , , Filter1, Filter2
Dim J As Integer
J = SS.Count - 1
Dim varAttributes As Variant
Dim N As Integer
For I = 1 To J
rstAttribs.AddNew
Set BLK = SS.Item(I)
varAttributes = BLK.GetAttributes
fldAttribs.Value = varAttributes(0).TextString
OUT$ = "RMNO=" & varAttributes(0).TextString & " ROOMNAME=" & varAttributes(1).TextString & " " & varAttributes(2).TextString
rstAttribs.Update
BlockCount.ListBox1.AddItem OUT$
Next I
SS.Clear
cnnDataBse.Close
End Sub
Basically there's a form with 3 buttons and 2 list boxes. When I run the macro BlockCount I get a list of all the blocks in the current drawing in listbox1. I can then select one of them and then click command button2 to get a list of each instance of the selected block along with all its attributes in listbox2. Then clicking command button3 is what spits the information into MS Access into a database file called hls.mdb in the table called import-temp. The next step will be to run some access macros to turn that table into a report saving the user the step of typing in all the room name and numbers when they are doing a survey of the building for code violations.
Any help would be greatly appreciated! I'm quite new at this and resourses like this seem to be the only things around that help!
rstAttribs.Open , , , , adCmdText
line when I run the code (Command text was not set for the command object)...? Here's all the code;
Public AcadDoc As AcadDocument 'Current AutoCAD drawing document.
Public cnnDataBse As ADODB.Connection 'ADO connection to database.
Public rstAttribs As ADODB.Recordset 'ADO recordset to update.Global SS As AcadSelectionSet
Global BLKS As AcadBlocks
Global BLK As AcadBlockReference
Global BLK2 As AcadBlock
Global Grps(0 To 1) As Integer
Global Dats(0 To 1) As Variant
Global Filter1, Filter2 As Variant
Public Function Connect(strDatabase As String, strTableName As String)
' 1) strDatabase - The database file location.
' 2) strTableName - Table within the database to open.
' Connect "Drawings.mdb", strTblName
Set cnnDataBse = New ADODB.Connection
With cnnDataBse
.CursorLocation = adUseServer
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Properties("Data Source").Value = strDatabase
.Open
End With
Set rstAttribs = New ADODB.Recordset
With rstAttribs
.LockType = adLockPessimistic
.ActiveConnection = cnnDataBse
.CursorType = adOpenKeyset
.CursorLocation = adUseServer
.Source = strSQL
End With
rstAttribs.Open , , , , adCmdText
If rstAttribs.RecordCount <> 0 Then
rstAttribs.MoveFirst
End If
End Function
Sub BlockCounter()
Grps(0) = 0: Dats(0) = "INSERT"
Grps(1) = 2: Dats(1) = ""
On Error Resume Next
Set SS = ThisDrawing.SelectionSets.Add("SS")
If Err.Number <> 1 Then
Set SS = ThisDrawing.SelectionSets.Item("SS")
End If
SS.Clear
Set BLKS = ThisDrawing.Blocks
J = BLKS.Count - 1
For I = 2 To J
Set BLK2 = BLKS.Item(I)
Dats(1) = BLK2.Name
Filter1 = Grps
Filter2 = Dats
SS.Select acSelectionSetAll, , , Filter1, Filter2
OUT$ = BLK2.Name
BlockCount.ListBox2.AddItem OUT$
SS.Clear
Next I
While 1 = 1
BlockCount.Show
Wend
End Sub
Private Sub CommandButton1_Click()
End
End Sub
Private Sub CommandButton2_Click()
BlockCount.ListBox1.Clear
Dim SS As AcadSelectionSet
Dim BLKS As AcadBlocks
Dim BLK As AcadBlockReference
Dim BLK2 As AcadBlock
Dim Grps(0 To 1) As Integer
Dim Dats(0 To 1) As Variant
Dim Filter1, Filter2 As Variant
Grps(0) = 0: Dats(0) = "INSERT"
Grps(1) = 2: Dats(1) = BlockCount.ListBox2.Value
On Error Resume Next
Set SS = ThisDrawing.SelectionSets.Add("SS")
If Err.Number <> 1 Then
Set SS = ThisDrawing.SelectionSets.Item("SS")
End If
SS.Clear
Filter1 = Grps
Filter2 = Dats
SS.Select acSelectionSetAll, , , Filter1, Filter2
Dim J As Integer
J = SS.Count - 1
Dim varAttributes As Variant
Dim N As Integer
For I = 1 To J
Set BLK = SS.Item(I)
varAttributes = BLK.GetAttributes
For N = LBound(varAttributes) To UBound(varAttributes)
strAttributes = strAttributes & " Tag: " & varAttributes(N).TagString & _
" Value: " & varAttributes(N).TextString & " "
Next
OUT$ = SS.Item(I).Name & " " & strAttributes
strAttributes = ""
BlockCount.ListBox1.AddItem OUT$
Next I
SS.Clear
End Sub
Private Sub CommandButton3_Click()
Connect "c:\temp\hls.mdb", "import-temp"
BlockCount.ListBox1.Clear
Dim SS As AcadSelectionSet
Dim BLKS As AcadBlocks
Dim BLK As AcadBlockReference
Dim BLK2 As AcadBlock
Dim fldAttribs As ADODB.Field
Dim Grps(0 To 1) As Integer
Dim Dats(0 To 1) As Variant
Dim Filter1, Filter2 As Variant
Grps(0) = 0: Dats(0) = "INSERT"
Grps(1) = 2: Dats(1) = BlockCount.ListBox2.Value
On Error Resume Next
Set SS = ThisDrawing.SelectionSets.Add("SS")
If Err.Number <> 1 Then
Set SS = ThisDrawing.SelectionSets.Item("SS")
End If
SS.Clear
Filter1 = Grps
Filter2 = Dats
SS.Select acSelectionSetAll, , , Filter1, Filter2
Dim J As Integer
J = SS.Count - 1
Dim varAttributes As Variant
Dim N As Integer
For I = 1 To J
rstAttribs.AddNew
Set BLK = SS.Item(I)
varAttributes = BLK.GetAttributes
fldAttribs.Value = varAttributes(0).TextString
OUT$ = "RMNO=" & varAttributes(0).TextString & " ROOMNAME=" & varAttributes(1).TextString & " " & varAttributes(2).TextString
rstAttribs.Update
BlockCount.ListBox1.AddItem OUT$
Next I
SS.Clear
cnnDataBse.Close
End Sub
Basically there's a form with 3 buttons and 2 list boxes. When I run the macro BlockCount I get a list of all the blocks in the current drawing in listbox1. I can then select one of them and then click command button2 to get a list of each instance of the selected block along with all its attributes in listbox2. Then clicking command button3 is what spits the information into MS Access into a database file called hls.mdb in the table called import-temp. The next step will be to run some access macros to turn that table into a report saving the user the step of typing in all the room name and numbers when they are doing a survey of the building for code violations.
Any help would be greatly appreciated! I'm quite new at this and resourses like this seem to be the only things around that help!