Macro Not Working on AutoCad 2018

Macro Not Working on AutoCad 2018

Anonymous
Not applicable
1,852 Views
6 Replies
Message 1 of 7

Macro Not Working on AutoCad 2018

Anonymous
Not applicable

Hello, everyone!

I updated my AutoCad software from 2012 to 2018 and my macro just stopped working. 

I use it to get some information of the draw number in my database and fill a layout block with it. I made a little research and I think it happens because of some incompatibility issues of VBA 7.1 (x64). I dont have much knowledge in programming and I really don´t know how to "convert" this code to a x64 version (if this is the cause of my issue)

 

When i enter the draw number (Registro) and type enter, my autocad 2018 freezes and i need to use the task manager to close it.

 

Can anyone help me?

 

This is my form:

thebrunnalves_0-1592575099906.png

 

Here is the code of the form:

 

Option Explicit

Private Sub btnok_Click()
On Error Resume Next
Form.Delete
AlterarDados Me.cmpregistro
Unload Me
End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub CommandButton2_Click()

Dim oEnt As AcadEntity
Dim Pt(0 To 2) As Double
Dim oLWP As AcadLWPolyline
Dim oP As AcadPolyline
Dim oP3D As Acad3DPolyline
ThisDrawing.Utility.GetEntity oEnt, Pt, "Selecione o objeto"

If TypeOf oEnt Is AcadLWPolyline Then
Set oLWP = oEnt
' Do your thing here. The coordinates array has X value on all the even items and Y value on all the odd items in the array
ElseIf TypeOf oEnt Is AcadPolyline Then
Set oP = oEnt
' Do your thing here. The coordinates array has X value on 0, 3, 6 .... items and Y value on 1, 4, 7 .... and Z values on 2, 5, 8 .... in the array

ElseIf TypeOf oEnt Is Acad3DPolyline Then
Set oP3D = oEnt
' Do your thing here. The coordinates array has X value on 0, 3, 6 .... items and Y value on 1, 4, 7 .... and Z values on 2, 5, 8 .... in the array

Else
MsgBox "Twit. You need to select a polyline !!!", vbCritical
End If

End Sub

 

 

 

And the code of the  module i use:

 

Global Form As AcadBlockReference
Global Form2 As AcadBlockReference

Sub AlterarDados(varregistro As Variant)
On Error Resume Next
Dim SQL As String
Dim insertptForm(0 To 2) As Double


Dim wsp As Workspace
Dim Banco As Database
Dim tabela As Recordset
Dim tabela2 As Recordset
Dim varAttributes As Variant
Dim contador As Integer
Dim contador2 As Integer

'DBEngine.SystemDB = "c:\cib\GROUP\Sector.MDW"
'Set wsp = CreateWorkspace("c:\CIB\GROUP\Sector.MDW", "lucas", "enak-132010")
Set Banco = OpenDatabase("\\192.168.3.254\pcp2019\dbs\colineSpartacus.mdb")

Set tabela = Banco.OpenRecordset("select * from base_codProcesso where codProcesso=" & varregistro, dbOpenDynaset)

While Not tabela.EOF
'Define o ponto de inserção
insertptForm(0) = 0
insertptForm(1) = 0
insertptForm(2) = 0

If (tabela("Unidade") = "CJ") Then
Set Form = ThisDrawing.ModelSpace.InsertBlock(insertptForm, "FORMATOCJ", 1, 1, 1, 0)
varAttributes = Form.GetAttributes

varAttributes(0).TextString = ""
Set tabela2 = Banco.OpenRecordset("select * from autocad_folha_processo_CJ where ID=" & tabela("idProcesso") & " order by t_item_PCJ", dbOpenDynaset)

varAttributes(0).TextString = tabela2("t_cliente")
varAttributes(1).TextString = tabela2("n_qtde")
varAttributes(2).TextString = tabela2("n_of")
varAttributes(3).TextString = Format(tabela2("n_pesototal"), "##0.00")
varAttributes(4).TextString = tabela2("t_desenho")
varAttributes(5).TextString = tabela2("t_posicao")
varAttributes(6).TextString = tabela2("t_revisao")
varAttributes(7).TextString = tabela2("t_clientefinal")
varAttributes(8).TextString = tabela2("d_dataatual")
varAttributes(9).TextString = tabela2("t_projeto")
varAttributes(10).TextString = tabela2("t_tipagem")
varAttributes(11).TextString = tabela2("t_codprocesso")
varAttributes(12).TextString = tabela2("t_numre")
varAttributes(13).TextString = tabela2("t_descritivo")
varAttributes(14).TextString = tabela2("t_dimensoesembarque")
varAttributes(15).TextString = tabela2("t_revisao")
varAttributes(16).TextString = tabela2("d_dataatual")

varAttributes(20).TextString = Format(tabela2("n_pesototal"), "##0.00")
varAttributes(21).TextString = ""
varAttributes(22).TextString = tabela2("t_posicao")
varAttributes(23).TextString = tabela2("n_qtde")
varAttributes(24).TextString = tabela2("t_descritivo")
varAttributes(25).TextString = tabela2("t_obs")


'INICIA UM LOOPING PARA AS PCJS
contador = 0
contador2 = 1
While Not tabela2.EOF
Set Form2 = ThisDrawing.ModelSpace.InsertBlock(insertptForm, "LINHA" & contador2, 0.0454977, 0.0454977, 0.0454977, 0)
varAttributes = Form2.GetAttributes
varAttributes(6).TextString = Format(tabela2("n_peso_PCJ"), "##0.00")
varAttributes(5).TextString = tabela2("t_composicao_PCJ")
varAttributes(4).TextString = tabela2("t_item_PCJ")
varAttributes(3).TextString = tabela2("n_qtde_PCJ")
varAttributes(2).TextString = tabela2("t_material_PCJ") & " " & tabela2("t_obs_PCJ")
varAttributes(1).TextString = tabela2("t_codprocesso_PCJ")
varAttributes(0).TextString = tabela2("t_dimensoes_PCJ")
tabela2.MoveNext
contador = contador + 7
contador2 = contador2 + 1
Wend


