However, sometimes you still need to call Stored Procedures, Views or create dynamic query statements to be passed directly to MS-SQL.
You can still achieve this by creating a passthru query in Access.
Below is a little bit of code I put together to allow you to run SQL statements using a passthru query called "SQLCall" or choosing one of your own.
To set things up, you'll need to create a passthru query in Access (see http://support.microsoft.com/kb/303968 for how to do this) called SQLCall and then drop this code into a module:
Public Sub SQLExec(ByVal sSQL As String _ , Optional bRetRecords As Boolean = False _ , Optional bExecute As Boolean = True _ , Optional sQueryName As String = "SQLCall" _ , Optional vConnect As Variant) ' vConnect : Optional connection string ' bRetRecords : Optional set return records on / off. Default: on ' sQueryName : Optional set alternate location for query storage On Error GoTo ErrorHandler Dim db As DAO.Database Dim qdf As DAO.QueryDef Dim sConnect As String Set db = CurrentDb Set qdf = db.QueryDefs(sQueryName) If IsMissing(vConnect) Then sConnect = qdf.Connect Else sConnect = CStr(vConnect) End If qdf.SQL = sSQL qdf.Connect = sConnect qdf.ReturnsRecords = bRetRecords If bExecute Then qdf.Execute GoTo ErrorExit ErrorHandler: Select Case Err.Number Case Else MsgBox cErrorIntro & vbCrLf _ & vbCrLf & Err.Number _ & vbCrLf & Err.Description _ & vbCrLf & Err.Source _ , vbCritical + vbOKOnly, "Error" End Select Resume Next ErrorExit: End Sub
You can use this Sub to do thinks like:
Call the untag all contacts stored procedure
SQLExec "EXEC spUntagAllContacts;", False, TrueDelete Contact #5
SQLExec "DELETE FROM tblContacts Where ContactID = 5;"Call the SP to tag a contact based on ContactID
SQLExec "EXEC spTagContact " & Nz(ContactID, 0) & ";"Do a select query and make the results accessible from the SQLContactList passthru
SQLExec "SELECT * from tblContacts WHERE ContactID < 100;", True, False, "SQLContactList"Call the stored procedure to get the current users security groups and make the results accessible from the SQLSecurityGroupList passthru
SQLExec "EXEC spListMySecurityGroups '" & UserName & "';", True, False, "SQLSecurityGroupList"
0 comments:
Post a Comment