Calling Stored Procedure while passing parameters from Access Module in VBA

前端 未结 2 482
孤街浪徒
孤街浪徒 2020-12-07 04:02

I am working in Access 2010 with a Microsoft SQL Server 2008 backend. I have a stored procedure that inserts new values(supplied by the parameters) into a table. The value

相关标签:
2条回答
  • 2020-12-07 04:21

    Use a saved pass-though query.

    You code then becomes:

    With currentdb.querydefs("MyPass")
      .sql = "exec StoreProcName " & strBach & “,” & strInstrmentName
      .execute
    End With
    

    So, you only need two lines of code here. You don't even have to declare any connection strings or even any variables if you use a saved pass-through query.

    0 讨论(0)
  • 2020-12-07 04:34
    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 
    
    0 讨论(0)
提交回复
热议问题