Online Book Reader

Home Category

Access Cookbook - Ken Getz [240]

By Root 2037 0
in the array of top-level windows.

Decide which window you want to close. Windows sometimes appends document names to the application name (e.g., "Microsoft Word—11-10.DOC"), so check against just the first portion of the window name in your array. For example:

For intI = 0 To intCount - 1

If Left$(atypWindowList(0).strCaption, 14) = "Microsoft Word" Then

' You found a match. Do something.

End If

Next intI

When you've found the item you want to close, use the acbCloseWindow function, passing to it the handle of the window you care about:

If acbCloseWindow(atypWindowList(intI).hWnd) = 0 Then

' If you got 0 back, it got the message!

End If

Discussion


The acbCloseWindow function calls the PostMessage API function. By posting a message to a particular window, you are telling it to do something, but you don't bother waiting for a response. (The corresponding API function, SendMessage, does cause you to wait for a response. You can use SendMessage if you want to stop and wait for the other application to close, but we don't recommend it.) The acbCloseWindow function sends the WM_CLOSE message to your chosen window, telling it to shut down. It's as if you quit your Windows shell program with some applications running. Your shell sends a message to each main application window to shut down because Windows is shutting down. The acbCloseWindow function, then, looks like this:

Function acbCloseWindow (ByVal hWnd As Long)

Const WM_CLOSE = &H10

acbCloseWindow = PostMessage(hWnd, WM_CLOSE, 0, vbNullString)

End Function

The purpose of this wrapper function that calls PostMessage is to prevent you from having to remember how to post a message to a window. It's a lot simpler to call acbCloseWindow than to call PostMessage directly.

Sending a WM_CLOSE message to a window doesn't necessarily close it. If that application has an unsaved document, it will pop up its own dialog asking what you want to do with that unsaved document. In the sample form, if this happens, the list box won't be updated correctly. Once you return from your duties with the foreign application, press the Requery button on the form to force it to search again for all open applications.

11.11. Set File Date and Time Stamps


Problem


Access makes it easy to retrieve the modification date and time for files on disk, using the FileDateTime function. In one application, though, you need to be able to reset the last-modification date of files manually; the Access FileCopy function doesn't reset file date and time stamps, and you'd like copied files to have the current time. Is there a Windows API call that allows you to set file date and time stamps?

Solution


Windows provides the GetFileTime and SetFileTime API functions. Both work with three different date/time values: date of creation, date of last access, and date of last write. You want to preserve the date of creation and update the dates of last access and update. The code shown in this example will allow you to do this.

The sample form, frmTimeStamp, allows you to select a filename. The function then displays the date and time of last modification for the file, as shown in Figure 11-13. In addition, you can set a new file date, time, or both (the function retains whichever setting you don't change, if you just change one).

Figure 11-13. frmTimeStamp shows a selected file's modification date and time

To set file date and time information in your own applications, follow these steps:

Import the module basTimeStamp from 11-11.MDB. This module includes the type definitions and Windows API declarations you'll need, as well as a VBA function to convert dates and times as retrieved from the API call into date/time values that Access can understand. If you want to use this sample form in your own applications, you'll also need to import basFillList, which includes functions to retrieve the list of files.

To set the modification-date information for a specific file, call the acbSetFileDateTime function, passing it a filename and a date/time value as parameters.

Return Main Page Previous Page Next Page

®Online Book Reader