By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,968 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,968 IT Pros & Developers. It's quick & easy.

Save Record when Access loses Focus

100+
P: 759
Hi !

My database consist in 4 parts:
One of them is BackEnd (all the tables are here)
The other 3 parts are FrontEnd (Forms, Queries, Reports)

I use first FrontEnd part to store static information - dbStatic, the second one to read non-static information (every month) - dbRead and the third part for computations and results (all the time) - dbComputation.

Sometimes is necessary to open more than one of the FrontEnd part (say I work with dbComputation and I need to change something using dbRead)

When the user leave dbStatic or dbRead and return to dbComputation I need to save the current record. Else the modifications will not be visible in dbComputation.

I do not like the idea to design a command button for that.

In fact I am looking for an event as dbStatic_LostFocus (or Application_LostFocus) or some code to simulate this event.

Thank you !
Nov 24 '11 #1
Share this Question
Share on Google+
9 Replies


Expert 100+
P: 446
Hi,
Access will automatically save the current record when moving to the next but you can force a 'save' by putting the folowing in the After_Update event of any field.
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunCommand acCmdSaveRecord
The Lost_focus event will probably not fire when just toggling between two Access databases. Each will retain its own focus.
S7
Nov 24 '11 #2

100+
P: 759
Thank you for reply, S7.
I know. This work. But my problem is not solved.
The user can type something in a bound text box then, without exit from that text box (so, the update event for this text box is not triggered), press the minimize button of Access and jump to (any) other Windows application.

I need some code to "feel" that Access will lost the focus.
Nov 24 '11 #3

NeoPa
Expert Mod 15k+
P: 31,709
Once Access loses focus the only events likely to fire are timer based, or other OS managed events. There is no such event for activating Access or a database.

The best approach I can think of is for you to include a timer event on your form and for that code to check the active status of the Access window within the OS.
Nov 24 '11 #4

100+
P: 759
Can you tell me how to check the status of Access ?
I have no idea because I can't find any thing about Access Application object.

Thank you for reply !
Nov 24 '11 #5

Expert 100+
P: 446
Mihail,
That's a matter of user training then. If someone types in a textbox and does not press Enter, then it is correct that the database is not updated, so this data should not be reflected in the report. It could be argued that the edit is not complete so should not be saved.

You would be very disappointed if writing an email, then popping into Excel to check figures, only to return to the incomplete email to find it had been sent without pressing Send.

S7
Nov 24 '11 #6

100+
P: 759
I know, S7. But I can't instruct every user.
As coder I try to prevent any mistake.
If I can... is other problem.

Any way, thank you for your interest !
Nov 24 '11 #7

NeoPa
Expert Mod 15k+
P: 31,709
Mihail:
I have no idea because I can't find any thing about Access Application object.
You are looking the wrong way. The OS doesn't provide a way to check Access, but application windows generally ;-)

Sierra7:
You would be very disappointed if writing an email, then popping into Excel to check figures, only to return to the incomplete email to find it had been sent without pressing Send.
I would also give this careful consideration before proceeding as it makes a lot of sense to me. Only if you decide there is sense in your approach after all, should you proceed with this approach which, on the face of it, seems ill-advised.
Nov 24 '11 #8

100+
P: 759
Hi again.
The main problem is how to catch this event.
I'll see later how I use it :) .

I find this VB project (see attached file), I test it in VB6 environment and work fine, but I can't make the code to work in Access environment.
Can someone help me ?

** Edit **
Expand|Select|Wrap|Line Numbers
  1. Attribute VB_Name = "Module1"
  2. Option Explicit
  3.  
  4. Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  5. Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  6.  
  7. Public Const WM_ACTIVATEAPP = &H1C
  8. Public Const GWL_WNDPROC = -4
  9.  
  10. Global lpPrevWndProc As Long
  11. Global gHW As Long
  12.  
  13. Public Sub Hook()
  14.    'Establish a hook to capture messages to this window
  15.    lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WindowProc)
  16. End Sub
  17.  
  18. Public Sub Unhook()
  19.  
  20.    Dim temp As Long
  21.  
  22.    'Reset the message handler for this window
  23.    temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
  24.  
  25. End Sub
  26.  
  27. Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  28.    'Check for the ActivateApp message
  29.  
  30.    If uMsg = WM_ACTIVATEAPP Then
  31.        'Check to see if Activating the application
  32.        If wParam <> 0 Then
  33.            'Application Received Focus
  34.            Form1.Caption = "Focus Restored"
  35.        Else
  36.            'Application Lost Focus
  37.            Form1.Caption = "Focus Lost"
  38.        End If
  39.    End If
  40.  
  41.    'Pass message on to the original window message handler
  42.    WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
  43.  
  44. End Function
Attached Files
File Type: zip application lost focus.zip (2.0 KB, 111 views)
Nov 25 '11 #9

NeoPa
Expert Mod 15k+
P: 31,709
I can't help with that Mihail, but I've put out a call in case of any VB experts who may be able to.
Nov 25 '11 #10

Post your reply

Sign in to post your reply or Sign up for a free account.