TheSwamp
Code Red => VB(A) => Topic started by: David Hall 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?
-
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
-
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.
-
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?
-
Could you use an enum in place of the const?
-
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?
-
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
-
Add an invisible control and as long as the form isn't unloaded you have your value.
-
makes sense
-
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.
Public Enum CloseMode
CloseMode_Close
CloseMode_Cancel
End Enum
-
I really hate to beat a dead horse, but I think the verbage is throwing me off. Here is one of the modules
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?
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
-
And would the enum go in the form init code? I realize I posted a module as an example
-
I'll look later when I have some time
but make the const private and it should work fine.
-
I really hate to beat a dead horse, but I think the verbage is throwing me off. Here is one of the modules
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?
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.
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.
-
Hi David,
I think you will be served better splitting GUI and "business" logic. ... particularly when you do more work in OOP languages.
-
Thanks Kerry, I'm thinking that way as well
-
Sometimes I get so focused on how to achieve something, that I forget to step back and ask myself if doing that thing is actually a good idea. Looks like I did it again. :D
-
............. Looks like I did it again. :D
I had to get a T-shirt made saying exactly that, and for the same reason Chuck. :lol:
-
Sometimes I get so focused on how to achieve something, that I forget to step back and ask myself if doing that thing is actually a good idea.
I have a similiar problem. I get set in my mind on how something needs to be done, that I fail to see the better way or easier way.