Solved

LostFocus and the Toolbar

Posted on 1998-08-03
4
446 Views
Last Modified: 2012-05-04
I have an MDI app with a toolbar.  The code in ButtonClick for the Save button simply calls the save functionality on the active child form.  I also have placed textbox field-level validation throughout the app in the lostfocus subroutines.

The problem:  If I enter data into a given textbox BUT DO NOT TAB OFF then press the save button on the toolbar, the save functionality gets fired without the field-level validation getting fired.  The result is that invalid data can get saved to the database.

Question:  How can I get the field-level validation to fire in the described scenario?  It is not acceptable to do field-level validation at save time.

Using setfocus to change the focus to another control does not fire the lostfocus on the original control (already tried to do this).

Basically, since the Toolbar is on the MDI form, clicking a button on it does not make the control on the Child form lose focus.  How do I get around this?
0
Comment
Question by:csmyser
  • 2
  • 2
4 Comments
 

Author Comment

by:csmyser
ID: 1467690
Edited text of question
0
 
LVL 2

Accepted Solution

by:
percosolator earned 50 total points
ID: 1467691
You are using MS's ToolBar?  Tried it myself, and you are right, it doesn't work.

Several options:

   Look into Activbar (it's slick), but I haven't encountered a problem like this with Activebar, so I don't have an immediate answer (will try it at work tomorrow)


   If you don't have, are using, or don't want to buy Activebar, then put extra validation code into the button click event of your toolbar.

I ran a test with MS's toolbar.  Created a form with "Text1" and "Text2", and a MS toolbar.  In the toolbar, I created a button with the Key = "Test"

Code follows.
-------

Option Explicit

Private Sub Text1_LostFocus()

    If (Not IsNumeric(Text1)) Then
   
        MsgBox "Text1 is not numeric"
       
        Text1 = ""
       
        Text1.SetFocus
       
    End If
   
End Sub

Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)

    If (Button.Key = "Test") Then
   
        If (Not IsNumeric(Text1)) Then
       
            MsgBox "Text1 is not numeric"
           
            Text1 = ""
           
            Text1.SetFocus
           
        End If
       
    End If
   
End Sub
----

That should help you out.  If your validation is more extensive than a basic IsNumeric check, put it into a function or subroutine.
0
 

Author Comment

by:csmyser
ID: 1467692
If you have the chance to see if this works with Activebar, I'd love to know.

As for putting the code behind the button, it is one of our discussed options.  However, we are building a large application with many areas and a host of field-level validations.  Putting the code in the save button, but not firing each field's validation at save time, will require a lot of case statements, etc, because only the field in which the user was last in would need to have the validation run at save time.

I appreciate your response, however.
0
 
LVL 2

Expert Comment

by:percosolator
ID: 1467693
Active bar does the same thing.  Lemme try something else.


--------------------okay------------------------------

try this on for size.  Same as before with Text1, Text2
and Toolbar1

------------------------------------------------------
Option Explicit

Dim mblnCancel As Boolean
Private Sub Text1_LostFocus()

    If (Not IsNumeric(Text1)) Then
   
        mblnCancel = True
       
        MsgBox "Text1 is not numeric"
       
        Text1 = ""
        Text1.SetFocus
       
    Else
       
        mblnCancel = False
       
    End If
End Sub

Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)
   
    mblnCancel = False

    Screen.ActiveForm.ActiveControl.SetFocus
    SendKeys "{TAB}"
    DoEvents
   
    If (mblnCancel) Then Exit Sub
   
    MsgBox "This won't show if Text1 is not numeric"

End Sub
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question