Author Topic: Module splitting or Form.Code  (Read 5928 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4073
Module splitting or Form.Code
« on: March 09, 2007, 11:16:27 AM »
I have a question about which is better (a relative term I know), all code in the form or split out into modules?  I have a form with a bunch of buttons, and when a user picks a button, it calls the module that applies.  The problem I'm running into has to do with scope.  I have a utility function that sets the current layer to a variable, changes the layer, does work, and changes layer back.  My problem seems to be that the variable (declared globally) seems to be losing its value.  I know that if I kept all the code inside 1 module, I wouldn't/shouldn't have scope issues.  Is there anything wrong with putting all the code in one place?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

hendie

  • Guest
Re: Module splitting or Form.Code
« Reply #1 on: March 12, 2007, 04:38:14 AM »
there's nothing wrong with putting all code into one place, like a form etc. It all depends upon how portable and modular you want your code to be.
When I first started coding vba I tended to put all my code into one place, 99% of the time into the form module.
However, I now tend to write as modular as I can and use the form only for directly related events

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Module splitting or Form.Code
« Reply #2 on: March 12, 2007, 10:10:07 AM »
Since I like 'Break on Unhandled Errors' instead of   'break in class module' I try to write as much as I can in standard modules. It also seems quicker in the long run to divorce some subs from the form, as in first make it do what it needs to do, then fit it into the form. At this point you are only error trapping for the form itself.
Because it has been written this way, it's more likely to be useful for another purpose as well, thus the tempation to leave it in a standard module.
So I guess that would be the main reason for me to not put all the code in a form, if I can use some of it elsewhwere, leave that in a module.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4073
Re: Module splitting or Form.Code
« Reply #3 on: March 12, 2007, 02:16:05 PM »
my problem has to do with scope.  I tried to write a utility function to handle layer/osnap values, but when I jump from form to module and back, im losing values.  Now before you say handle all that in the module, I was trying to NOT duplicate the same code in every module, thus the utility function.  Also, I cant delcare a const value in the form, only a module.  Any ideas?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Chuck Gabriel

  • Guest
Re: Module splitting or Form.Code
« Reply #4 on: March 12, 2007, 02:20:02 PM »
Could you use an enum in place of the const?

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4073
Re: Module splitting or Form.Code
« Reply #5 on: March 12, 2007, 02:22:06 PM »
Hadn't thought to try that.  Would I declare the enumuration in the form initialize, and then reference it in my error checking select case?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4073
Re: Module splitting or Form.Code
« Reply #6 on: March 12, 2007, 02:22:49 PM »
The constants were from RR's error checking code for user pushing ESC.  If there is a better way to do this, I might be able to toss the const away for good
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Module splitting or Form.Code
« Reply #7 on: March 12, 2007, 02:24:01 PM »
Add an invisible control and as long as the form isn't unloaded you have your value.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4073
Re: Module splitting or Form.Code
« Reply #8 on: March 12, 2007, 02:35:27 PM »
makes sense
« Last Edit: March 12, 2007, 02:46:43 PM by CmdrDuh »
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Chuck Gabriel

  • Guest
Re: Module splitting or Form.Code
« Reply #9 on: March 12, 2007, 03:33:22 PM »
Hadn't thought to try that.  Would I declare the enumuration in the form initialize, and then reference it in my error checking select case?

You just put something like below at file scope in the form code.

Code: [Select]
Public Enum CloseMode
  CloseMode_Close
  CloseMode_Cancel
End Enum

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4073
Re: Module splitting or Form.Code
« Reply #10 on: March 12, 2007, 05:30:40 PM »
I really hate to beat a dead horse, but I think the verbage is throwing me off.  Here is one of the modules
Code: [Select]
Option Explicit
Public Const VK_ESCAPE = &H1B
Public Const VK_LBUTTON = &H1
Public Const VK_SPACE = &H20
Public Const VK_RETURN = &HD
Public Const VK_LEFT = &H25

Public Declare Function GetAsyncKeyState Lib "user32" _
                                         (ByVal vKey As Long) As Integer
Public Function AddBKR()
      Dim dimsc As Integer
      dimsc = ThisDrawing.GetVariable("DIMSCALE")
      Dim InsPT As Variant
      Dim intOsMode As Integer
      Dim dblRotation As Double
      Dim objBlockRef As AcadBlockReference

      '************** Esc Code *************
      Dim varCancel As Variant
      On Error GoTo Err_Control
      '************** Esc Code *************

      Call LayerSet("3D-EQPM", acRed)
      intOsMode = ThisDrawing.GetVariable("OSMODE")
      ThisDrawing.SetVariable "OSMODE", 32
      InsPT = ThisDrawing.Utility.GetPoint(, "Pick Insertion Point: ")
      ThisDrawing.SetVariable "OSMODE", 512
      dblRotation = ThisDrawing.Utility.GetAngle(InsPT, "Pick Cabinet Side: ")
      Set objBlockRef = ThisDrawing.ModelSpace.InsertBlock(InsPT, Path & "breaker.dwg", 1, 1, 1, dblRotation)
      '************** Esc Code *************
Exit_Here:
      LayerReSet
      ThisDrawing.SetVariable "OSMODE", intOsMode
      ThisDrawing.SetVariable "DIMSCALE", dimsc
      ThisDrawing.SetVariable "INSUNITS", 1
      Exit Function
Err_Control:
      Select Case Err.Number
            Case -2147352567
                  'Debug.Print Err.Number, Err.Description
                  varCancel = ThisDrawing.GetVariable("LASTPROMPT")
                  If InStr(1, varCancel, "*Cancel*") <> 0 Then
                        If GetAsyncKeyState(VK_ESCAPE) And 8000 > 0 Then
                              Err.Clear
                              Resume Exit_Here
                        ElseIf GetAsyncKeyState(VK_LBUTTON) > 0 Then
                              Err.Clear
                              Resume
                        End If
                  Else
                        If GetAsyncKeyState(VK_SPACE) Then
                              Resume Exit_Here
                        End If
                        'Missed the pick, send them back!
                        Err.Clear
                        Resume
                  End If
            Case Else
                  MsgBox Err.Description
                  Resume Exit_Here
      End Select
End Function

what your saying is change to this?
Code: [Select]
Option Explicit
Public Enum CloseMode
  CloseMode_Close=???
  CloseMode_Cancel=&H1B  ' Added here and I would reference CloseMode.CloseMode_Cancel  ?
End Enum


Public Declare Function GetAsyncKeyState Lib "user32" _
                                         (ByVal vKey As Long) As Integer
Public Function AddBKR()
      Dim dimsc As Integer
      dimsc = ThisDrawing.GetVariable("DIMSCALE")
      Dim InsPT As Variant
      Dim intOsMode As Integer
      Dim dblRotation As Double
      Dim objBlockRef As AcadBlockReference

      '************** Esc Code *************
      Dim varCancel As Variant
      On Error GoTo Err_Control
      '************** Esc Code *************

      Call LayerSet("3D-EQPM", acRed)
      intOsMode = ThisDrawing.GetVariable("OSMODE")
      ThisDrawing.SetVariable "OSMODE", 32
      InsPT = ThisDrawing.Utility.GetPoint(, "Pick Insertion Point: ")
      ThisDrawing.SetVariable "OSMODE", 512
      dblRotation = ThisDrawing.Utility.GetAngle(InsPT, "Pick Cabinet Side: ")
      Set objBlockRef = ThisDrawing.ModelSpace.InsertBlock(InsPT, Path & "breaker.dwg", 1, 1, 1, dblRotation)
      '************** Esc Code *************
Exit_Here:
      LayerReSet
      ThisDrawing.SetVariable "OSMODE", intOsMode
      ThisDrawing.SetVariable "DIMSCALE", dimsc
      ThisDrawing.SetVariable "INSUNITS", 1
      Exit Function
Err_Control:
      Select Case Err.Number
            Case -2147352567
                  'Debug.Print Err.Number, Err.Description
                  varCancel = ThisDrawing.GetVariable("LASTPROMPT")
                  If InStr(1, varCancel, "*Cancel*") <> 0 Then
                        If GetAsyncKeyState(VK_ESCAPE) And 8000 > 0 Then
                              Err.Clear
                              Resume Exit_Here
                        ElseIf GetAsyncKeyState(VK_LBUTTON) > 0 Then
                              Err.Clear
                              Resume
                        End If
                  Else
                        If GetAsyncKeyState(VK_SPACE) Then
                              Resume Exit_Here
                        End If
                        'Missed the pick, send them back!
                        Err.Clear
                        Resume
                  End If
            Case Else
                  MsgBox Err.Description
                  Resume Exit_Here
      End Select
End Function
« Last Edit: March 12, 2007, 05:33:23 PM by CmdrDuh »
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4073
Re: Module splitting or Form.Code
« Reply #11 on: March 12, 2007, 05:32:20 PM »
And would the enum go in the form init code?  I realize I posted a module as an example
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Module splitting or Form.Code
« Reply #12 on: March 12, 2007, 07:09:52 PM »
I'll look later when I have some time
but make the const private and it should work fine.

Chuck Gabriel

  • Guest
Re: Module splitting or Form.Code
« Reply #13 on: March 13, 2007, 09:30:29 AM »
I really hate to beat a dead horse, but I think the verbage is throwing me off.  Here is one of the modules
Code: [Select]
Option Explicit
Public Const VK_ESCAPE = &H1B
Public Const VK_LBUTTON = &H1
Public Const VK_SPACE = &H20
Public Const VK_RETURN = &HD
Public Const VK_LEFT = &H25

what your saying is change to this?
Code: [Select]
Option Explicit
Public Enum CloseMode
  CloseMode_Close=???
  CloseMode_Cancel=&H1B  ' Added here and I would reference CloseMode.CloseMode_Cancel  ?
End Enum

Not quite.  I didn't mean you should use exactly what I posted.  Just that general form.  Here is something more suited to your particular needs.

Code: [Select]
Public Enum VirtualKeys
  ESCAPE = &H1B
  LBUTTON = &H1
  SPACE = &H20
  RETURN = &HD
  LEFT = &H25
End Enum

By the way, in the other example I posted, I didn't explicitly assign a value to each member of the enumeration because it would have been irrelevant in the code I plucked that from.

And would the enum go in the form init code?  I realize I posted a module as an example

No.  The enum should go at file scope (outside of any procedure).

By the way.  I think Bryco is right.  Just changing Public Const to Private Const would be a lot more expedient.  Sorry if I've led you on a wild goose chase.
« Last Edit: March 13, 2007, 12:42:05 PM by Chuck Gabriel »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Module splitting or Form.Code
« Reply #14 on: March 13, 2007, 07:19:08 PM »
Hi David,

I think you will be served better splitting GUI and "business" logic. ... particularly when you do more work in OOP languages.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.