Author Topic: Example code for retrieving TextBox text  (Read 3019 times)

0 Members and 1 Guest are viewing this topic.

Arizona

  • Guest
Re: Example code for retrieving TextBox text
« Reply #15 on: October 18, 2006, 06:28:35 AM »
WoW, this is beautifully written, Michael!
I really love the detailed comments as well as your sense of humor!
Thank you for sharing and for helping me to continue to learn as well!

Mark

  • Custom Title
  • Seagull
  • Posts: 28685
Re: Example code for retrieving TextBox text
« Reply #16 on: October 18, 2006, 07:36:33 AM »
Quick question. "Private myExitState As Boolean" declares the variable "myExitState" as a boolean and is only accessible to frmMain, correct?
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17446
Re: Example code for retrieving TextBox text
« Reply #17 on: October 18, 2006, 07:46:47 AM »
WoW, this is beautifully written, Michael!

The wow is that you think so -- thanks Arizona!!


I really love the detailed comments ...

<Michael is smiling inside>.

...as well as your sense of humor!

I was funny? <reviewing post ...>

Thank you for sharing and for helping me to continue to learn as well!

Truly my pleasure -- we're all in a state of share and learn -- this is the little bit I'm delusional enough to think I know well enough to share -- and but a teeny, teeny fragment compared to the stuff I've learned at the swamp!!

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

MP

  • Seagull
  • Posts: 17446
Re: Example code for retrieving TextBox text
« Reply #18 on: October 18, 2006, 07:48:09 AM »
Quick question. "Private myExitState As Boolean" declares the variable "myExitState" as a boolean and is only accessible to frmMain, correct?

Yes sir!

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

MP

  • Seagull
  • Posts: 17446
Re: Example code for retrieving TextBox text
« Reply #19 on: October 18, 2006, 08:16:55 AM »
Quick question. "Private myExitState As Boolean" declares the variable "myExitState" as a boolean ...

Another tactic might be to exploit the MsgBox constants, and return the exit state as a function of the applicable constant, i.e. vbOKOnly, vbOKCancel ... I was just trying to keep the example relatively simple.

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Troy Williams

  • Guest
Re: Example code for retrieving TextBox text
« Reply #20 on: October 18, 2006, 08:17:35 AM »
Hi MP, nice work!

I modified your sub main code, particularly the error handler. I was wondering if I could get your opinion on it? I basically rearranged the error handler to a form that I have been using for a few years now and it seems to perform well. There is one big cavet with this type of strategy - you can not have any errors occur in the error handler itself  :-)

Code: [Select]
Option Explicit

Public Sub Main()   
    Dim myForm As frmMain

    ''' moved the On Error statement as close to the begining of the routine so that any possible error would pass through the error handler
    On Error GoTo ErrHandler

    Set myForm = New frmMain
   
    myForm.UserText = "Mark's great adventure."   
    myForm.Show   
   
If Not (myForm Is Nothing) Then           
        If myForm.ExitState Then       
            ''  access the UserText Property we exposed           
            MsgBox _
                "User pressed [Ok]." & vbCrLf & vbCrLf & _
                "UserText = <" & _
                myForm.UserText & ">"               
        Else       
            MsgBox "User pressed [Cancel]."           
        End If
    End If '''Moved this statement out of the  errorhandler

'''Moved the OutOfHere label so that it doesn't need to be called explicitly if nothing went wrong, the code sort of falls into it :)
OutOfHere:   
if Not myForm is nothing then
    Unload myForm       
        Set myForm = Nothing               
    end if

Exit Sub
ErrHandler:
'''Changed from an if statement to a select case so that more errors can be easily accomodated for in the future
        Select Case Err.Number
Case -2147418105    
            MsgBox _
                "The user closed the form via the " & _
                "form's control box."
               
        Case Else       
            ''  Uhhh, what the h3ll? An error we didn't
            ''  anticipate. Let's display the error
            ''  description.
           
            MsgBox Err.Description           
        End Select

        Err.Clear       
        Resume OutOfHere           
End Sub

The biggest change from your original method is placing the "OutOfHere" label before the "ErrHandler" label and putting a select case statement within the error handler itself. My theory on setting up the error handler in this fashion was that I was able to make a template and simply copy and paste the error handling code into my routines (with a few changes like exit sub or exit function).

I would simply paste this into the body of the routine and go from there:
Code: [Select]
'Variable Declaration goes before the On Error Statement

On Error Goto ErrHandler

'Code goes here

ExitHere:   
'Perform Cleanup here:

Exit Sub 'This needs to change depending on the type of routine - sub or function
ErrHandler:
        Select Case Err.Number
'create a case statement to catch a specfic error number
        Case Else                   
            MsgBox Err.Number & ": " & Err.Description           
        End Select

        Err.Clear       
        Resume ExitHere



MP

  • Seagull
  • Posts: 17446
