1

I am trying to run a VBA script that will Run and Export an Access query. I'm able to get to the step in code where I run the query, however, I need to connect to DB2 to run this query in Access and I don't know how to implement into my code to enter the username and password.

Sub RunQuery()
Dim A As Object
Application.DisplayAlerts = False
Set A = CreateObject("Access.Application")
A.Visible = False
A.OpenCurrentDatabase ("J:\user\filename.mdb")
A.DoCmd.OpenQuery "QueryName"
A.DoCmd.ConnectString
Application.DisplayAlerts = True

End Sub

The code just stalls at the line:

A.DoCmd.OpenQuery "QueryName"

And If I open my Database from here with my query it is just waiting for my username and password. I'll try and attach a picture of the prompt.

Any help would be greatly appreciated!!

Thank you very muchenter image description here

1

1 Answer 1

2

As Ryan said using ADO will be a better option, see below

Public Sub RunQuery()
    Dim A As Object
    Dim rs As Object
    Dim strSql As String
    Dim strConnection As String
    Set A = CreateObject("ADODB.Connection")
    strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=J:\user\filename.mdb"
    strSql = "SELECT * FROM table"
    A.Open strConnection
    Set rs = A.Execute(strSql)
    arr = rs.GetRows

    'now the array arr has the data queried

    rs.Close
    Set rs = Nothing
    A.Close
    Set A = Nothing
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.