|
Prev: vb6 function
Next: forms editor not configured
From: dm4714 on 23 Mar 2005 10:10 I have a VB6 application that updates a SQL Server database. The program works fine, but sometimes I receive SQL errors due to bad input or misisng information. I'm exeuting a bunch of SP that return various information. I have an Error Handler in my function and it is trapping for errors. My problem is, however, that I've changed my mouse pointer to an hour glass while the SQL queries are running. When I do get an error and the error handler is invoked, control is returned to my main form and the mouse pointer is still an hour glass. Now I realize I can simply put it back to normal within my error handler... but is this the best way to do this? If I have various checks for valid data within my function and attempt to do an "Exit Sub" instead of using the error handler, is there a recommended way to handle resetting the mouse pointer back? Perhaps adding something to my main form activate event? Any ideas/recommendation/best practices would be appreciated.
From: Ken Halter on 23 Mar 2005 10:39 "dm4714" <spam(a)spam.net> wrote in message news:eEs$uo7LFHA.436(a)TK2MSFTNGP09.phx.gbl... >I have a VB6 application that updates a SQL Server database. The program >works fine, but sometimes I receive SQL errors due to bad input or misisng >information. I'm exeuting a bunch of SP that return various information. > > I have an Error Handler in my function and it is trapping for errors. > > My problem is, however, that I've changed my mouse pointer to an hour > glass while the SQL queries are running. When I do get an error and the > error handler is invoked, control is returned to my main form and the > mouse pointer is still an hour glass. Most of my apps do something like this.... when a user does something that would take a while, Call EnableControls(False)... disables controls and sets the mouse pointer. When it's done, error or not, Call EnableControls(True)... enables controls and sets the mouse pointer. '=============== Option Explicit 'this represents something a user would actually do (besides click on the form <g>) Private Sub Form_Click() 'fwiw, CodeSmart 2005 adds the error handling code with a single keystroke (including the horizontal "lines") Const ErrorLocation = "Form_Click" On Error GoTo ErrorTrap '============================================================== Dim f As Single 'Disable the controls Call EnableControls(False) 'do some work f = Timer + 3 '3 seconds Do While f > Timer DoEvents Loop '============================================================== Terminate: 'Enable the controls Call EnableControls(True) Exit Sub ErrorTrap: MsgBox "Error " & Err.Number & vbCrLf & Err.Description Debug.Assert False 'Stops here in the IDE Resume Terminate Resume 'In the IDE, jump here to find the line that raised the error. End Sub Private Sub EnableControls(State As Boolean) Dim c As Control Me.MousePointer = IIf(State, vbArrow, vbHourglass) For Each c In Controls c.Enabled = State Next DoEvents End Sub '=============== -- Ken Halter - MS-MVP-VB - http://www.vbsight.com Sign up now to help keep VB support alive - http://classicvb.org/petition Please keep all discussions in the groups..
From: Larry Serflaten on 23 Mar 2005 11:30 "dm4714" <spam(a)spam.net> wrote > My problem is, however, that I've changed my mouse pointer to an hour glass > while the SQL queries are running. When I do get an error and the error > handler is invoked, control is returned to my main form and the mouse > pointer is still an hour glass. > > Now I realize I can simply put it back to normal within my error handler... > but is this the best way to do this? > > If I have various checks for valid data within my function and attempt to do > an "Exit Sub" instead of using the error handler, is there a recommended way > to handle resetting the mouse pointer back? > > Perhaps adding something to my main form activate event? > > Any ideas/recommendation/best practices would be appreciated. You happened to hit on a topic that was beaten to death when .Net first came out. .Net did not (easily) support deterministic finalization (meaning objects were not destroyed when all references were released as happens in VB6. In ..Net the Garbage Collector has to come by to do the clean up.) One example that was used to show why DF was a good thing was the mouse pointer dilema you have found yourself in the middle of. You could add a label to your routine's exit point and place the mouse code there, making sure all exits go through that one exit point. But what if you want to raise the error, or handle it, etc... You end up with mulitple statements no matter how you go about it.... A clever fix is to create a class that you instantiate at the start of your routine. No matter how you exit that routine, VB will drop the reference to that class as it exits. You can use that fact to your advantage.... ' [HourGlass Class code] Private Sub Class_Initialize() Screen.MousePointer = vbHourglass End Sub Private Sub Class_Terminate() Screen.MousePointer = vbDefault End Sub When that object is created, it will set the mousepointer to an hourglass. And, when that object is destroyed, it will return the mousepointer to its default value. To use it, you simply create an instance of that object at the start of the routine and that's it. No matter how the routine exits, VB will drop the reference causing the object to be destroyed: Private Sub Form_Click() Dim Cursor As HourGlass Set Cursor = New HourGlass ' Any and all code.... MsgBox "Waiting..." 'to see it was set to HourGlass End Sub See it that might suit your needs..... LFS
From: YYZ on 23 Mar 2005 12:10 "Larry Serflaten" <serflaten(a)usinternet.com> wrote in message news:eGjd5U8LFHA.4092(a)tk2msftngp13.phx.gbl... > > A clever fix is to create a class that you instantiate at the start of > your routine. > No matter how you exit that routine, VB will drop the reference to that > class > as it exits. You can use that fact to your advantage.... <snip> > Private Sub Form_Click() > Dim Cursor As HourGlass > Set Cursor = New HourGlass > ' Any and all code.... > MsgBox "Waiting..." 'to see it was set to HourGlass > End Sub That's nice. A question, though. I have stayed away from use dim X as New Something in the past because I lose control of when VB creates an object...and I can't set it to nothing because it will just be created again if any code references it... But in this case I think that this might not be an issue, and using Dim Cursor as New HourGlass would make it a one-liner (Go Rick!) and eliminate the possibility that I might forget the Set line. Can you think of any reason why using Dim as New would be bad in this situation? Matt
From: Ken Halter on 23 Mar 2005 12:15
"YYZ" <none(a)none.com> wrote in message news:%239ARqs8LFHA.1392(a)TK2MSFTNGP10.phx.gbl... > > Can you think of any reason why using Dim as New would be bad in this > situation? > > Matt I can... I can (hand waving wildly)... There's no reference to the class, other than the Set xx = New line so it would never be instantiated. |