Question

Q: VBA to find Windows Version This thread is locked from future replies

I am using the following code to try to determine the version of Windows a user of my application has. I am trying it out on my computer that is running Windows 7 64 bit and the code says that I am running Major Version 5 and Minor Version 1. (Windows Vista is  6.1, so I am assuming that Windows 7 must be Major Version 6.2) This would indicate that I am running Windows XP, but I am running Windows 7. What am I doing wrong? I have written my program using Access 2002. But that should not have anything to do with the code determining the version of windows that I an running. Right? Any help with the code would be greatly appreciated.

 

'FUNCTION TO DETERMINE WINDOWS VERSION NUMBER

Option Compare Database
Option Explicit

Private Type OSVERSIONINFO
  dwOSVersionInfoSize As Long
  dwMajorVersion As Long
  dwMinorVersion As Long
  dwBuildNumber As Long
  dwPlatformId As Long
  szCSDVersion As String * 128
End Type

Public Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Integer

Public Function getVersion() As String

  Dim osinfo As OSVERSIONINFO
  Dim retvalue As Integer

  osinfo.dwOSVersionInfoSize = 148
  osinfo.szCSDVersion = Space$(128)
  retvalue = GetVersionExA(osinfo)

  getVersion = "Version is " & osinfo.dwMajorVersion & "." & osinfo.dwMinorVersion

End Function

 



* Please try a lower page number.

* Please enter only numbers.

* Please try a lower page number.

* Please enter only numbers.

Windows 7 should be 6.1 (Windows Vista = 6.0, Windows 7 = 6.1 and Windows 8 = 6.2).

 

Are you perhaps running Access 2002 in Windows XP compatibility mode? See the Compatibility tab of the Properties window of the shortcut you use to start Excel.

If so, you'd get 5.1, the internal version number of Windows XP.

---
Kind regards, HansV
www.eileenslounge.com

Did this solve your problem?

Sorry this didn't help.


HansV,

 

Thanks for your quick response. I appreciate it.

 

I am not running Access 2002 in XP compatibility mode, however. I checked the shortcut that I am using to start my Access front-end database, and I do not have compatibility mode selected at all. Any other suggestions?

Did this solve your problem?

Sorry this didn't help.


Are you sure you're really getting 5.1, and not 6.1?
---
Kind regards, HansV
www.eileenslounge.com

Did this solve your problem?

Sorry this didn't help.


The version can sometimes impact development, especially if you and your client(s) don't have the same setup.  For instance, MS release an update not to long ago, if you were to have it install and not your client your db wouldn't work for them!

 

Depending on your need, are you after Major/Minor... info or looking for more user-friendly info (plain English that your client might understand and be able to relay to you), then you may prefer using a function such as:

 

'---------------------------------------------------------------------------------------
' 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
'---------------------------------------------------------------------------------------
Public Function getOperatingSystem()
    Dim localHost       As String
    Dim objWMIService   As Variant
    Dim colOperatingSystems As Variant
    Dim objOperatingSystem As Variant

    On Error GoTo Error_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

Error_Handler_Exit:
    On Error Resume Next
    Exit Function

Error_Handler:
    MsgBox "The following error has occured." & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: getOperatingSystem" & vbCrLf & _
           "Error Description: " & Err.Description, _
           vbCritical, "An Error has Occured!"
    Resume Error_Handler_Exit
End Function

--
Daniel Pineault
Microsoft MVP 2010-2017

Did this solve your problem?

Sorry this didn't help.

3 people were helped by this reply


Thanks Daniel,

 

Your code works great! Thank you so much. I really appreciate your help.

 

Robert Valentine

Did this solve your problem?

Sorry this didn't help.


I have Windows 10 Pro, and the return value I get is 6.2 (using Bob Valentine's original function), which indicates Windows 8.0!  I never even had Win8 on this computer, I jumped from Win7 to Win10.

I just tried Danial Pineault's function, and it worked!  Don't know how it works, but it did.

Did this solve your problem?

Sorry this didn't help.


Daniel,

Great function, but it is using things I've never seen before!  Could you elaborate a bit on what you are doing in the code?

Did this solve your problem?

Sorry this didn't help.


Please don't piggyback your question on another thread. Please use the Participate link avoice to start your own thread. You can include a link to the older thread. 

Hope this helps,
Scott<>
Blog: http://scottgem.wordpress.com
Microsoft Access MVP since 2007

Did this solve your problem?

Sorry this didn't help.


How does my question not pertain to the original thread?   Daniel proposed an alternative function, and I just asked for him to clarify what he did.   If anything, you could say his post should have required a new thread since he didn't address the function Bob first detailed.

Did this solve your problem?

Sorry this didn't help.


How does my question not pertain to the original thread?   Daniel proposed an alternative function, and I just asked for him to clarify what he did.   If anything, you could say his post should have required a new thread since he didn't address the function Bob first detailed.

I didn't say anything about whether your question pertained. Piggybacking is when a person other than the person who started the thread adds their own question (relevant or not) to the thread. Especially years after the thread originally started. Piggybacking is frowned on in most Q&A forums in my experience.

Daniel's post was not another question but a relevant and helpful response to the original post.

Hope this helps,
Scott<>
Blog: http://scottgem.wordpress.com
Microsoft Access MVP since 2007

Did this solve your problem?

Sorry this didn't help.


* Please try a lower page number.

* Please enter only numbers.

* Please try a lower page number.

* Please enter only numbers.

 
Question Info

Views: 8,750 Last updated: April 4, 2018 Applies to: