Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ldb file not cleaned up after hooking form

Status
Not open for further replies.

joelflorendo

Technical User
Dec 5, 2004
92
US
I'm using the code here to hook my form so I can minimize it to the system tray. (
I want to quit the access application when the form is closed so I'm calling the unhook function from the onclose event of the form and then call Application.Quit. The odd thing is that Access isn't cleaning up the ldb file when I do this. If I separate the two processes by unhooking the form and closing the form from two different buttons, the ldb file goes away, but not if I try to call the unhook and quit in the same sub.

Even more strange is that I can delete the ldb file without any problems. So the connection is being closed but it's just not cleaning up the ldb file.

Any ideas?
 
Hi Joel,

Excuse my ignorance of the Unhook function, but the only context I can place it in is as a Windows API function to release hook procedures and you certainly didn't provide enough info to make a go of sorting that out. I suggest a read of Microsoft's documentation on the function.

But hook matters aside, have you tried closing the application on the form's UnLoad event instead?

Cheers, Bill
 
Yes, the hook/unhook makes use of Windows API functions. The code I'm using can be seen through the link I provided above and again here ( Basically, the only difference between the code that is shown there and mine is that I'm trying to call the unhook function from an event within a form's close procedure whereas Dev Ashish is calling the unhook from a command button. It does work if I call the unhook function from a separate process than from where I'm quitting (for example, unhook from command button and quit from form Close Event). But doesn't work if I try to call both from the same process (i.e. call unhook then Application.Quit from a single command button or from the OnClose event of the form).

I did try calling the unhook and quit functions from the unload event and still the same results. Again it only happens if I call Application.Quit after unhooking within the same process. I can even place the uhook in the unload event and then quit in the OnClose event and still the ldb file doesn't get cleaned up.

I've checked before and after the hook and it returns the form back correctly to it's original Process ID so I'm sure it's unhooking the form. But what it seems like is that it's not being given enough time to complete the unhook before calling the Application.Quit so that Access can clean up and close out properly.
 
Hi Joel,

Perhaps a timer for a second or two while things clear out.

In the meantime I will try to figure out why my version of it gets hung up in a call back loop and then perhaps I can suggest something more useful.

Cheers, Bill
 
Just wanted to post the solution I ended up with. I found some vb6 code I was able to modify to work with access so here it is:

in a module
Code:
Option Compare Database
'****************************************************************************************************************
' example code found at: [URL unfurl="true"]http://www.trap17.com/index.php/Tutorial-Visual-Basic-6-Minimize-Tray_t6940.html[/URL]
'****************************************************************************************************************

Public nID As NOTIFYICONDATA 'loads an icon, cursor, or bitmap.

'Create an Icon in System Tray Needs
Public Type NOTIFYICONDATA
    cbSize As Long
    hwnd As Long
    Uid As Long
    UFlags As Long
    UCallbackMessage As Long
    hIcon As Long
    SzTip As String * 64
End Type

Private Const IMAGE_ICON = 1         ' Loads an icon.
Private Const LR_LOADFROMFILE = &H10      'Loads the image from the file specified by the lpszName parameter.

Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public Const WM_MOUSEMOVE = &H200
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const WM_LBUTTONDBLCLK = &H203 'Double-click
Public Const WM_LBUTTONDOWN = &H201 'Button down
Public Const WM_LBUTTONUP = &H202 'Button up
Public Const WM_RBUTTONDBLCLK = &H206 'Double-click
Public Const WM_RBUTTONDOWN = &H204 'Button down
Public Const WM_RBUTTONUP = &H205 'Button up

Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, _
                                                                                  pnid As NOTIFYICONDATA) As Boolean

Private Declare Function apiLoadImage Lib "user32" _
    Alias "LoadImageA" _
   (ByVal hInst As Long, _
   ByVal lpszName As String, _
   ByVal uType As Long, _
   ByVal cxDesired As Long, _
   ByVal cyDesired As Long, _
   ByVal fuLoad As Long) _
   As Long
   
Public Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long

Public Sub MinimizeToTray(ByRef m_fFrm As Form, ByVal strIconPath)
    Dim hIcon As Long
    
    'Load the 16x16 icon from file
    hIcon = apiLoadImage(0&, strIconPath, IMAGE_ICON, 16&, 16&, LR_LOADFROMFILE)
       
    nID.cbSize = Len(nID)
    nID.hwnd = m_fFrm.hwnd
    nID.Uid = vbNull
    nID.UFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    nID.UCallbackMessage = WM_MOUSEMOVE
    nID.hIcon = hIcon
    nID.SzTip = "CPT Lookup" & vbNullChar
    Shell_NotifyIcon NIM_ADD, nID
End Sub


'--------------------------------------------------
Function TwipsPerPixelX() As Single
'--------------------------------------------------
'Returns the width of a pixel, in twips.
'--------------------------------------------------
  Dim lngDC As Long
  lngDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelX = 1440& / GetDeviceCaps(lngDC, LOGPIXELSX)
  ReleaseDC HWND_DESKTOP, lngDC
End Function

on the form:
Code:
Private Sub Form_Resize()
    'check if resize is for minimizing
    If IsIconic(Me.hwnd) Then
        Me.Visible = False
        MinimizeToTray Me, SYSTRAY_ICON
    End If
End Sub

'***********************************

Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim Msg As Long
    Dim sFilter As String
    
    Msg = X / TwipsPerPixelX
    
    Select Case Msg
        Case WM_LBUTTONDOWN
        Case WM_LBUTTONUP
        Case WM_LBUTTONDBLCLK
            Me.Visible = True ' show form
            Shell_NotifyIcon NIM_DELETE, nID ' del tray icon
        Case WM_RBUTTONDOWN
        Case WM_RBUTTONUP
        Case WM_RBUTTONDBLCLK
    End Select
End Sub

This works without having to hook the form so I no longer have the ldb file hanging out after close of the database. The code is also much simpler.

The only problem I'm having is that the form is made visible again after a single click instead of a double click even though I have it set to show after double click. I haven't really looked that deeply into the problem but I don't think it will be a big deal for the user for now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top