设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 659|回复: 2
打印 上一主题 下一主题

[其它] 一起研究这个,谢谢!

[复制链接]
跳转到指定楼层
1#
发表于 2006-3-24 02:57:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
以下代码是Access帮助里的示例代码(搜索关键字‘存储过程’),如何把它改成输出记录集到一个子窗体,即不以debug.print方式显示出来,我要把输出的记录集用于子窗体的源记录,请帮忙,很急啊!不胜感激!

'BeginActiveConnectionVB

    'To integrate this code
    'replace the data source and initial catalog values
    'in the connection string

Public Sub Main()
    On Error GoTo ErrorHandler

    'recordset, command and connection variables
    Dim Cnxn As ADODB.connection
    Dim cmdByRoyalty As ADODB.Command
    Dim prmByRoyalty As ADODB.Parameter
    Dim rstByRoyalty As ADODB.Recordset
    Dim rstAuthors As ADODB.Recordset
    Dim strCnxn As String
    Dim strSQLAuthors As String
    Dim strSQLByRoyalty As String
     'record variables
    Dim intRoyalty As Integer
    Dim strAuthorID As String

    ' Define a command object for a stored procedure
    Set Cnxn = New ADODB.connection
    strCnxn = "rovider=sqloledb;Data Source=localhost;" & _
        "Initial Catalog=Pubs;Integrated Security=SSPI;"
    Cnxn.Open strCnxn
   
    Set cmdByRoyalty = New ADODB.Command
    Set cmdByRoyalty.ActiveConnection = Cnxn
    ' Set the criteria
    strSQLByRoyalty = "byroyalty"
    cmdByRoyalty.CommandText = strSQLByRoyalty
    cmdByRoyalty.CommandType = adCmdStoredProc
    cmdByRoyalty.CommandTimeout = 15
      
    ' Define the stored procedure's input parameter
    intRoyalty = Trim(InputBox("Enter royalty:"))
    Set prmByRoyalty = New ADODB.Parameter
    prmByRoyalty.Type = adInteger
    prmByRoyalty.Size = 3
    prmByRoyalty.Direction = adParamInput
    prmByRoyalty.value = intRoyalty
   
    cmdByRoyalty.Parameters.Append prmByRoyalty
   
    ' Create a recordset by executing the command.
    Set rstByRoyalty = cmdByRoyalty.Execute()
      
    ' Open the Authors Table to get author names for display
    Set rstAuthors = New ADODB.Recordset
    strSQLAuthors = "Authors"
   
    'rstAuthors.Open strSQLAuthors, strCnxn, , , adCmdTable
    rstAuthors.Open strSQLAuthors, strCnxn, adOpenForwardOnly, adLockReadOnly, adCmdTable
    'the above two lines of code are identical as the default values for
    'CursorType and LockType arguments match those shown
   
    ' Print the recordset and add author names from Table
    Debug.Print "Authors with " & intRoyalty & _
       " percent royalty"
      
    Do Until rstByRoyalty.EOF
        strAuthorID = rstByRoyalty!au_id
        Debug.Print , rstByRoyalty!au_id & ", ";
        rstAuthors.Filter = "au_id = '" & strAuthorID & "'"
        Debug.Print rstAuthors!au_fname & " " & _
            rstAuthors!au_lname
        rstByRoyalty.MoveNext
    Loop

    ' clean up
    rstAuthors.Close
    rstByRoyalty.Close
    Cnxn.Close
    Set rstAuthors = Nothing
    Set rstByRoyalty = Nothing
    Set Cnxn = Nothing
    Exit Sub
   
ErrorHandler:
    ' clean up
    If Not rstAuthors Is Nothing Then
        If rstAuthors.State = adStateOpen Then rstAuthors.Close
    End If
    Set rstAuthors = Nothing
   
    If Not rstByRoyalty Is Nothing Then
        If rstByRoyalty.State = adStateOpen Then rstByRoyalty.Close
    End If
    Set rstByRoyalty = Nothing
   
    If Not Cnxn Is Nothing Then
        If Cnxn.State = adStateOpen Then Cnxn.Close
    End If
    Set Cnxn = Nothing
   
    If err <> 0 Then
        MsgBox err.Source & "-->" & err.Description, , "Error"
    End If
End Sub
'EndActiveConnectionVB
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2006-3-24 03:34:00 | 只看该作者
光凭上面的代码怎么改啊,把你的实力示例发上来,针对例子来做才行啊。
3#
 楼主| 发表于 2006-3-24 17:16:00 | 只看该作者
这个是Access帮助里的代码,把它整个拷到窗体里,然后call main就行的,当然还有一个是sql server的数据库pubs
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-11-15 15:52 , Processed in 0.082772 second(s), 26 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表