Solved

LostFocus and the Toolbar

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now