Determine OS from VBA - a few tests needed

spikepl

Eledittingent Beliped
Local time
Today, 15:56
Joined
Nov 3, 2010
Messages
6,142
I have picked up this code to determine the OS on which the Access app is running (from http://www.vbaexpress.com/forum/showthread.php?21062-Operating-System-using-Access-VBA )

Code:
Function getOS() As String      
Dim OS      
    For Each OS In GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")          
       getOS = OS.Caption     
    Next OS     
Set OS = Nothing  
End Function
Win 7 Pro (32 bits) returns: Microsoft Windows 7 Professional

If you have Vista, or Win 8, can you please try and write here what it returned?

Also, I wonder how to run this on Windows 64-bit - I dont have access to any such installation to test.
 
spikepl,

?getos()
Microsoft® Windows Vista™ Home Basic

Perfect.
 

Attachments

  • OSForSpike.jpg
    OSForSpike.jpg
    79.6 KB · Views: 122
Returned:

Microsoft Windows 7 Professional

on a 64bit computer.
 
Thx jdraw. That's pretty weird that it returned all these non-standard characters for Vista, but good to know.
 
@Paul

Did you run the code as is? So it did not stumble on "Win32_OperatingSystem ?"
 
I ran as is and it did not stumble (okay, I tweaked to debug.print). Threw an error when I tried changing 32 to 64. See attached.
 

Attachments

  • OS.jpg
    OS.jpg
    46.9 KB · Views: 150
@TJPoorman

Thanks - but I think the actual name comes from .Name according to your link, whereas architecture says 32 or 64 bits (or something else)... I simply need to be able to tell which OS I am on so as to find the signature file for Outlook.

(I know there is one location on XP, another on Vista and 7, and no idea where it is on 8.
 
@Paul

Just to be clear: was the OS 64 bit or just the processor?
 
As shown in pic, 64 bit OS.
 
is environ any good?

?environ("os")
Windows_NT

on my W7 machine. does that make sense?
 
I came upon this very old thread when trawling through looking for something else. As it hadn't perhaps been fully answered, I offer the following functions which give both the OS version and Access version info together with bitness.
I use this info to provide screens like this with my apps

attachment.php


Code:
Function GetWindowsVersion()

 Dim localHost       As String
    Dim objWMIService   As Variant
    Dim colOperatingSystems As Variant
    Dim objOperatingSystem As Variant

    On Error GoTo Err_Handler

    localHost = "." 'Technically could be run against remote computers, if allowed
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & localHost & "\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

    For Each objOperatingSystem In colOperatingSystems
        GetWindowsVersion = objOperatingSystem.Version
    Next

Exit_Handler:
   ' On Error Resume Next
    Exit Function

Err_Handler:
    FormattedMsgBox "Error " & Err.Number & " in GetWindowsVersion procedure :             " & _
        "@" & Err.description & "            @", vbCritical, "Program error"
    Resume Exit_Handler
    
End Function

This gives a result in the following format: 10.0.16299

The next function by Daniel Pineault gives a more meaningful result
I have modified it to include the 'bitness'
e.g. Microsoft Windows 10 Pro 10.0.16299 32-bit

Code:
'---------------------------------------------------------------------------------------
' Procedure : GetOperatingSystem
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Return the active OS details
' Copyright : The following may be altered and reused as you wish so long as the
'             copyright notice is left unchanged (including Author, Website and
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2012-Sep-27                 Initial Release
'---------------------------------------------------------------------------------------

'CR 28/04/2014 - added code to check if 32 bit / 64 bit

Public Function GetOperatingSystem()
    Dim localHost       As String
    Dim objWMIService   As Variant
    Dim colOperatingSystems As Variant
    Dim objOperatingSystem As Variant

    On Error GoTo Err_Handler

    localHost = "." 'Technically could be run against remote computers, if allowed
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & localHost & "\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

    For Each objOperatingSystem In colOperatingSystems
        GetOperatingSystem = objOperatingSystem.Caption & " " & objOperatingSystem.Version
      '  Exit Function
    Next
    
    'Now determine if operating system is 32-bit or 64-bit
    '-------------------------------------------------
    GetOperatingSystem = GetOperatingSystem & " " & IsWin32OrWin64
    
    
Exit_Handler:
   ' On Error Resume Next
    Exit Function

Err_Handler:
    FormattedMsgBox "Error " & Err.Number & " in GetOperatingSystem procedure :             " & _
        "@" & Err.description & "            @", vbCritical, "Program error"
    Resume Exit_Handler
End Function

Code:
Function IsWin32OrWin64()

'Determine if operating system is 32-bit or 64-bit
    '-------------------------------------------------
    'Modified from code provided at:
    '  http://www.vb-helper.com/howto_get_os_name.html
    Dim proc_query As String
    Dim proc_results As Object
    Dim Info As Object
    
    proc_query = "SELECT * FROM Win32_Processor"
    Set proc_results = GetObject("Winmgmts:").ExecQuery(proc_query)
    For Each Info In proc_results
        IsWin32OrWin64 = Info.AddressWidth & "-bit"
    Next Info
    
End Function

On a similar theme, this gets the Office 'bitness'

Code:
Function IsOfficex64()
'checks if Office is 32 or 64 bit

#If Win64 Then
  IsOfficex64 = "64-bit"
#Else
  IsOfficex64 = "32-bit"
#End If

End Function

This gives the Access build version e.g. 14.0.7195

Code:
Function GetAccessBuildVersion() As String
'CR 29/05/2012
'Gets full Access version number including program updates / service packs
'e.g. 14.6024 for Access 2010 SP1
GetAccessBuildVersion = Nz(SysCmd(acSysCmdAccessVer) & "." & SysCmd(715), "None")
'Debug.Print GetAccessBuildVersion
End Function

Finally, this gives an Access version description e.g. 2010 SP2

Code:
Function GetAccessVersionName() As String
'CR 29/05/2012
' Converts version number to product name - needs exact match
'GetAccessVersionName = Nz(DLookup("AccessVersion", "tblAccessInfo", "ApplicationVersion = " & CInt(SysCmd(acSysCmdAccessVer)) & "." & SysCmd(715)), "None")

'Updated to extract highest listed version below that listed where no exact match
'e.g. Version 14.7104 (2010 SP2 with updates) matched to 14.7015 (2010 SP2)
GetAccessVersionName = Nz(DLast("AccessVersion", "tblAccessInfo", "ApplicationVersion <= " & CInt(SysCmd(acSysCmdAccessVer)) & "." & SysCmd(715)), "None")

End Function
This uses a reference table tblAccessInfo - see attached Excel file:

Just combine the last 3 functions to get something like: Access 2010 SP2 version 14.0.7195 32-bit

Hope this is useful to others
 

Attachments

Last edited:

Users who are viewing this thread

Back
Top Bottom