我在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/,我仍然对如何制作它们感到困惑。任何帮助将不胜感激
答案 0 :(得分:4)
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", adVarChar, adParamInput, 60, "value for InstrumentName")
'...
cmd.Execute
conn.Close
答案 1 :(得分:-2)
使用已保存的传递查询。
然后您的代码变为:
With currentdb.querydefs("MyPass")
.sql = "exec StoreProcName " & strBach & “,” & strInstrmentName
.execute
End With
所以,你这里只需要两行代码。如果使用保存的传递查询,您甚至不必声明任何连接字符串甚至任何变量。