设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 488|回复: 0

VBA: Finding the record(s) in RecordSet

[复制链接]
发表于 2011-9-15 14:31:48 | 显示全部楼层 |阅读模式
Finding a Record Based on Multiple Criteria
  1. Sub Find_WithFilter()
  2.    Dim conn As ADODB.Connection
  3.    Dim rst As ADODB.Recordset

  4.    Set conn = New ADODB.Connection
  5.    conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurrentProject.Path & _
  6.       "\mydb.mdb"

  7.    Set rst = New ADODB.Recordset
  8.    rst.Open "Employees", conn, adOpenKeyset, adLockOptimistic
  9.    rst.Filter = "TitleOfCourtesy ='Ms.' and Country ='USA'"
  10.    Do Until rst.EOF
  11.       Debug.Print rst.Fields("LastName").Value
  12.       rst.MoveNext
  13.    Loop

  14.    rst.Close
  15.    Set rst = Nothing
  16.    conn.Close
  17.    Set conn = Nothing
  18. End Sub
复制代码
Finding Records Using the Find Method
  1. Sub Find_WithFind()
  2.    Dim conn As ADODB.Connection
  3.    Dim rst As ADODB.Recordset

  4.    Set conn = New ADODB.Connection
  5.    conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurrentProject.Path & "\mydb.mdb"
  6.    Set rst = New ADODB.Recordset
  7.    rst.Open "Employees", conn, adOpenKeyset, adLockOptimistic

  8.    rst.Find "TitleOfCourtesy ='Ms.'"
  9.    Do Until rst.EOF
  10.       Debug.Print rst.Fields("LastName").Value
  11.       rst.Find "TitleOfCourtesy ='Ms.'", SkipRecords:=1, _
  12.           SearchDirection:=adSearchForward
  13.    Loop

  14.    rst.Close
  15.    Set rst = Nothing
  16.    conn.Close
  17.    Set conn = Nothing
  18. End Sub
复制代码
Finding a Specific Record in a Recordset
  1. Sub FindProject()
  2.     Dim strSQL As String
  3.     Dim rst As ADODB.Recordset
  4.     Set rst = New ADODB.Recordset

  5.     rst.ActiveConnection = CurrentProject.Connection
  6.     rst.CursorType = adOpenStatic
  7.     rst.Open "Select * from Employees"

  8.     'Attempt to find a specific project
  9.     strSQL = "[EmployeeID] = " & 1
  10.     rst.Find strSQL

  11.     'Determine if the specified project was found
  12.     If rst.EOF Then
  13.         msgBox lngValue & " Not Found"
  14.     Else
  15.         msgBox lngValue & " Found"
  16.     End If
  17.     rst.Close
  18.     Set rst = Nothing
  19. End Sub
复制代码
Finding the Record Position
  1. Sub FindRecordPosition()
  2.    Dim conn As ADODB.Connection
  3.    Dim rst As ADODB.Recordset
  4.    Dim strConn As String

  5.    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  6.       "Data Source=" & CurrentProject.Path & _
  7.       "\mydb.mdb"

  8.    Set conn = New ADODB.Connection
  9.    conn.Open strConn

  10.    Set rst = New ADODB.Recordset
  11.    With rst
  12.       .Open "Select * from Employees", conn, adOpenKeyset, _
  13.           adLockOptimistic, adCmdText
  14.    Debug.Print .AbsolutePosition
  15.       .Move 3 ' move forward 3 records
  16.       Debug.Print .AbsolutePosition
  17.       .MoveLast ' move to the last record
  18.       Debug.Print .AbsolutePosition
  19.       Debug.Print .RecordCount
  20.       .Close
  21.    End With

  22.    Set rst = Nothing
  23.    conn.Close
  24.    Set conn = Nothing
  25. End Sub
复制代码
Change column data case
  1. Sub exaRecordsetEdit()

  2.     Dim db As Database
  3.     Dim rs As Recordset
  4.    
  5.     Set db = CurrentDb
  6.     Set rs = db.OpenRecordset("Employees")
  7.    
  8.     rs.MoveFirst
  9.     Do While Not rs.EOF
  10.        rs.Edit
  11.        rs!Title = UCase$(rs!Title)
  12.        rs.Update
  13.        rs.MoveNext
  14.     Loop
  15.    
  16.     rs.Close
  17.    
  18. End Sub
复制代码
SQL with where clause
  1. Sub MyFirstConnection()
  2.     Dim myConnection As ADODB.Connection
  3.     Dim myRecordset As ADODB.Recordset
  4.     Dim strSQL As String
  5.     Dim strSearch As String
  6.    
  7.     strSearch = "Joe"
  8.    
  9.     strSQL = "SELECT txtCustFirstName, txtCustLastName FROM tblCustomer" & _
  10.               " WHERE txtCustLastName = " & " '" & strSearch & "'"
  11.    
  12.     Set myConnection = CurrentProject.Connection
  13.    
  14.     Set myRecordset = New ADODB.Recordset
  15.     myRecordset.Open strSQL, myConnection
  16.    
  17.     Do Until myRecordset.EOF
  18.        Debug.Print myRecordset.Fields("txtCustFirstName"), _
  19.                    myRecordset.Fields("txtCustLastName")
  20.        myRecordset.MoveNext
  21.     Loop
  22.     myRecordset.Close
  23.     myConnection.Close
  24.     Set myConnection = Nothing
  25.     Set myRecordset = Nothing
  26. End Sub
复制代码
NoMatch property in Recordset
  1. Sub SeekByPrice(curPrice As Currency)
  2.   Dim db As Database
  3.   Dim rec As Recordset
  4.   Dim strSQL As String
  5.   strSQL = "tblSales"
  6.   Set db = CurrentDb()
  7.   Set rec = db.OpenRecordset(strSQL)
  8.   rec.Index = "AmountPaid"
  9.   rec.Seek "=", curPrice
  10.   
  11.   If rec.NoMatch = True Then
  12.     Debug.Print "No orders cost " & FormatCurrency(curPrice)
  13.   Else
  14.     Debug.Print "Order No. " & rec("SalesID") & " placed on " & _
  15.          FormatDateTime(rec("DateOrdered"), vbLongDate) & _
  16.          " cost " & FormatCurrency(rec("AmountPaid"))
  17.   End If
  18.   rec.Close
  19. End Sub
复制代码




您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2026-2-5 03:27 , Processed in 0.011863 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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