Re: Example code for retrieving TextBox text
« Reply #21 on: October 18, 2006, 09:00:52 AM »
Thanks for the kind words Troy, means a lot coming from you.

:)

Error handling is actually not a trivial subject, and my attempting to discuss it now <on the 2 +/- hours sleep I'm running on this morning> is probably unwise at best. However, I don't wish to leave your post dangling so I'll attempt something of a response.

Off the top of my head what strikes me first is the placement of the On Error Goto ... statement. As is in theory it could catch potential design errors (i.e. an error with the form, or no form at all) rather than execution errors, i.e. what happens after the form has been given life. Is that desired behavior? Is that likely to happen? I don't know; I'll have to muse a bit on that.

The subtitle to the preceding comment is that I <generally> try to put error handing code right where an error is likely to be thrown (where defensive coding cannot prevent) rather than a blanket handler, but that may be a flaw in my design rationale / style, not yours.

Code sporting a label in the middle of an If .. End If block <as mine did>? Probably poor form, given that it's easy enough to code a cleaner alternatate.

Branching on the error number via the select statement <as yours did>? Flexible: I should have shown same.

An aside, to be semi complete on error handing one should address turning on and off error handing, that is, one should turn off handling (via On Error Goto 0) if there is indeed no chance of error in a spread of code, especially if there is any intensive (e.g. looping) logic going on, restoring error handling if it becomes applicable again.

Finally (no try-catch ... pun intended) I tend to be shy of error templates as they tend to make me a bit lazy, i.e. relying on it rather than the specifics of the present implementation, but again, this is me recognizing my habits and shortcoming, not yours. The alternate view is that the template approach makes for constancy -- a good thing.

So as an overall strategy? I think what you posted is probably better than what I had. It's clear, consistant and logical -- what's not to like?

<kinda rambly, sorry?>

:)
« Last Edit: October 18, 2006, 09:02:28 AM by MP »
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: Example code for retrieving TextBox text
« Reply #22 on: October 18, 2006, 09:13:39 AM »
I to realize that I am just a hack :-(  Great coding MP, I learned something, and its not even sun up yet.
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

Troy Williams

  • Guest
Re: Example code for retrieving TextBox text
« Reply #23 on: October 18, 2006, 11:01:00 AM »
MP,
I appreciate your input!

Error handling is indeed a large topic and potential can o' worms  :-)

The error handling method that I have presented is a little simplified from what I use. Normally I report back a substantial amount of information about the error and I have a custom logging class that will email me the information.

I have found that error handling (similar to what I presented) at the function/sub level works well in practice (for me). The other thing that I have found by wrapping the working code in an error handler (similar to a try catch block) the readability of the code is improved - which is important to me.



MP

  • Seagull
  • Posts: 17446
Re: Example code for retrieving TextBox text
« Reply #24 on: October 18, 2006, 01:12:58 PM »
I to realize that I am just a hack :-(  Great coding MP, I learned something, and its not even sun up yet.

Wish y'all wouldn't dismiss your own abilities and talents and disproportionately elevate mine -- we're all in this school together -- and take turns teachin', learnin', sharin' <sounds like a Journey song>  ... !

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Mark

  • Custom Title
  • Seagull
  • Posts: 28685
Re: Example code for retrieving TextBox text
« Reply #25 on: October 18, 2006, 01:18:01 PM »
-- and take turns teachin', learnin', sharin' <sounds like a Journey song>  ... !

< Larry the cable guy >That's funny right there

Have a nice nap Michael? :-)
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17446
Re: Example code for retrieving TextBox text
« Reply #26 on: October 18, 2006, 01:26:55 PM »
MP,
I appreciate your input!

Thanks Troy, wish I'd had more sleeps before the post your refer to, might have been a litle more coherent!

The error handling method that I have presented is a little simplified from what I use. Normally I report back a substantial amount of information about the error and I have a custom logging class that will email me the information.

I use the logging class technique too! Mine isn't clever enough to email me, it just collects errors and returns them in a message box at the end of program execution (if any messages were collected). This is for an app that has to execute many different steps, regardless the success of any individual step, and for which "as generated" error reporting would be inefficient (and annoying). So each method is passed the sole instance of the logger, which is interogated at the end of the program.

But I digress! <spank>

I have found that error handling (similar to what I presented) at the function/sub level works well in practice (for me). The other thing that I have found by wrapping the working code in an error handler (similar to a try catch block) the readability of the code is improved - which is important to me.

And of paramount importance to me too. Sure hope that shows!

Thanks for sharing your thoughts Troy. Hope you didn't have any impression other than I thought your post, and strategy was a good one. I take notice every time you post -- always good, insightful techniques and info. Keep up the good works Troy!

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

MP

  • Seagull
  • Posts: 17446
Re: Example code for retrieving TextBox text
« Reply #27 on: October 18, 2006, 01:28:06 PM »
Have a nice nap Michael? :-)

Not long enough (1 sleep cycle; 90 minutes) but I'll take what I can get!

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox