473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

VBA to populate access table

I am completely stuck...

The following code is supposed to get the attribute values from an acad drawing and populate an access table. However it only populates one row and stops.

Is it possible to modify the code so that the array will export all the attribute values to the table?

Any assistance is appreciated



Expand|Select|Wrap|Line Numbers
  1. Sub ExportTitle()  
  2.  
  3. ' Open database and delete old records if exist
  4. ' ---------------------------------------------
  5.     Dim dbs As DAO.Database
  6.     Dim dbsName As String
  7.  
  8.     dbsName = "Project_Drawings.mdb"
  9.     Set dbs = DBEngine.OpenDatabase("C:\Users\tim\Desktop\" & dbsName)
  10.  
  11.  
  12.     Dim activeDoc As AcadDocument
  13.     Dim DocLength As Integer
  14.     Dim Shortlength As Integer
  15.     Dim DwgPath As String
  16.     Dim DwgName As String
  17.     Dim DocName As String
  18.     Set activeDoc = ThisDrawing.Application.ActiveDocument
  19.     DocName = activeDoc.Name
  20.     DocLength = Len(DocName)
  21.  
  22. ' Strip of extension .dwg
  23. ' -------------------------------
  24.     Shortlength = DocLength - 4
  25.     DwgPath = activeDoc.Path
  26.     DwgName = Left(DocName, Shortlength)
  27.  
  28. ' Open recordset in table
  29. ' -----------------------
  30.     Dim data As DAO.Recordset
  31.     Dim TableDef As String
  32.  
  33.  
  34. ' Determine Tables Def
  35. ' --------------------------
  36.     TableDef = "Attribute Report1"
  37.  
  38.     Set data = dbs.OpenRecordset(TableDef, dbOpenDynaset)
  39.  
  40.  
  41. ' Clear old values from table
  42. ' ---------------------------
  43.     dbs.Execute "DELETE * FROM [Attribute Report1]", dbFailOnError
  44.  
  45.     Dim attribs
  46.     Dim Handle As String
  47.     Dim Blockname As String
  48.     Dim Tag As String
  49.     Dim Loops As String
  50.     Dim Address As String
  51.     Dim Label1 As String
  52.     Dim Label2 As String
  53.     Dim Device_Label As String
  54.     Dim Extended_Label As String
  55.     Dim Qty As String
  56.     Dim Model_Num As String
  57.     Dim Description As String
  58.     Dim Vendor As String
  59.     Dim CSFM_Num As String
  60.  
  61.  
  62. ' Make Selection set of Blocks
  63. ' ----------------------------
  64.     Dim ssnew As Object
  65.     Dim setObj As Object
  66.     Dim setColl As AcadSelectionSets
  67.     Dim Entity As AcadEntity
  68.          With activeDoc
  69.           Set setColl = .SelectionSets
  70.           For Each setObj In .SelectionSets
  71.                If setObj.Name = "VBA" Then
  72.                     .SelectionSets.item("VBA").Delete
  73.                     Exit For
  74.                End If
  75.           Next
  76.  
  77.     Set ssnew = activeDoc.SelectionSets.Add("VBA")
  78.     End With
  79.     ssnew.Select acSelectionSetAll
  80.  
  81.  
  82. ' Get Attribute values
  83. ' ---------------------
  84. Dim i
  85.     For Each Entity In ssnew
  86.         attribs = Entity.GetAttributes
  87.         For i = LBound(attribs) To UBound(attribs)
  88.  
  89.             If attribs(i).TagString = "HANDLE" Then
  90.               Handle = attribs(i).TextString
  91.             End If
  92.             If attribs(i).TagString = "BLOCKNAME" Then
  93.               Blockname = attribs(i).TextString
  94.             End If
  95.             If attribs(i).TagString = "TAG" Then
  96.               Tag = attribs(i).TextString
  97.             End If
  98.             If attribs(i).TagString = "LOOP" Then
  99.               Loops = attribs(i).TextString
  100.             End If
  101.             If attribs(i).TagString = "ADDRESS" Then
  102.               Address = attribs(i).TextString
  103.             End If
  104.             If attribs(i).TagString = "LABEL1" Then
  105.               Label1 = attribs(i).TextString
  106.             End If
  107.             If attribs(i).TagString = "LABEL2" Then
  108.               Label2 = attribs(i).TextString
  109.             End If
  110.             If attribs(i).TagString = "DEVICE_LABEL" Then
  111.               Device_Label = attribs(i).TextString
  112.             End If
  113.             If attribs(i).TagString = "EXTENDED_LABEL" Then
  114.               Extended_Label = attribs(i).TextString
  115.             End If
  116.             If attribs(i).TagString = "QTY" Then
  117.               Qty = attribs(i).TextString
  118.             End If
  119.             If attribs(i).TagString = "MODEL_NUM" Then
  120.               Model_Num = attribs(i).TextString
  121.             End If
  122.             If attribs(i).TagString = "DESCRIPTION" Then
  123.               Description = attribs(i).TextString
  124.             End If
  125.             If attribs(i).TagString = "VENDOR" Then
  126.               Vendor = attribs(i).TextString
  127.             End If
  128.             If attribs(i).TagString = "CSFM_NUM" Then
  129.               CSFM_Num = attribs(i).TextString
  130.             End If
  131.  
  132.          Next
  133.  
  134.  
  135. ' Fill Database Table-----------------
  136.  
  137.          If Handle = "" Then Handle = " "
  138.          If Blockname = "" Then Blockname = " "
  139.          If Tag = "" Then Tag = " "
  140.          If Loops = "" Then Loops = " "
  141.          If Address = "" Then Address = " "
  142.          If Label1 = "" Then Label1 = " "
  143.          If Label2 = "" Then Label2 = " "
  144.          If Device_Label = "" Then Device_Label = " "
  145.          If Extended_Label = "" Then Extended_Label = " "
  146.          If Qty = "" Then Qty = " "
  147.          If Model_Num = "" Then Model_Num = " "
  148.          If Description = "" Then Description = " "
  149.          If Vendor = "" Then Vendor = " "
  150.          If CSFM_Num = "" Then CSFM_Num = " "
  151.  
  152.  
  153.          data.AddNew
  154.  
  155.          data!Handle = "'" & Handle
  156.          data!Blockname = Blockname
  157.          data!Tag = Tag
  158.          data!Address = Address
  159.          data!Label1 = Label1
  160.          data!Label2 = Label2
  161.          data!Device_Label = Device_Label
  162.          data!Extended_Label = Extended_Label
  163.          data!Qty = Qty
  164.          data!Model_Num = Model_Num
  165.          data!Description = Description
  166.          data!Vendor = Vendor
  167.          data!CSFM_Num = CSFM_Num
  168.  
  169.          data.Update  '****Gets Stuck Here****
  170.  
  171.     Next
  172.  
  173. ' Close Database
  174. ' --------------
  175.   data.Close
  176.   dbs.Close
  177.   Set data = Nothing
  178.   Set dbs = Nothing
  179.   ssnew.Delete
  180.  
  181. End Sub
  182.  
  183.  
Apr 11 '15 #1

✓ answered by NeoPa

If I were to guess it would be that your description of the problem is flawed Tim - in as much as the comment in the code says it gets stuck at that point.

Reading the code it seems clear that the section that writes to the table is outside of the loop. Thus, would only ever run once. In order to get it to write a record for each item in the loop then the code must fall within the loop itself.

As a general rule it is a very good idea to indent code where it makes sense to (Between For ... Next, With ... End With, Do ... Loop, etc). Nevertheless, it can actually be more of a hindrance than a help unless the indenting is done consistently throughout the code. Not that this is a particularly bad example, but it can be such a help to you that I thought I'd mention it.

7 2342
NeoPa
32,556 Expert Mod 16PB
If I were to guess it would be that your description of the problem is flawed Tim - in as much as the comment in the code says it gets stuck at that point.

Reading the code it seems clear that the section that writes to the table is outside of the loop. Thus, would only ever run once. In order to get it to write a record for each item in the loop then the code must fall within the loop itself.

As a general rule it is a very good idea to indent code where it makes sense to (Between For ... Next, With ... End With, Do ... Loop, etc). Nevertheless, it can actually be more of a hindrance than a help unless the indenting is done consistently throughout the code. Not that this is a particularly bad example, but it can be such a help to you that I thought I'd mention it.
Apr 11 '15 #2
Thank you so much for the reply and the pointers...

As I am not skilled in vba and barely know how to put pieces of code together. I will do some research on this. Just please point me in the right direction. Is it that I need to move the the secton that writes the update in to the existing loop and add something like a "next row +1" to the loop. Or does it need something more?

Thanks again for any pointers...
Apr 11 '15 #3
Awesome...

I got it to work. It was caught up on the primary key field.
Now if possible how do i clean up the code...
Apr 11 '15 #4
NeoPa
32,556 Expert Mod 16PB
TimLeonard:
Now if possible how do i clean up the code...
That's easy enough to do, but a little harder to explain.

In this case I think you just need to move the Dim lines to the top and determine how many indents are required at each point.
Expand|Select|Wrap|Line Numbers
  1. Sub ExportTitle()  
  2.  
  3. ' Open database and delete old records if exist
  4. ' ---------------------------------------------
  5.     Dim dbs As DAO.Database
  6.     Dim dbsName As String
  7.     Dim activeDoc As AcadDocument
  8.     Dim DocLength As Integer
  9.     Dim Shortlength As Integer
  10.     Dim DwgPath As String
  11.     Dim DwgName As String
  12.     Dim DocName As String
  13.     Dim data As DAO.Recordset
  14.     Dim TableDef As String
  15.     Dim attribs            'Defaults to Variant
  16.     Dim Handle As String
  17.     Dim Blockname As String
  18.     Dim Tag As String
  19.     Dim Loops As String
  20.     Dim Address As String
  21.     Dim Label1 As String
  22.     Dim Label2 As String
  23.     Dim Device_Label As String
  24.     Dim Extended_Label As String
  25.     Dim Qty As String
  26.     Dim Model_Num As String
  27.     Dim Description As String
  28.     Dim Vendor As String
  29.     Dim CSFM_Num As String
  30.     Dim ssnew As Object
  31.     Dim setObj As Object
  32.     Dim setColl As AcadSelectionSets
  33.     Dim Entity As AcadEntity
  34.     Dim i            'Defaults to Variant
  35.  
  36.     dbsName = "Project_Drawings.mdb"
  37.     Set dbs = DBEngine.OpenDatabase("C:\Users\tim\Desktop\" & dbsName)
  38.     Set activeDoc = ThisDrawing.Application.ActiveDocument
  39.     DocName = activeDoc.Name
  40.     DocLength = Len(DocName)
  41.  
  42. ' Strip off extension .dwg
  43. ' -------------------------------
  44.     Shortlength = DocLength - 4
  45.     DwgPath = activeDoc.Path
  46.     DwgName = Left(DocName, Shortlength)
  47.  
  48. ' Open recordset in table
  49. ' -----------------------
  50.  
  51. ' Determine Tables Def
  52. ' --------------------------
  53.     TableDef = "Attribute Report1"
  54.     Set data = dbs.OpenRecordset(TableDef, dbOpenDynaset)
  55.  
  56. ' Clear old values from table
  57. ' ---------------------------
  58.     dbs.Execute "DELETE * FROM [Attribute Report1]", dbFailOnError
  59.  
  60. ' Make Selection set of Blocks
  61. ' ----------------------------
  62.     With activeDoc
  63.         Set setColl = .SelectionSets
  64.         For Each setObj In .SelectionSets
  65.             If setObj.Name = "VBA" Then
  66.                 .SelectionSets.item("VBA").Delete
  67.                 Exit For
  68.             End If
  69.         Next
  70.         Set ssnew = activeDoc.SelectionSets.Add("VBA")
  71.     End With
  72.     ssnew.Select acSelectionSetAll
  73.  
  74. ' Get Attribute values
  75. ' ---------------------
  76.     For Each Entity In ssnew
  77.         attribs = Entity.GetAttributes
  78.         For i = LBound(attribs) To UBound(attribs)
  79.  
  80.             If attribs(i).TagString = "HANDLE" Then
  81.                 Handle = attribs(i).TextString
  82.             End If
  83.             If attribs(i).TagString = "BLOCKNAME" Then
  84.                 Blockname = attribs(i).TextString
  85.             End If
  86.             If attribs(i).TagString = "TAG" Then
  87.                 Tag = attribs(i).TextString
  88.             End If
  89.             If attribs(i).TagString = "LOOP" Then
  90.                 Loops = attribs(i).TextString
  91.             End If
  92.             If attribs(i).TagString = "ADDRESS" Then
  93.                 Address = attribs(i).TextString
  94.             End If
  95.             If attribs(i).TagString = "LABEL1" Then
  96.                 Label1 = attribs(i).TextString
  97.             End If
  98.             If attribs(i).TagString = "LABEL2" Then
  99.                 Label2 = attribs(i).TextString
  100.             End If
  101.             If attribs(i).TagString = "DEVICE_LABEL" Then
  102.                 Device_Label = attribs(i).TextString
  103.             End If
  104.             If attribs(i).TagString = "EXTENDED_LABEL" Then
  105.                 Extended_Label = attribs(i).TextString
  106.             End If
  107.             If attribs(i).TagString = "QTY" Then
  108.                 Qty = attribs(i).TextString
  109.             End If
  110.             If attribs(i).TagString = "MODEL_NUM" Then
  111.                 Model_Num = attribs(i).TextString
  112.             End If
  113.             If attribs(i).TagString = "DESCRIPTION" Then
  114.                 Description = attribs(i).TextString
  115.             End If
  116.             If attribs(i).TagString = "VENDOR" Then
  117.                 Vendor = attribs(i).TextString
  118.             End If
  119.             If attribs(i).TagString = "CSFM_NUM" Then
  120.                 CSFM_Num = attribs(i).TextString
  121.             End If
  122.  
  123. ' Fill Database Table-----------------
  124.             If Handle = "" Then Handle = " "
  125.             If Blockname = "" Then Blockname = " "
  126.             If Tag = "" Then Tag = " "
  127.             If Loops = "" Then Loops = " "
  128.             If Address = "" Then Address = " "
  129.             If Label1 = "" Then Label1 = " "
  130.             If Label2 = "" Then Label2 = " "
  131.             If Device_Label = "" Then Device_Label = " "
  132.             If Extended_Label = "" Then Extended_Label = " "
  133.             If Qty = "" Then Qty = " "
  134.             If Model_Num = "" Then Model_Num = " "
  135.             If Description = "" Then Description = " "
  136.             If Vendor = "" Then Vendor = " "
  137.             If CSFM_Num = "" Then CSFM_Num = " "
  138.  
  139.             data.AddNew
  140.             data!Handle = "'" & Handle
  141.             data!Blockname = Blockname
  142.             data!Tag = Tag
  143.             data!Address = Address
  144.             data!Label1 = Label1
  145.             data!Label2 = Label2
  146.             data!Device_Label = Device_Label
  147.             data!Extended_Label = Extended_Label
  148.             data!Qty = Qty
  149.             data!Model_Num = Model_Num
  150.             data!Description = Description
  151.             data!Vendor = Vendor
  152.             data!CSFM_Num = CSFM_Num
  153.             data.Update
  154.         Next i
  155.     Next Entity
  156.  
  157. ' Close Database
  158. ' --------------
  159.     data.Close
  160.     dbs.Close
  161.     Set data = Nothing
  162.     Set dbs = Nothing
  163.     ssnew.Delete
  164. End Sub
PS. Lines 78 through 121 can be done a lot more straightforwardly using a Select Case statement.
Apr 12 '15 #5
NeoPa

Thank you so much for your help...
BTW I took your advice and used the select case statement and it worked great...

Thanks Again
Apr 13 '15 #6
NeoPa
32,556 Expert Mod 16PB
I'm very happy that helped Tim :-)
Apr 16 '15 #7
I am so sorry to resurrect this but I have been looking for a way to expand it.

I've posted my new question in VBA to Populate Table (Update).
Jun 5 '16 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Vladimir | last post by:
Hello, I have a table in MS Access database. It has one field (with BYTE datatype) that has several properties set in Lookup tab of table Design View. Display Control = Combo Box. Row Source...
2
by: SenseForAll | last post by:
First please note I am a novice at VBA and not even that experienced with DAO/ADO and MS-SQL. Any assistance is appreciated. That said... I have an application written in Access w/ VBA. I need to...
5
by: premmehrotra | last post by:
I am using Microsoft Access 2000 and Oracle 9.2.0.5 on Windows 2000. When I export a table from Access to Oracle using ODBC I get error: ORA 972 identifier too long I think the error is because...
1
by: hubertSVK | last post by:
I would like you to help me ... I'm a beginning vb.net programmer and I need an advice ... I'm creating a DB application and I need to read data from the Access table with MS Jet 4.0 into Listview...
0
by: Glum | last post by:
I have created a DataSet in a VB.NET program and wanted to know if there is an easy way to push tables from the DataSet to a microsoft access database. What I am trying to do is take a...
3
by: adebiasio | last post by:
I have a procedure that uses ADOX to create a database with a table. That works fine. I then need to populate that table with a record but the code is not working. I believe that the database is...
3
by: equalive | last post by:
Hi guys, I'm having problem locking a table in Ms Access using VB6 code. Actually I have 3 tables. Assume table names is A, B, C. Following is the process. 1. Open table A and update table...
4
by: Terry Olsen | last post by:
I have an access database with a table that contains two columns: Computer_Name,User_ID The table contains 600 computer names. I need to populate the User_ID column with data from an SQL...
1
by: elena | last post by:
Hi, All I have flat text file with fixed record lenght w/o record delimiter, i need read this file and populate Access Database table, how can i achive this? The Record count is 1465321 records: I...
1
by: mbedford | last post by:
I've built a series of queries that bring together and process data from several different tables and queries and at the end a single query returns a single record based on a selection made in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.