I have both Excel 2010 and Access 2010 already running on my Windows 7 PC. The worksheet has hundreds of rows and many columns eg, Surname (column A), Given name (col B), etc. The database has an existing query that asks for two parameters – Surname and Given name – as input to retrieve matching records. Because I use that query many times throughout the day to check on different names from the worksheet, manually typing in the names each time becomes a chore (and prone to typing errors) so I would like to automate that process.
I came up with the macro below in Excel to read the name values from the active row, switch over to the Access database (which is already running), open the existing query, and feed the values into the parameters. It seems to work (the hourglass cursor appears for the same number of seconds as if I had run the query manually, and the macro ends without error) but the problem is that Access doesn’t show any results (it is still running but doesn’t open the query datasheet window). Stepping through the macro line by line, it seems that after switching over to the running instance of Access, the query and parameters are not being processed in that instance of Access yet it steps through to the end of the macro without giving an error. Thanks in advance.
Sub test() Dim PatSurname As String, PatFirstName As String Dim db As DAO.Database, qry As DAO.QueryDef, rs As DAO.Recordset Sheets(“UniqNames”).Activate PatSurname = Selection.End(xlToLeft).Value PatFirstName = Selection.End(xlToLeft).Offset(0, 1).Value AppActivate “Reporting Server” ‘this is the db appname that appears in Task Manager Set db = CurrentDb Set qry = db.QueryDefs(“Retrieve DVA Client details via FacilID & MRN or Name ~PCD”) With qry .Parameters(“[First Name?]”) = PatFirstName .Parameters(“[Surname?]”) = PatSurname End With Set rs = qry.OpenRecordset Set db = Nothing Set qry = Nothing Set rs = Nothing End Sub