Block Attributes To Access

Discussion in 'AutoCAD' started by Tecajnik1, Jan 21, 2004.

  1. Tecajnik1

    Tecajnik1 Guest

    Hello!

    I need some help with my new application. Soory for my bad English.

    Application runs in AutoCAD and. AutoCAD projects includes a lot of BLOCKs
    with Attributes. I would like to store this values of Attributes into a
    Database (Access). In the current project a have many BLOCKs with the same
    name, but with different Attribute VAlues. This Values is needed for
    calculation of entire project.

    So, the new block (Extetnal) that I insert into project, should have some
    data (Attributes) that are stored into database (price, time to oreder,
    quantity in by store, ...) and if there is no item into the store the system
    should prompt me that I MUST order the item; This values of blocks are used
    for other financial program that also "look" into database of this project.
    Then I can make quick calculation how much the project costs, how many and
    witch items I need, witch i must order, etc.

    I tried to use and program with VBA and DAO 3.6. Something is missing: how
    to link block attribute to database record? Please HELP!!!

    Thx in advance.

    Uros LESJAK,
    Slovenia - EU
     
    Tecajnik1, Jan 21, 2004
    #1
  2. The description of the process for transferring attributes between AutoCAD
    and MS-Access goes like this.

    1. Set a project reference to the data access library you are using, either
    MS Data Access Objects (DAO) or MS ActiveX Data Objects (ADO).

    2. In the AutoCAD VBA code, create a connection to the MS-Access database
    using DAO or ADO. See the documentation for details.

    3. Collect the blocks containing the attributes you want into a selection
    set.

    4.. Step through the selection set and save the attribute objects you want
    to an array or collection. This should include a primary key which uniquely
    identifies the block to which the attribute belongs. An entity handle works
    well for this purpose.

    5. To send attributes from AutoCAD to MS-Access:
    a. Concatenate a string expression that contains a SQL INSERT statement
    containing the primary key, attribute tag, and attribute value . See the
    documentation for details.
    b. Execute the SQL INSERT string expression via the connection from step
    2. This will add a new record to the database table.
    c. Repeat to add as many records as you need.

    6. To send attributes from MS-Access to AutoCAD:
    a. Concatenate a string expression that contains a SQL SELECT statement
    containing the fields you want. See the documentation for details.
    b. Execute the SQL SELECT string expression via the connection from step
    2. This will create a RECORDSET object.
    c. Step through the RECORDSET object. Read the primary key, attribute
    tag, and attribute value of the current record. Locate the array or
    collection element from step 4 with the matching primary key and attribute
    tag. Update the attribute value.

    When you have digested this very basic outline, ask some more questions. Of
    course, experienced developers will see other probably more efficient
    methods. Fell free to jump in.
     
    John Goodfellow, Jan 22, 2004
    #2
  3. Tecajnik1

    Jason M Culp Guest

    Here's a snipet of code for accessing the attributes of blocks. As for the
    Access access, I have no experience. my application for this subroutine was
    to pull the revision letter from a block with attributes. Since multiple
    rev blocks could possibly reside in a particular drawing, I had to find a
    way to get the individual attribute values. I found that the only way to
    access the attributes for a block already in the drawing was to cycle
    through the model space objects and use the AcadBlockReference object to
    access the attributes. Please realize this is a rough draft, and that there
    are most likely more efficient ways to handle this, but I thought it may
    help in some way. If anybody has tips to streamline this routine, I'm open
    for suggestions. I don't particularly like having to cycle through all of
    the ModelSpace objects.

    Regards,
    Jason

    Public Sub GetAcadRevision(ByVal DontKnowDoc As AcadDocument)
    Dim BlockRef As AcadBlockReference
    Dim X As Integer
    LatestRev = ""
    Dim AttSet As Variant
    Dim item As AcadObject

    On Error Resume Next

    For Each item In DontKnowDoc.ModelSpace
    If item.HasAttributes Then
    If Not Err Then
    AttSet = item.GetAttributes
    For X = LBound(AttSet) To UBound(AttSet)
    If AttSet(X).TagString = "REV" Then
    If LatestRev = "" Then
    LatestRev = AttSet(X).TextString
    Else
    If Asc(AttSet(X).TextString) > Asc(LatestRev)
    Then
    LatestRev = AttSet(X).TextString
    End If
    End If
    End If
    Next
    Else
    Err.Clear
    End If
    End If
    Next

    On Error GoTo 0

    If LatestRev = "" Then
    LatestRev = "0"
    End If

    End Sub
     
    Jason M Culp, Jan 27, 2004
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.