gpt4 book ai didi

sql - 从 VBA 中的 Access 模块传递参数时调用存储过程

转载 作者:行者123 更新时间:2023-12-01 12:41:23 25 4
gpt4 key购买 nike

我在 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

我不太确定应该从这个示例中获取什么以及如何合并传递值。即使我 Access 过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", adVarChar, adParamInput, 60, "value for InstrumentName")
'...

cmd.Execute
conn.Close

关于sql - 从 VBA 中的 Access 模块传递参数时调用存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24248870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com