我需要一些帮助来解决这个问题。顺便说一句,我是 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