0

I am trying to run a stored procedure (using Access VBA) in the SQL database. I can find a few examples of this on SO and on other sites but what I am unable to find is connection string where I can provide my login details as windows authentication won't work because I have to use a different user to connect to SQL database

Here is what I have tried so far (got this from one of the post in SO, unfortunately, I can't find the post anymore):

Sub RunSQLProc()
    Dim cdb As DAO.Database, qdf As DAO.QueryDef
    Set cdb = CurrentDb
    Set qdf = cdb.CreateQueryDef("")
    qdf.Connect = "ODBC;" & _
            "Driver=SQL Server;" & _
            "Server=myServer;" & _
            "Database=myDatabase;" & _
            "UID=myUsername;" & _
            "PWD=myPassword;" & _
            "Trusted_Connection=yes;"
'            "Driver={SQL Server Native Client 11.0};" & _
'            "MARS Connection=True;"
    qdf.SQL = "SQLStroedProcedure;"
    qdf.ReturnsRecords = False
    qdf.Execute dbFailOnError   '<-- this line throws the error
    Set qdf = Nothing
    Set cdb = Nothing
End Sub

On qdf.Execute dbFailOnError line, I get the following error:

Run-time error '3151': ODBC--connection to 'SQL ServermyServer'failed.

I have managed to connect to the SQL database and run the stored procedure through SQL Server Management Studio. This tells me that the details in my connection string are correct (although I haven't provided the actual details here) but I suspect there is an issue with my connection string.

Happy to provide more details. Any help is much appreciated, thanks

P.S. Apologies for my lack of knowledge in Access. I just don't use it very often

1 Answer 1

1

to use SQL Server security remove Trusted_Connection=yes.

https://www.connectionstrings.com/microsoft-sql-server-odbc-driver/

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

9 Comments

Hi Zdravko, thanks for the response. Unfortunately I've tried that but I still get the same error as in my post
Change Driver=SQL Server to Driver={SQL Server}. What version of SQL server?
Just tried that, same result unfortunately. Could it be possible that SQL server is rejecting the connection because it's not windows authentication?
yes, it is possible. the sql server needs to be configured to accept sql server auth. try to connect with something like the Enterprise Manager or similar tool to verify that the account actually works.
no, that is windows auth. in the login dialog select SQL Server auth and then try.
|

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.