Solved

LostFocus and the Toolbar

Posted on 1998-08-03
4
449 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
passing parameter in sql procedure 9 70
Add and format columns in vb6 7 81
How to Add / Edit Windows Menu 4 78
Need Nag Screen for Reboot if system up time is over 14 days 12 94
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

742 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