以下为朱亦文提供的回复,代为上传:
以下示例使用 CreateQueryDef 和 OpenRecordset 方法以及 SQL 属性,查询 Microsoft SQL Server 示例数据库 Pubs 中的书名表,并返回最畅销书籍的书名和书名标识符。然后查询作者表,并指示用户根据每个作者的版税份额向其发送红利支票(总红利为 ¥1,000,每个作者应收到该金额的一定份额)。
Visual Basic for Applications
Sub ClientServerX2()
Dim dbsCurrent As Database
Dim qdfBestSellers As QueryDef
Dim qdfBonusEarners As QueryDef
Dim rstTopSeller As Recordset
Dim rstBonusRecipients As Recordset
Dim strAuthorList As String
' Open a database from which QueryDef objects can be
' created.
Set dbsCurrent = OpenDatabase("DB1.mdb")
' Create a temporary QueryDef object to retrieve
' data from a Microsoft SQL Server database.
Set qdfBestSellers = dbsCurrent.CreateQueryDef("")
With qdfBestSellers
' Note: The DSN referenced below must be configured to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
.Connect = "ODBC;DATABASE=pubs;DSN=Publishers"
.SQL = "SELECT title, title_id FROM titles " & _
"ORDER BY ytd_sales DESC"
Set rstTopSeller = .OpenRecordset()
rstTopSeller.MoveFirst
End With
' Create a temporary QueryDef to retrieve data from
' a Microsoft SQL Server database based on the results from
' the first query.
Set qdfBonusEarners = dbsCurrent.CreateQueryDef("")
With qdfBonusEarners
' Note: The DSN referenced below must be configured to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
.Connect = "ODBC;DATABASE=pubs;DSN=Publishers"
.SQL = "SELECT * FROM titleauthor " & _
"WHERE title_id = '" & _
rstTopSeller!title_id & "'"
Set rstBonusRecipients = .OpenRecordset()
End With
' Build the output string.
With rstBonusRecipients
Do While Not .EOF
strAuthorList = strAuthorList & " " & _
!au_id & ": $" & (10 * !royaltyper) & vbCr
.MoveNext
Loop
End With
' Display results.
MsgBox "Please send a check to the following " & _
"authors in the amounts shown:" & vbCr & _
strAuthorList & "for outstanding sales of " & _
rstTopSeller!Title & "."
rstTopSeller.Close
dbsCurrent.Close
End Sub