Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS Access call SQL Server stored procedure

I have an MS Access application that contains all tables linked to SQL Server, so in MS Access VBA code or query I work with those tables very simple, I access them via name, like [Customers].

Also I have a stored procedure in SQL Server called sp_CopyData which I need to call from my VBA code. How can I do that without creating new connection to SQL Server (I already have it somewhere!? because I have access to tables)?

Or it's impossible? Appreciate any help. Thanks!

like image 863
ihorko Avatar asked Sep 14 '13 10:09

ihorko


People also ask

How do I run a SQL stored procedure in access?

On the create tab, click Query design in the other group. Click close in the Show Table dialog box without adding any tables or queries. On the Design tab, click Pass-Through in the Query Type workgroup.

Can we create stored procedure in MS Access?

Creates a stored procedure. The Microsoft Access database engine does not support the use of CREATE PROCEDURE, or any of the DDL statements, with non-Microsoft Jet database engine databases.

Can you use SQL commands in Access?

SQL is a computer language for working with sets of facts and the relationships between them. Relational database programs, such as Microsoft Office Access, use SQL to work with data. Unlike many computer languages, SQL is not difficult to read and understand, even for a novice.

How can we call stored procedure from data service?

One way is you make a stored procedure in database then call it in job using script below.. sql('datastore','exec Stored_Procedure_Name'); 2. Other way to achieve it – import your stored procedure in function..


2 Answers

The right answer found out, it should be like:

Dim qdef As DAO.QueryDef
Set qdef = CurrentDb.CreateQueryDef("")
qdef.Connect = CurrentDb.TableDefs("[ANY LINKED TABLE TO MS SQL SERVER]").Connect
qdef.SQL = "EXEC sp_CopyData"
qdef.ReturnsRecords = False  ''avoid 3065 error
qdef.Execute
like image 115
ihorko Avatar answered Sep 30 '22 04:09

ihorko


Create a pass-though query, and you can then use this throught the WHOLE application anytime you need to execute some T-SQL.

The code this becomes:

With CurrentDb.QueryDefs("qPass")
  .SQL = "exec sp_copydata"
  .ReturnsRecords = False  ''avoid 3065 error
  .Execute
End With
like image 38
Albert D. Kallal Avatar answered Sep 30 '22 04:09

Albert D. Kallal