Else
Set Form = ThisDrawing.ModelSpace.InsertBlock(insertptForm, "FORMULÁRIO", 1, 1, 1, 0)
varAttributes = Form.GetAttributes

Set tabela2 = Banco.OpenRecordset("select * from autocad_folha_processo where ID=" & tabela("idProcesso"), dbOpenDynaset)


If (UserForm1.semdesenho.Value = True) Then
varAttributes(16).TextString = "SEM DESENHO"

SQL = "update Processos_origem set SemDesenho = true, Desenho = 'SEM DESENHO' where ID =" & tabela("idProcesso")
Banco.Execute (SQL)

Else
varAttributes(16).TextString = ""
End If


If (tabela2("Uni") = "PC") Then
varAttributes(0).TextString = ""
varAttributes(1).TextString = ""
varAttributes(2).TextString = ""
varAttributes(3).TextString = ""
varAttributes(9).TextString = ""
Else
varAttributes(0).TextString = tabela2("DesenhoCJ")
varAttributes(1).TextString = tabela2("qtdeCJ")
varAttributes(2).TextString = tabela2("RegCJ")
varAttributes(3).TextString = tabela2("Item")
varAttributes(9).TextString = tabela2("codkit")
End If

varAttributes(4).TextString = Format(tabela2("PesoTotal"), "##0.00")
varAttributes(5).TextString = tabela2("Comprimento")
varAttributes(6).TextString = tabela2("Largura")
varAttributes(7).TextString = tabela2("qtdee")
varAttributes(8).TextString = tabela2("identificacao")

varAttributes(10).TextString = tabela2("Uni")
varAttributes(11).TextString = tabela2("numOF")
varAttributes(12).TextString = tabela2("numRegistro")
varAttributes(13).TextString = tabela2("qtded")
varAttributes(14).TextString = tabela2("composicao")
varAttributes(15).TextString = tabela2("material")
End If

tabela.MoveNext
Wend


tabela.Close
tabela2.Close
Banco.Close

End Sub

Sub openForm2()
UserForm1.Show
End Sub

 

Thanks!!

0 Likes
1,853 Views
6 Replies
Replies (6)
Message 2 of 7

vince.krueger
Advocate
Advocate

Freezes can be difficult to troubleshoot.  Normally they are caused by endless loops or the code waiting on another object or application.

 

Two things to look at / for

1. Why are you doing a Form.Delete right away?

2. Add a breakpoint in the code on the button and single step through the code to find out where it gets stuck.

 

I would normally also recommend adding some error handling code (will not catch freezes, but lets you better handle other errors).

0 Likes
Message 3 of 7

grobnik
Collaborator
Collaborator

Hi @Anonymous , could be very useful if you use debug, inserting a break point in the code with F9 function key.

Perhaps first of all inside the form immediately into confirmation command button code (I guess there is a Keypress event), in addition insert another one where the code will follow after the drawing number entering.

In this case the procedure will be stopped and wait for F8 functional key for go ahead step by step.

You can skip part of code that you are sure about functionality in the same way, insert a break point with F9, run again with F5, and F8 step by step after break point.

So you will be able to define exactly where the code hangs.

We have not your database here, and we do not have your same situation so could be little bit hard to find the part of code not fully compliant with new release.

Probably there are some reference library that are recalled by some function, or something else.

This is my approach, perhaps somebody in the forum could have a different one.

0 Likes
Message 4 of 7

norman.yuan
Mentor
Mentor

Since your VBA program needs to connect to MS Access database (*.mdb), so, yes, your issue is very likely because of AutoCAD's VBA since 2015 being 64-bit, if the AutoCAD is 64-bit.

 

You need to install 64 bit Microsoft Access DB Engine (unless the CAD computer has 64-bit MS Access installed), which is a free download from MS. Note, it is very tricky to install 64-bit MS Access DB Engine, if the computer has 32-bit MS Office suite installed. You can search the net on how to download/install 64-bit MS Access DB Engine. 

 

By the way, if the data volume used by your VBA application is relatively small, IMO, it is time to move away from using MS Access database to avoid the trouble of having install extra piece of significant software (MS Access DB Engine). If the VBA application is mission-critical, you might even want to migrate the VBA app to AutoCAD .NET API.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 7

lena.talkhina
Alumni
Alumni

Welcome to the Autodesk Community @Anonymous !

 

Great to see you here on Visual Basic Customization forum.

Hope you will find a solution soon!

Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям! | Do you find the posts helpful? "LIKE" these posts!
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.



Лена Талхина/Lena Talkhina
Менеджер Сообщества - Русский/Community Manager - Russian

0 Likes
Message 6 of 7

Anonymous
Not applicable

Thanks for the replies. I'll try to install the "64 bit Microsoft Access DB Engine" (I use 32bit office version, i think it will be tricky indeed) and analize the code using step by step debug. I'll post the results later on.

Message 7 of 7

vince.krueger
Advocate
Advocate

Another idea is to ditch Access entirely.  I use SQL Server Express (which is free) for all of my database needs.  The tables can be imported easily, and the SQL interface driver is cleaner to use (and has much fewer compatibility issues than Access).

 

Keep in mind also that AutoCAD uses Access also, and from experience it is pretty easy to break the AutoCAD connections when trying to upgrade / convert Access (like Norman said, be careful and follow the directions completely)

0 Likes