Results 1 to 14 of 14
  1. #1
    Macallan60 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2010
    Posts
    51

    GetPCUptime for 64 bit

    Hello,



    I have Microsoft Access for Office 365 on Win10 PC. We are slowly migrating from the 32 bit version to 64 bit. I have a VB code given to me by a collogue to determine how long it's been since a PC restart has occurred. It works great on the 32 bit version. But I get a error on the 64 bit version (see screenshot).







    I'm posting the code below. What modifications do I need to make to have it run on the 64 bit version?


    Thanks in advance. Kevin


    Function GetPCUptime(Optional sHost As String = ".") As String
    'Obtains how long in minutes the PC has been running since its last restart (aka reboot).

    '

    'FUNCTION SETUP:

    'Copy/paste the this function into a standard module. Name it modPC_UpTime.

    'In the Form that loads when the database launches, do the following:

    ' 1. Create a unbound textbox and name it txtPC_UpTime.

    ' 2. In the Control Source, enter =GetPCUptime().

    ' 3. Set Visible to No.

    'Go to the Form's Events tab and enter the following in the On Load event:

    ' Private Sub Form_Load()

    ' DoCmd.SetWarnings False

    ' 'txtPC_UpTime.Value = number of seconds (3600 sec = 1 hr)

    ' If Me.txtPC_UpTime.Value > 86400 Then

    ' MsgBox "It has been more than 24 hours since your last computer restart." & vbCrLf & _

    ' "Click the OK button then restart this computer." & vbCrLf & vbCrLf & _

    ' "Last Restart (aka Reboot) was " & FormatNumber(Me.txtPC_UpTime.Value / 3600, 0) & _

    ' " hours ago", vbExclamation, "Restart Computer"

    ' DoCmd.Quit acSave

    ' Else

    ' End If

    ' End Sub

    '

    On Error GoTo Error_Handler

    Dim oWMI As Object 'WMI object to query about the PC's OS

    Dim oOSs As Object 'Collection of OSs

    Dim oOS As Object 'Individual OS

    Dim lUpTime As Long



    Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonat e}!\" & sHost & "\root\cimv2")

    Set oOSs = oWMI.ExecQuery("SELECT SystemUpTime FROM Win32_PerfFormattedData_PerfOS_System")

    For Each oOS In oOSs

    lUpTime = oOS.SystemUpTime

    'UpTime in seconds

    GetPCUptime = Int(oOS.SystemUpTime)

    Next



    Error_Handler_Exit:

    On Error Resume Next

    Set oOS = Nothing

    Set oOSs = Nothing

    Set oWMI = Nothing

    Exit Function



    Error_Handler:

    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _

    "Error Number: " & Err.Number & vbCrLf & _

    "Error Source: GetPCUptime" & vbCrLf & _

    "Error Description: " & Err.Description & _

    Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _

    , vbOKOnly + vbCritical, "An Error has Occurred!"

    Resume Error_Handler_Exit

    End Function

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Macallan60 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2010
    Posts
    51

    GetPCUptime for 64 bit

    I'm attaching a sample database with this issue. Works fine on 32 bit, errors on 64 bit.

    Any suggestions is greatly appreciated.

    Thank you all for your help. Kevin
    Attached Files Attached Files

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    64bit WIN10 AC 365

    Device name Laptop-DELL2in1
    Processor 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz 2.80 GHz
    Installed RAM 16.0 GB (15.7 GB usable)
    System type 64-bit operating system, x64-based processor
    Pen and touch Touch support with 10 touch points



    Edition Windows 10 Home
    Version 21H2
    Installed on ‎19-‎Oct-‎21
    OS build 19044.1826
    Experience Windows Feature Experience Pack 120.2212.4180.0

    Access Version 2206 Build 15330.20264 ClickToRun



    ?GetPCUptime
    1386165

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    I thought we were talking about 64bit Access not O/S ?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    Quote Originally Posted by Welshgasman View Post
    I thought we were talking about 64bit Access not O/S ?
    Paul,
    My access is 64 bit. I was providing some context for completeness.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    Ah ok, sorry I could not see that. It works over in UA as well. Also apparently it is Daniel Pinault's code with the credits removed.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    Macallan60 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2010
    Posts
    51
    Here's my reply to Daniel on UA:
    I've been down this road before regarding performing a Office repair. They will not do it for a small issue like this. It must be "business critical"
    That's just ridiculous. With MS365, Repair, is now a must. Where it used to be a rare need, MS365 regularly needs it! It is up to management to tell IT to do.
    I appreciate and share your opinion, but sometimes that's how it goes. I've taken the Office repair issue to the highest management that I have available in the past. Sometimes the answer is no. My company has more than 100k employees. One of the facts of life I've learned from working in big companies is that some things will not be done no matter how useful it is.
    I have a VB code given to me by a collogue to determine....
    That's my code, which you didn't keep the required header/copyright to, come on!

    I'm sorry your code was altered, It was provided to me by a former collogue. I always keep all the information on any code I get from the internet just in case. If I knew this was your code, I would have contacted your first. You have always been helpful to me in the past.

    I do not know how to integrate your suggestion into the code. But I appreciate your help.

    Thank you
    Gustav was very helpful as well. I informed him that where it's nice to remind end users to restart their PC, this code is not critical to the database operation. So I decided to remove it from the database.

    Thanks everyone for your help.

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    Daniel gave you powershell code. I suspect you would save that as a file, then call powershell to run that file?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    Macallan60 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2010
    Posts
    51
    Hi Welshgasman,

    I've never worked with powershell code. But thanks anyway. I went ahead and removed the code and re-deployed my database.

    Have a good weekend!

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    It really is very powerful. I use a one liner to make a system image every month. Not from access, but that should not be a stumbling block. Might try over the weekend. :-) You also have a good one.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    OK, this is Daniel's code
    Code:
    $lastBoot = (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime
    $now = Get-Date
    $uptime = $now - $lastBoot
    Write-Output "Days: $($uptime.days), Hours: $($uptime.Hours), Minutes:$($uptime.Minutes)"
    I saved that as UpTime.ps1
    Then I ran it from the immediate window. I found that you have to set the policies for Powershell as they are restricted otherwise.
    So I referred to the MS link in the error message in command window and set the policy for the current user.

    https://docs.microsoft.com/en-us/pow...powershell-7.2

    However, I suspect this would not be an option for you due to this and IT.
    Furthermore on my laptop, it does not appear accurate as I just switched my laptop on this morning.

    Now it went to sleep, so possibly that time includes sleep time?

    Code to run was
    Code:
    shell ("powershell -noexit -file " & """c:\temp\uptime.ps1""")
    The -nooexit is required else the window closes after script has run.
    Attached Thumbnails Attached Thumbnails powershell.JPG  
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    Well I shutdown my laptop for a few minutes, started it back up and ran that powershell script.
    That showed the laptop had been up for 12 hours, so not sure what it is showing?

    Gustav's code shows the same amount of hours as well?

    Edit: Task Manager is showing that 12 hours as well, so whilst the code works in both scenarios, I have no ides as to why it is showing 12 hours plus
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    Well it was determined over on UA, that the time is since the last RESTART as a shutdown is no longer a shutdown as we know it.
    I confirmed that is the case with a restart.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Please reply to this thread with any new information or opinions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums