Author Topic: VBA - How do you maintain your code?  (Read 7259 times)

0 Members and 1 Guest are viewing this topic.

Grrr1337

  • Swamp Rat
  • Posts: 812
VBA - How do you maintain your code?
« on: September 21, 2017, 06:15:22 PM »
Hello guys,
This is a newbie'ish question from a lisper (me) regardless maintaining your code in VBA:

As some of you know - in LISP its very simple, you just use a text editor to write and edit your code, then a few months/years might pass and you still can reopen that .lsp file and improve or modify it. Ofcourse optional would be to comple a .vlx file and keep the .lsp file for further improvements.

But now I'm trying to getting started with VBA and this issue is the first drawback for me:
Obviously you create few modules/userforms and write some codes inside, then save the whole project as .dvb file (ctrl+s).
After that you close AutoCAD and start a new session again, deciding to reopen the project you previously wrote, but the only way I found is to try importing the .dvb file so I'm ending up with a compiled s*it...

So whats the proper way to maintain VBA projects (codes) ?
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: VBA - How do you maintain your code?
« Reply #1 on: September 21, 2017, 06:55:16 PM »
I tend to export to BASIC (.bas) files, which are text format.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: VBA - How do you maintain your code?
« Reply #2 on: September 21, 2017, 07:13:56 PM »
If you reference the runtime for Visual Basic Applications Extensibility (I'm posting from my phone - so it may not be named exactly that - but should be close) you can write code to iterate over all projects and code panes etc and export the code to .bas and .cls files as applicable. I do this for excel especially -- every time I press {ctr}{b} it creates a dated folder hierarchy with exported code. O.o
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: VBA - How do you maintain your code?
« Reply #3 on: September 21, 2017, 08:02:00 PM »
I tend to export to BASIC (.bas) files, which are text format.

I just noticed that one could export a single module/userform, Thanks Lee!


If you reference the runtime for Visual Basic Applications Extensibility (I'm posting from my phone - so it may not be named exactly that - but should be close) you can write code to iterate over all projects and code panes etc and export the code to .bas and .cls files as applicable. I do this for excel especially -- every time I press {ctr}{b} it creates a dated folder hierarchy with exported code. O.o

Umm.. I see, so a functions to export/import the project hierarchy are required (I thought such options have to be built-in already <duh>).
Thanks, Michael! [BTW tablet might be more comfortable for writing] :)



Guess I'll stick to scripting objects by using lisp for a while, and use the VBAIDE editor (aswell some internet VBA codes) to guide me thru my object-scripting tasks.
I just hope that bit by bit I'll get used with VBA (although my recent problems are lisp solvable) so that was my first VBA struggle.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Re: VBA - How do you maintain your code?
« Reply #4 on: September 22, 2017, 04:34:46 AM »
Dont forget you can make big VBA code with lots of functions so you only have 1 code version yes you can do same in lisps also. Your forms re DCL are saved in the file as well, not to mention the forms are interactive rather than write code and test.

Nice thing is you can group your code under a heading for a common theme and jump to just that group of code. Where as in lisp you would have to say scroll down to the top defun level to see all the functions in that group.

Lee can you do this in VLIDE?
A man who never made a mistake never made anything

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: VBA - How do you maintain your code?
« Reply #5 on: September 22, 2017, 04:32:43 PM »
Hi BIGAL, just sharing impressions from my perspective:

Your forms re DCL are saved in the file as well, not to mention the forms are interactive rather than write code and test.

Well the lisp alternative (obviously) is only to create the dcl file on-the-fly.
I agree that it seems alot easier to work with the userforms - regarding the flexible creation of the dialog and asigning subroutines to each tile.
But the main advantage I see, is that you have tiles that are not available with the standard DCL:
- ComboBox
- TabStrip
- Multipage
Ofcourse you could simulate a similair behaviour (for instance using buttons and nested dialogs, that represent the "Multipage" control).
Also there is a program that allow to generate the (standard) DCL code, while creating the dialog similarly to VBA's userforms - But I still havent tried it.

Nice thing is you can group your code under a heading for a common theme and jump to just that group of code. Where as in lisp you would have to say scroll down to the top defun level to see all the functions in that group.

Lee can you do this in VLIDE?

I prefer to use NP++ along with the "Indent By Fold" plugin for quick and easy formatting, and just double clicking on a word to highlight all of its instances (defun).
Another thing is a helpful structure I keep when I have defuns localised inside my main defun:
Code - Auto/Visual Lisp: [Select]
  1. (defun C:TestMain ( / SubDefun1 SubDefun2 SubDefun3 *error* var1 var2 var3 var3 ) ; <- note that *error* separates my localised defuns from my localised variables
  2.  
  3.   (defun SubDefun1 ...)
  4.   (defun SubDefun2 ...)
  5.   (defun SubDefun3 ...)
  6.  
  7.   (defun *error* ...) ; <- after this defun I know that my main code starts
  8.  
  9.   ; And now here starts the main code:
  10.   (setq
  11.     var1 ...
  12.     var2 ...
  13.     var3 ...
  14.   )
  15. )
  16.  

And another important rule was marko_ribar's advice on my thread about handling very big/long programs.

...but more important is to be sure what routine is doing from beginning to the end and to spot where such problematic portion of code was written to correctly...

Since then I never get lost in my LISP code and maintain it easily, so my debugging tasks were limited only to a doubtful evaluations (the rest was written from experience, intuition and examples from others).
So thats the reason I never used VLIDE, but rather NP++ (I don't feel comfortable writing codes from scratch in VLIDE). However I use oftenly VLIDE's console to check/trace evaluations I'm not sure about their returns and will they error-out.

(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

57gmc

  • Bull Frog
  • Posts: 358
Re: VBA - How do you maintain your code?
« Reply #6 on: September 26, 2017, 11:06:46 AM »
But now I'm trying to getting started with VBA and this issue is the first drawback for me:
Obviously you create few modules/userforms and write some codes inside, then save the whole project as .dvb file (ctrl+s).
After that you close AutoCAD and start a new session again, deciding to reopen the project you previously wrote, but the only way I found is to try importing the .dvb file so I'm ending up with a compiled s*it...

So whats the proper way to maintain VBA projects (codes) ?
All vba has to be compiled to run. I don't see what you are worried about. If you have an older dvb, opening it in a newer version of vba will automatically upgrade it. I've never had a problem with leaving my code in a dvb for the last 20 years.