Hi Todd,
The variable blockObj is never set = to the current ent that is a block
reference and has a name starting with PIPE, or in other words:
This
If Left(str, 4) = "PIPE" Then
If blockObj.HasAttributes Then
ObjAtt = blockObj.GetAttributes
MsgBox "Here is the value " & ObjAtt(0).TextString
End If
Is missing this
Set blockObj = ent
So that it will look like:
Private Sub CommandButton1_Click()
Dim blockObj As AcadBlockReference
Dim ObjAtt As Variant
Dim value As String
Dim ssetObj As AcadSelectionSet
Dim ent As Object
Dim mode As Integer
Dim rtnPoint1, rtnPoint2 As Variant
Dim i As Integer
Dim str As String
Set ssetObj = ThisDrawing.SelectionSets.Add("pipe")
mode = acSelectionSetCrossing
Me.Hide
rtnPoint1 = ThisDrawing.Utility.GetPoint(, "Point 1 ")
rtnPoint2 = ThisDrawing.Utility.GetPoint(, "Point 2 ")
ssetObj.Select mode, rtnPoint1, rtnPoint2
For Each ent In ssetObj
str = ent.Name
If Left(str, 4) = "PIPE" Then
Set blockObj = ent
If blockObj.HasAttributes Then
ObjAtt = blockObj.GetAttributes
MsgBox "Here is the value " & ObjAtt(0).TextString
End If
End If
Next ent
End Sub
I couldn't resist adding a few other other fine points, try this:
Private Sub CommandButton1_Click()
Dim blockObj As AcadBlockReference
Dim ObjAtt As Variant
Dim value As String
Dim intType(1) As Integer
Dim varData(1) As Variant
Dim ssetObj As AcadSelectionSet
Dim mode As Integer
Dim objUtil As AcadUtility
Dim rtnPoint1, rtnPoint2 As Variant
Dim i As Integer
Dim str As String
On Error GoTo Err_Control
intType(0) = 0
varData(0) = "INSERT"
intType(1) = 2
varData(1) = "PIPE*"
Set objUtil = ThisDrawing.Utility
Set ssetObj = ThisDrawing.SelectionSets.Add("pipe")
mode = acSelectionSetCrossing
Me.Hide
rtnPoint1 = objUtil.GetPoint(, "Point 1 ")
objUtil.InitializeUserInput 32
rtnPoint2 = objUtil.GetPoint(rtnPoint1, "Point 2 ")
ssetObj.Select mode, rtnPoint1, rtnPoint2, intType, varData
For Each blockObj In ssetObj
If blockObj.HasAttributes Then
ObjAtt = blockObj.GetAttributes
MsgBox "Here is the value " & ObjAtt(0).TextString
End If
Next blockObj
ssetObj.Delete
Exit_Here:
Exit Sub
Err_Control:
Select Case Err.Number
Case -2145320851
If RemoveSet("pipe") Then
Resume
Else
MsgBox Err.Description
Resume Exit_Here
End If
Case Else
MsgBox Err.Description
Resume Exit_Here
End Select
End Sub
Function RemoveSet(strName As String) As Boolean
On Error GoTo Err_Control
ThisDrawing.SelectionSets(strName).Delete
RemoveSet = True
Exit_Here:
Exit Function
Err_Control:
Resume Exit_Here
End Function
Now, lets see, I know I wrote an article with code that will give you just
what you need for that data base question ... Ah, here it is. This is from
the Jan. 14, 2000 issue of A Code A Day, enjoy:
DATABASE ANYONE?
Warning, another boring lesson by my dad ~ Jessica!
Ok, lets try that whole database thing over from the beginning. For the sake
of expedience I will assume that you are familiar with database structure
and SQL and that the only thing I need to provide is the information to work
with databases using the languages available to us in VBA. Ralph and I
worked for a long time to develop a tutorial that would provide information,
working examples, and explanations of the processes involved in database
related code. We couldn't do it, so you get this. Ok, just kidding. Lets
start with the fundamentals of DAO from a real life view.
Creation
Before you can create, you need a blue print. For the sake of keeping this
AutoCAD related, our blueprint is a block within the drawing, what the block
represents does not matter, it need only have attributes (no constant
attributes, please) and a name. Creation of the database is easy, so easy it
can be done in two lines of VBA code:
Dim objDBlk As Database
Set objDBlk = CreateDatabase("C:\Block.mdb", dbLangGeneral)
As easy as that, there is now a database named Block.mdb at the root of the
c drive. Of course it has no records, or tables to contain them, but before
we move into building the database structure lets look a little closer at
that code:
The syntax for the CreateDataBase" method is:
CreateDataBase (Name As String, Local As String, [Option])
And its return value is the created database. The first argument is clear
enough, we need to provide the name for the database (this can also be a
network path), but what is "Local"? Its a string that defines the collating
order for creating the database! In this case I used "dbLangGeneral" which
is a constant from the DAO library for ";LANGID=0x0409;CP=1252;COUNTRY=0"
The following list provides all available constants for DAO:
dbLangGeneral = English, German, French, Portuguese, Italian, and Modern
Spanish
dbLangArabic = Arabic
dbLangChineseSimplified = Simplified Chinese
dbLangChineseTraditional = Traditional Chinese
dbLangCyrillic = Russian
dbLangCzech = Czech
dbLangDutch = Dutch
dbLangGreek = Greek
dbLangHebrew = Hebrew
dbLangHungarian = Hungarian
dbLangIcelandic = Icelandic
dbLangJapanese = Japanese
dbLangKorean = Korean
dbLangNordic = Nordic languages (Microsoft Jet database engine version 1.0
only)
dbLangNorwDan = Norwegian and Danish
dbLangPolish = Polish
dbLangSlovenian = Slovenian
dbLangSpanish = Traditional Spanish
dbLangSwedFin = Swedish and Finnish
dbLangThai = Thai
dbLangTurkish = Turkish
You can also provide a password in this string by concatenating the constant
and the password like this:
dbLangGeneral & ";pwd=wonderllama"
The last argument is for encrypting the database or providing the version
that it should be created as. The applicable constants are:
dbEncrypt
Creates an encrypted database.
dbVersion10
Creates a database that uses the Microsoft Jet database engine version 1.0
file format.
dbVersion11
Creates a database that uses the Microsoft Jet database engine version 1.1
file format.
dbVersion20
Creates a database that uses the Microsoft Jet database engine version 2.0
file format.
dbVersion30
(Default) Creates a database that uses the Microsoft Jet database engine
version 3.0 file format (compatible with version 3.5).
There is a hidden aspect to this process. The database is actually being
created by a workspace, and by not providing the workspace in the methods
syntax DAO uses the default workspace. What is a workspace? Simple stuff
really, a workspace is a named session of a database engine (don't even ask)
that contains all of the users open databases, log in and off times, etc.
For this project, this just isn't needed as the default workspace provides
the settings we are going to need. If you are interested in working with
Workspaces, I plan on covering them down the road, but the general syntax
looks like:
Dim objWS As Workspace
Dim objDBlk As Database
Set objWS = DBEngine(0)
Set objDBlk = objWS.CreateDatabase("C:\Block.mdb", dbLangGeneral)
Again I will cover the use of Workspace objects in greater detail later. For
now we are going to stay focused on the basics.
Table for Four, please
Now that we have the database creation down, we need to give it some
structure so that we can place records into it. In this example we pick a
block from the AutoCAD graphics screen and use its structure to provide our
database's structure. Lets peek at the process:
Public Function CreateDB(strName As String) As Database
Dim objDBBlks As Database
Dim objTbl As TableDef
Dim objFld As Field
Dim objBlk As AcadBlockReference
Dim varAtts As Variant
Dim varPnt As Variant
Dim intCnt As Integer
Dim strPrmpt As String
strPrmpt = "Select a block: "
On Error GoTo Error_Control
ThisDrawing.Utility.GetEntity objBlk, varPnt, strPrmpt
If objBlk.HasAttributes Then
varAtts = objBlk.GetAttributes
Set objDBBlks = CreateDatabase(strName, dbLangGeneral)
Set objTbl = objDBBlks.CreateTableDef(objBlk.Name)
For intCnt = LBound(varAtts) To UBound(varAtts)
Set objFld = objTbl.CreateField(varAtts(intCnt).TagString, dbText)
objTbl.Fields.Append objFld
Next
objDBBlks.TableDefs.Append objTbl
Set CreateDB = objDBBlks
Else
MsgBox "The selected block did not have any attributes"
End If
Exit Function
Error_Control:
'For example just dump out
MsgBox "unbelievable fatal error, your not even allowed to have this
error!"
'This will of course leave the calling function open to an error, so there
had
'better be a test to make sure that this returned something (Not Nothing)
End Function
A few new objects to be defined here, TableDef & field. Think of them in
terms of an Excel spreadsheet, where the TableDef is an individual sheet and
the Fields are Columns. In this example, we made a new sheet named whatever
the block is named and added columns (how many depends on how many
attributes the block contained) named after each attribute's tag string.
Notice that this procedure is a Function, that is to say, it returns a
value. In this case it returns the newly created database, which is, by the
way, still open. This particular example is far short of ideal for a
database creation. Ralph and I left out creating relationships with existing
tables (this can be done even with tables from other databases), Adding an
Index (more on that later), and adding queries. Like I said, just the basics
for now. A few of the lines need a closer look:
Set objFld = objTbl.CreateField(varAtts(intCnt).TagString, dbText)
This line provides the name for the field and defines the kind of data that
will be stored there, dbText. You can use any of the following constants to
define the type for a field,
DbBigInt
Stores a signed, exact numeric value with precision 19 (signed) or 20
(unsigned), scale 0 (signed: -263 = n = 263-1; unsigned: 0 = n = 264-1).
DbBinary
Stores fixed-length binary data. The maximum length is 255 bytes.
DbBoolean
A True/False or yes/no value.
DbByte
Used to hold small positive integer numbers ranging from 0 to 255.
DbChar
Stores a fixed-length character string.
DbCurrency
Store numbers with up to 15 digits to the left of the decimal point and 4
digits to the right.
DbDate
A real number (Julian date time see the Llama Files)
DbDecimal
Stores a signed, exact numeric value with precision p and scale s (1 = p
=15; 0 = s = p).
DbDouble
Holds double-precision floating-point numbers in IEEE format. A Double
variable is stored as a 64-bit (8-byte) number ranging in value
from -1.79769313486231E308 to -4.94065645841247E-324 for negative values,
from 4.94065645841247E-324 to 1.79769313486231E308 for positive values, and
0.
DbFloat
Stores a signed, approximate numeric value with mantissa precision 15 (zero
or absolute value 10-308 to 10308).
DbGUID
A unique identification string used with remote procedure calls.
DbInteger
An Integer variable stored as a 16-bit (2-byte) number ranging in value
from -32,768 to 32,767.
DbLong
A Long variable stored as a 32-bit (4-byte) number ranging in value
from -2,147,483,648 to 2,147,483,647.
DbLongBinary
Objects created in other applications that can be linked or embedded in a
Microsoft Jet database.
DbMemo
Can contain up to 1.2 GB of text data.
DbNumeric
Stores a signed, exact numeric value with precision p and scale s (1 = p
=15; 0 = s = p).
DbSingle
Variable stored as a 32-bit (4-byte) number ranging in value
from -3.402823E38 to -1.401298E-45 for negative values, from 1.401298E-45 to
3.402823E38 for positive values, and 0.
DbText
Can contain up to 255 characters or the number of characters specified by
the Size property of the Field object, whichever is less. If the Size
property of the text field is set to 0, the text field can hold up to 255
characters of data.
DbTime
Stores a time value.
DbTimeStamp
Stores a TimeStamp.
DbVarBinary
Stores variable-length binary data.
Now you might have noticed (assuming you ACCTUALY read all of those) that
dbText (the one used in the example) can be limited to the specified size in
the "Size" property. Doesn't look like I mentioned anything about a
"property" at all in the example, does it? Well, I didn't. I have no idea
how long those attribute tag strings are going to be, but if I did, I might
have changed that line to:
Set objFld = objTbl.CreateField(varAtts(intCnt).TagString, dbText, 15)
Or however long I needed the field to be. By doing this I force the users of
this database to provide strings of the "size" length (or less).
Then there is this line:
objTbl.Fields.Append objFld
Which very simply adds this new field into the fields collection of the new
table definition. Later we do the same thing for the table definition:
objDBBlks.TableDefs.Append objTbl
Which adds the new table definition to the TableDefs collection of the new
database.
Simple? Confused? Excited? Let me know what questions you have, or comments,
in the next lesson we will cover any of these and do something productive
with this code!