0

I'm trying to open a Access database using Python. All I want it to do is open the .accdb file, as I have a macro that auto-runs. The only code I can find is this:

import win32api
import time
from win32com.client import Dispatch

strDbName = 'Empactis Delete Cloud v1.accdb'
objAccess = Dispatch("Access.Application")
objAccess.Visible = True
objAccess.OpenCurrentDatabase(strDbName)
objDB = objAccess.CurrentDb()
objAccess.run('test')
objAccess.Application.Quit()

My first issue is, I'm not running a macro from Python so I'm not sure which parts to remove and which parts to keep.

Second issue is, I cannot find win32api as a module anywhere. I have Python 3.8 and it's not an included module, PIP also can't find it to install.

There must be a simple way of just opening an MS Access file?

rfkortekaas
  • 6,049
  • 2
  • 27
  • 34
TomCrow
  • 47
  • 8
  • Do you want to open Access object within python and manipulate that object? Or do you want to open Access file and let user take control of it? If you don't really want Python to connect to db, just launch it, see if this helps http://www.learningaboutelectronics.com/Articles/How-to-launch-computer-programs-using-Python.php, however, I don't think that can open a specific Access database file. – June7 Jan 29 '21 at 19:34
  • I have been able to launch an Access database with a VBScript, no idea how to translate that into Python. Can be done with PowerShell and .bat file as well. https://stackoverflow.com/questions/28745336/how-do-you-launch-an-access-database-file-from-a-batch-script – June7 Jan 29 '21 at 19:45
  • First hit on Google for win32com, https://pypi.org/project/pywin32/. First hit on Google for win32api, https://stackoverflow.com/questions/3580855/where-to-find-the-win32api-module-for-python. Both the same project as it includes both these things. If the macro auto-runs, you can remove `objAccess.run('test')` (and you can remove `import time`, not a clue what that's doing there), the rest is all for opening the database and closing when you're done. – Erik A Jan 29 '21 at 19:52
  • @ErikA - Thanks but that doesn't help much. The pypi page for the pywin32 project has no documentation. The question page for the win32api just says it's now part of pywin32 but doesn't give any indication as to why or what's changed. I've now installed pywin32 but it's not called that in my modules list and I can't seem to use any functions within it? – TomCrow Mar 02 '21 at 13:46
  • Sorry, but I can't troubleshoot module installation issues without any way to reproduce what's going wrong or any details. – Erik A Mar 02 '21 at 13:51
  • Hi @ErikA - The module installed fine, it's the lack of documentation around what it does that's the problem. I've started getting a specific error now on https://stackoverflow.com/questions/66440826/opening-ms-access-and-opening-a-accdb-file-with-python – TomCrow Mar 02 '21 at 13:58

0 Answers0