Solved

LostFocus and the Toolbar

Posted on 1998-08-03
4
448 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 - Convert HH:MM into Decimal 8 63
Passing a Text Box name to a Sub 6 99
Problem to With line 4 63
backup program with robocopy 6 47
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

821 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