本文介绍了如何通话程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如何使用visual 基本调用来自Access的SQL Server上的程序? 谢谢, mw Hi,How to call procedure, which is on SQL Server from Access using visualbasic? Thanks,mw 推荐答案 我们看不到存储过程,所以我们不知道你想要的是什么。存储过程可用于执行许多操作 - 有时它们返回 一个记录集,有时是多个记录集,有时甚至没有。有时他们 有参数(输入和输出),有时没有。所以你应该发布你的存储过程并说出你需要做什么。 但你可以尝试在你问之前进行搜索 - 必须有足够的 例子。 We can''t see the stored procedure, so we don''t know what you are trying todo. Stored procedures can be used to do many things - sometimes they returna recordset, sometimes multiple recordsets, sometimes none. Sometimes theyhave parameters (input and output) and sometimes none. So really you shouldpost your stored procedure and say what you need to do with it.But you could try doing a search before you ask - there must be plenty ofexamples. 我不确定您的程序名称是否为年到年销售额匹配 它做什么。如果想要创建一个基于一年的记录集, 那么也许你只需要一个输入参数 - 年份。你还需要 小心在...之间当使用日期作为订单日期时,也可能包含时间部分。我也会避免在 存储过程的名称中加入空格,因为这意味着你必须使用愚蠢的括号。 我不知道是否如果要将其包装在vba函数中,我会费心在存储的 过程中引发错误。您可以使用 vba编码来检查您是否有开始日期和结束日期。如果你要开始提出错误,那么也许你应该考虑让你的 存储过程提供一个返回值来显示它是否成功。 然后你必须得到你的vba编码来获得这个输出的价值 参数。我这里没有这样做,但是这给了你一般的想法: Private Sub cmdTest_Click() 错误GoTo Err_Handler Dim cnn As ADODB.Connection Dim cmd As ADODB.Command Dim prm As ADODB.Parameter Dim rst作为ADODB.Recordset Dim strConn As String Dim dteStart As Date Dim dteEnd As Date dteStart = DateSerial(2006,1,1) dteEnd = DateSerial(2006,2,1) strConn ="提供商= SQLOLEDB;" &安培; _ " Data Source = MyServer;" &安培; _ " Initial Catalog = MyDatabase;" &安培; _ 集成安全性= SSPI 设置cnn =新ADODB.Connection cnn.Open strConn 设置cmd =新ADODB.Command cmd.ActiveConnection = cnn cmd .CommandText =" [年度销售额]" cmd.CommandType = adCmdStoredProc 设置prm = cmd.CreateParameter(" ; @ BeginningDate",adDate,adParamInput ,, dteStart) cmd.Parameters.Append prm 设置prm = cmd .CreateParameter(" @EndingDate",adDate,adParamInput ,, dteEnd) cmd.Parameters.Append prm 设置rst = cmd.Execute 虽然不是rst.EOF Debug.Print rst.Fields(" OrderID") rst.MoveNext Wend MsgBox" Done",vbInformation Exit_Handler: 如果不是第一个那么 如果rst.State<> adStateClosed然后 rst.Close 结束如果 设置rst = Nothing 结束如果 设置prm = Nothing 设置cmd = Nothing 如果不是cnn则没有那么 如果cnn.State<> adStateClosed然后 cnn.Close 结束如果 设置cnn = Nothing 结束如果 退出Sub Err_Handler: MsgBox Err.Description,vbExclamation,"错误号:" &安培; Err.Number 恢复Exit_Handler 结束子 I''m not sure whether the name of your procedure "Year to Year Sales" matcheswhat it does. If the idea is to create a recordset based on a single year,then perhaps you only need one input parameter - the year. You also need tobe careful with "between" when using dates as the order date may alsocontain a time portion. I would also avoid putting spaces in the names ofstored procedures as it means you have to use the silly brackets. I don''t know whether I would bother to raise an error in the storedprocedure if you are going to wrap it in a vba function. You could use thevba coding to check whether you have a start and end date. If you are goingto start raising errors, then perhaps you should think about getting yourstored procedure to provide a return value to show if it was successful.You then have to get your vba coding to get the value of this outputparameter. I have not done this here, but this gives you the general idea:Private Sub cmdTest_Click() On Error GoTo Err_Handler Dim cnn As ADODB.ConnectionDim cmd As ADODB.CommandDim prm As ADODB.ParameterDim rst As ADODB.RecordsetDim strConn As StringDim dteStart As DateDim dteEnd As Date dteStart = DateSerial(2006, 1, 1) dteEnd = DateSerial(2006, 2, 1) strConn = "Provider=sqloledb;" & _"Data Source=MyServer;" & _"Initial Catalog=MyDatabase;" & _"Integrated Security=SSPI" Set cnn = New ADODB.Connection cnn.Open strConn Set cmd = New ADODB.Command cmd.ActiveConnection = cnn cmd.CommandText = "[Year to Year Sales]" cmd.CommandType = adCmdStoredProc Set prm = cmd.CreateParameter("@BeginningDate", adDate, adParamInput, ,dteStart)cmd.Parameters.Append prm Set prm = cmd.CreateParameter("@EndingDate", adDate, adParamInput, ,dteEnd)cmd.Parameters.Append prm Set rst = cmd.Execute While Not rst.EOFDebug.Print rst.Fields("OrderID")rst.MoveNextWend MsgBox "Done", vbInformation Exit_Handler: If Not rst Is Nothing ThenIf rst.State <> adStateClosed Thenrst.CloseEnd IfSet rst = NothingEnd If Set prm = Nothing Set cmd = Nothing If Not cnn Is Nothing ThenIf cnn.State <> adStateClosed Thencnn.CloseEnd IfSet cnn = NothingEnd If Exit Sub Err_Handler:MsgBox Err.Description, vbExclamation, "Error No: " & Err.NumberResume Exit_Handler End Sub 这篇关于如何通话程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 02:09