Python Forum

Full Version: Trying to use win32 Sendkeys to log into database overnight
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to log into and run a macro in an Access database while not at my computer. I have written a script that achieves what I want while I am logged in but when I schedule the task to run overnight it does not work. What would cause this to happen?

I have tried writing a script that keeps the computer awake by sending a key repeatedly for 12 hours but that did not help. I have other Python programs that run overnight that don't use sendkeys and they have no problems.

I have tried running it only up to before the macro and it appears to not be saving the dates or entering a password. If I run it with the macro, I see it attempted the macro but did not start it because it does not have the password.

The popup is asking for the password to make the ODBC connection between Access and the backend Oracle database.

All of this is done on a virtual desktop that stays logged in for 24 hours. The physical computer is explicitly told to sleep. Is it possible that sendkeys is trying to send from the physical keyboard?

This is the latest Python 3. I am using 32bit Anaconda/Python on Windows 10.

My script is like this:

import win32com.client
import win32api

access = win32com.client.Dispatch("Access.Application")
shell = win32com.client.Dispatch("WScript.Shell") 
access.OpenCurrentDatabase("myDbPath")

time.sleep(400) #I've tried less time too
shell.Sendkeys("{TAB}") # tab to button that triggers the login popup
time.sleep(10)
shell.Sendkeys("{ENTER}") 
time.sleep(10)
shell.Sendkeys("pw")
time.sleep(10)
shell.Sendkeys("{ENTER}")
time.sleep(10)

shell.Sendkeys("{TAB}") # go to start date field
time.sleep(10)
shell.Sendkeys(startDate) # fill start date
time.sleep(10)  
shell.Sendkeys("{TAB}") # go to enddate field
time.sleep(10)
shell.Sendkeys(endDate) # fill end date
time.sleep(10)
shell.Sendkeys("{TAB}") # to save dates button
time.sleep(10)
shell.Sendkeys("{ENTER}") # click save dates button    
time.sleep(10)
access.DoCmd.RunMacro('myMacro') # run macro

access.CloseCurrentDatabase()
access.Application.Quit()