Access Cookbook - Ken Getz [245]
' 4: Available page file in bytes
' 5: Total virtual memory in bytes
' 6: Available virtual memory in bytes
' Out:
' Return Value: The requested information
Dim MS As MEMORYSTATUSEX
' Set the length member before you call GlobalMemoryStatus.
MS.dwLength = Len(MS)
GlobalMemoryStatusEx MS
Select Case intItem
Case 0
acbGetMemoryStatus = MS.dwMemoryLoad * 10000 / 1024
Case 1
acbGetMemoryStatus = MS.dwTotalPhys * 10000 / 1024
Case 2
acbGetMemoryStatus = MS.dwAvailPhys * 10000 / 1024
Case 3
acbGetMemoryStatus = MS.dwTotalPageFile * 10000 / 1024
Case 4
acbGetMemoryStatus = MS.dwAvailPageFile * 10000 / 1024
Case 5
acbGetMemoryStatus = MS.dwTotalVirtual * 10000 / 1024
Case 6
acbGetMemoryStatus = MS.dwAvailVirtual * 10000 / 1024
Case Else
acbGetMemoryStatus = 0
End Select
End Function
TIP
Although it seems odd, the code in acbGetMemoryStatus is performing an important conversion. Because the values filled in by the GlobalMemoryStatusEx method are so large, you must use Currency values to contain the results. In order to store large values, VBA scales the contents of Currency variables by a factor of 10,000. Therefore, when you want to use the values here, you must multiply them by 10,000. In addition, to convert from bytes to KB, the code divides the totals by the number of bytes in a kilobyte, 1024.
Operating system information
The GetVersionEx API function does the work here. To simplify its use, we've provided the acbGetOSInfo function, as follows. Note that the Platform ID value simply indicates whether you're running a Windows 95-based operating system (such as Windows 98 or Windows ME) or a Windows NT-based operating system, such as Windows 2000, Windows XP, or Windows Server 2003. You can definitely retrieve more detailed results than this—see the documentation at http://msdn.microsoft.com for more information.
Public Function acbGetOSInfo(intItem As Integer) As Variant
' Retrieve operating system information
' In:
' intItem: Which piece of information to retrieve
' 0: Major Version
' 1: Minor version
' 2: Build Number
' 3: Platform ID
' 0 = Win32s (not going to happen!)
' 1 = Win95
' 2 = WinNT
' Out:
' Return Value: The requested information
Dim OSInfo As OSVERSIONINFO
' Set the length member before you call GetVersionEx.
OSInfo.dwOSVersionInfoSize = Len(OSInfo)
If GetVersionEx(OSInfo) Then
Select Case intItem
Case 0
acbGetOSInfo = OSInfo.dwMajorVersion
Case 1
acbGetOSInfo = OSInfo.dwMinorVersion
Case 2
' Get just the low word of the result.
acbGetOSInfo = OSInfo.dwBuildNumber And &HFFFF&
Case 3
acbGetOSInfo = OSInfo.dwPlatformId
End Select
Else
acbGetOSInfo = 0
End If
End Function
Directories
To retrieve the Windows directory, call acbWindowsDirectory, shown in the following code. For the Windows System directory, call acbSystemDirectory; for the temporary storage path, call acbTempPath; and to find out which directory Access is running from, call acbAccessDirectory. (Note that acbAccessDirectory doesn't actually use the Windows API to find the location of Access; the SysCmd function in Access makes that information available.)
Public Function acbWindowsDirectory( )
' Retrieve the Windows directory.
Dim strBuffer As String
Dim intCount As Integer
strBuffer = Space(MAX_PATH)
intCount = GetWindowsDirectory(strBuffer, MAX_PATH)
acbWindowsDirectory = CleanPath(Left(strBuffer, intCount))
End Function
System information
The GetSystemInfo API function provides all the information. To make this easier for you, we've provided the acbGetSystemStatus function, shown in the following code. Call this function with a number representing the piece of information you want.
Public Function acbGetSystemStatus(intItem As Integer) As Variant
' Retrieve system status information
' In:
' intItem: Which piece of information to retrieve
' 0: Computer identifier, specific to OEM
' 1: Returns page size and specifies the granularity of page
' protection and commitment
' 2: Lowest memory address accessible