sql – 在VBA中从Access模块​​传递参数时调用存储过程

前端之家收集整理的这篇文章主要介绍了sql – 在VBA中从Access模块​​传递参数时调用存储过程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Access 2010中使用Microsoft sql Server 2008后端.我有一个存储过程将新值(由参数提供)插入表中.分配给参数的值是从存储在文件夹中的文件获取的. Windows文件系统用于扫描特定文件夹以生成其中的文件列表.对于每个扫描的文件,调用存储过程,并将FileName和QueueId(没有扩展名的文件名)以及其他值用作调用的存储过程的参数.存储过程用于为表的每个文件创建新记录.
Public Function CreateInstrumentInterfaceLogRecords(BatchID As Long,InstrumentName As            String) As Boolean
On Error GoTo HandleError

Dim objFSO As FileSystemObject
Dim AllFiles As Object
Dim objFolder As Object
Dim objFile As Object
Dim FileExt As String
Dim strsql As String

CreateInstrumentInterfaceLogRecords = False
strsql = "SELECT FileExt FROM tlkpInstrument"
FileExt = ExecuteScalar(strsql)


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(NewPath) 

'NewPath is a public variable that holds the path to the folder'

Set AllFiles = objFolder.Files
For Each objFile In AllFiles

FileName = objFile.FileName
QuenueId = Replace(FileName,FileExt,"")

'call procedure to pass values'

Next objFile

ExitProc:
Exit Function
HandleError:
MsgBox Err.Number & " " & Err.Description & " in CreateInstrumentInterfaceLogRecords"
GoTo ExitProc

End Function

并且存储过程是:

CREATE PROCEDURE upInsertToInstrumentInterfaceLog @BatchID nvarchar(60),@InstrumentName nvarchar(60),@FIleName nvarchar(60),@QueueId nvarchar(60) 
AS
INSERT INTO tblInstrumentInterfaceLog (BatchID,InstrumentName,FileName,QueueID,DateOfBatch,Folder)
VALUES (@BatchID,@InstrumentName,@FileName,@QueueId,getdate(),'New');
GO

所有这些例子都没有给我一个创建连接和调用过程的可靠想法.我给出的一些建议是研究ExecuteNonquery如何工作,所以我一直试图找到与此相关的例子.以下是我发现的示例模板之一

Dim conn As ADODB.Connection 
Dim cmd As ADODB.Command 

Set conn = New ADODB.Connection 
conn.ConnectionString = “your connection String here” 
conn.Open 

Set cmd = New ADODB.Command 
cmd.ActiveConnection = conn 
cmd.CommandType = adCmdStoredProc 
cmd.CommandText = "put stored procedure name here" 

cmd.Execute 
conn.Close 

Set conn = Nothing 
Set cmd = Nothing

我不确定从这个例子中应该采取什么以及如何合并传递值.即使我已经访问了http://www.connectionstrings.com/,我仍然对如何制作它们感到困惑.任何帮助将不胜感激

解决方法

Dim conn As ADODB.Connection 
Dim cmd As ADODB.Command 

Set conn = New ADODB.Connection 
conn.ConnectionString = “your connection String here” 
conn.Open 

Set cmd = New ADODB.Command 
cmd.ActiveConnection = conn 
cmd.CommandType = adCmdStoredProc 
cmd.CommandText = "upInsertToInstrumentInterfaceLog" 

cmd.parameters.Append cmd.CreateParameter("@BatchID",adVarChar,adParamInput,60,"value for BatchID")   
cmd.parameters.Append cmd.CreateParameter("@InstrumentName","value for InstrumentName")   
'...

cmd.Execute 
conn.Close
原文链接:https://www.f2er.com/mssql/83917.html

猜你在找的MsSQL相关文章