如何在Access 2010中显示ADO记录集

时间:2018-03-13 15:17:27

标签: sql ms-access-2010 recordset adoconnection

我需要一些帮助来解决这个问题。顺便说一句,我是VB的新手。我目前有一个存储过程接受3个参数,并在执行时返回临时表。我在SQL中测试了我的程序,它按预期工作。

我使用Access表单作为我的前端。它是通过3个文本框控件接受3个参数,然后执行我的程序'点击' (用户单击搜索按钮以执行并启动SQL)。下面的代码实现了这一点,直到它在表格中显示我的记录集为止。我坚持如何通过使用ADO属性来显示表格中的程序结果我可以在数据表格中显示记录集或将它们插入表格中。

代码似乎在这一行窒息,我试图查看我的记录集:     rs.Open cmd

访问VB代码

Private Sub cmdExecuteQuery_Click()
   Dim conn As ADODB.Connection
   Dim cmd As ADODB.Command
   Dim rs As ADODB.Recordset
   Dim accessionno As String

   Set conn = New ADODB.Connection
   Set rs = New ADODB.Recordset

   conn.ConnectionString = "Driver={SQL Server};Server=***;Database=***;Uid=****;Pwd=***;"
   conn.Open

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

  cmd.Parameters.Append cmd.CreateParameter("@worksheetno", adVarChar, 
  adParamInput, 10, Forms!RepeatForm!txtEnterWrkSheetno)
  cmd.Parameters.Append cmd.CreateParameter("@name", adVarChar, 
  adParamInput, 10, Forms!RepeatForm!txtEnterName)
  cmd.Parameters.Append cmd.CreateParameter("@year", adVarChar, 
  adParamInput, 4, Forms!RepeatForm!txtEnterYear)

  With rs
     .ActiveConnection = conn
     .CursorType = adOpenForwardOnly
     .CursorLocation = adUseServer
  End With

  Set rs = cmd.Execute()
  DoCmd.OpenForm "frmRepeats", acViewNormal
  rs.Open cmd

  rs.Close
  conn.Close

  Set rs = Nothing
  Set cmd = Nothing
  Set conn = Nothing
End Sub

SQL

CREATE PROCEDURE [dbo].[NameofStoredProcedure] @worksheetno varchar(10), 
@name varchar(10), @year varchar(4)
AS 
BEGIN
CREATE TABLE #Temp (accessionno varchar (100),
No varchar (100),
worksheetno varchar (10),
name varchar(100),
location varchar (10),
Result varchar (100),
year varchar (4))

INSERT INTO #Temp
SELECT DISTINCT
    a.accessionno,
    e.name + substring(a.Year, 3,2) + '-' + convert(varchar(10), 
    c.SampleTestNum) AS No,
    a.worksheetno,
    b.name,
    c.platetext AS Location,
    d.finalcomment AS Result,
    a.Year
FROM
    tb_sample a (nolock), tb_patient b (nolock), tb_plate c (nolock), 
    tb_finalresult d (nolock), tb_testcode e (nolock)
WHERE
    a.sample_id = b.sample_id
    and a.sample_id = c.sample_id
    and a.sample_id = d.sample_id
    and a.test_id = e.test_id
    and finalcomment like '%1061%'
    and worksheetno = @worksheetno
    and e.name = @name
    and a.year = @year
ORDER BY No
END
select distinct * from #Temp
drop table #Temp
GO

0 个答案:

没有答案