0

I am using Excel 2013 and Access 2013. My access query has a form that takes2 inputs, "start date" and "end date", and then runs a macro in access. Is there a way I can use VBA to input "start date" and "end date" from excel and run the form in access? Here is what I have tried, but I get an error saying "The action or method is invalid because the form or report isn't bound to a table or query"

Sub RunAccessQuery()

Dim strDatabasePath As String
Dim appAccess As Access.Application
Dim startDate As Date
Dim endDate As Date

startDate = ThisWorkbook.Sheets("sheet1").Range("B2").Value

strDatabasePath = "access database path"
Set appAccess = New Access.Application
With appAccess
    Application.DisplayAlerts = False
    .OpenCurrentDatabase strDatabasePath
    .DoCmd.OpenForm "inputForm", , , "start =" & startDate
    '.Quit
End With
Set appAccess = Nothing

ThisWorkbook.RefreshAll
MsgBox ("Data has been updated")

End Sub

This is what my form looks like. Click me runs a macro, the first text box holds variable "start" and second one holds variable "end"

enter image description here

7
  • Please provide data on the form (record source, which controls are on it, etc). Commented Aug 18, 2017 at 18:55
  • theres 2 text boxes that you fill with a start date and end date (first text box holds variable "start" and second holds variable "end". then theres a button at the button that will run a macro when clicked. what is record source? Commented Aug 18, 2017 at 19:13
  • I do not get that error. DB opens, form opens, DB closes. I even confirmed with .Visible = True. However, the form is not filtered to the desired record until I change code to: "start = #" & startDate & "#". What is the form's RecordSource - a table, query, SQL statement? Commented Aug 18, 2017 at 19:25
  • the form just runs a macro. i dont think its attached to a query or table Commented Aug 18, 2017 at 19:30
  • Then why would you use the OpenForm WHERE CONDITION argument? Remove that from your code. Commented Aug 18, 2017 at 19:31

1 Answer 1

1

Since I assume your form has no recordsource, and the controls are unbound, the following should avoid the error you've encountered:

Sub RunAccessQuery()

Dim strDatabasePath As String
Dim appAccess As Access.Application
Dim startDate As Date
Dim endDate As Date

startDate = ThisWorkbook.Sheets("sheet1").Range("B2").Value

strDatabasePath = "access database path"
Set appAccess = New Access.Application
With appAccess
    Application.DisplayAlerts = False
    .OpenCurrentDatabase strDatabasePath
    .DoCmd.OpenForm "inputForm"
    .Forms("inputForm").start = startDate
    '.Quit
End With
Set appAccess = Nothing

ThisWorkbook.RefreshAll
MsgBox ("Data has been updated")

End Sub

However, this still does nothing more than just open the form and set the start date, and closes Access immediately after.

Sign up to request clarification or add additional context in comments.

1 Comment

" .Forms("inputForm").start = startDate " Thats what i was looking for. I just put a .DoCmd.RunMacro "macro" afterward to run the macro that the form should run. I just didnt know how to set the variables in the form. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.