レコード数/最大値/最小値 取得


[ADO]


  1. Option Compare Database
  2. Option Explicit
  3. '==========================================================================================
  4. 'General Module
  5. '==========================================================================================
  6. '/ ConnectionString
  7. Public Const CONNECTIONTEXT As String = "Provider=SQLOLEDB.1;" & _
  8. "Data Source=(Computer Name)\(Instance Name);" & _
  9. "Initial Catalog=(Dtabase Name);" & _
  10. "Intergrated Security=SSPI;" & _
  11. "USER ID=(User ID);" & _
  12. "Password=(Password);"
  13.  

  1. Public Function lngDCount(strFields As String, _
  2. strTable As String, _
  3. Optional strWhere As String = "") As Long
  4. '概要: lngDCount:指定された条件のレコード件数を返します。
  5. '引数: strField :"*"と指定
  6. ' strTable :テーブル名を指定
  7. ' strWhere :条件を指定
  8. '返値: Long :件数
  9. '備考:
  10.  
  11. '/ On Error は省略
  12.  
  13. '/ 定義
  14. Dim cn As ADODB.Connection
  15. Dim rs As ADODB.Recordset
  16. Set cn = New ADODB.Connection
  17. Set rs = New ADODB.Recordset
  18.  
  19. '/ ConnectionStringとRecordsetの指定
  20. cn.Open CONNECTIONTEXT
  21. rs.CursorType = adOpenForwardOnly
  22. rs.LockType = adLockReadOnly
  23. rs.ActiveConnection = cn
  24. rs.Source = "SELECT COUNT(*) FROM " & strTable
  25.  
  26. '/ Where条件の存在
  27. If strWhere <> "" Then
  28. rs.Source = rs.Source & " WHERE " & strWhere
  29. End If
  30.  
  31. '/ Recordsetを開く
  32. rs.Open
  33.  
  34. '/ 値を返す
  35. lngDCount = CLng(rs.Fields(0))
  36.  
  37. '/ RecordsetとConnectionを閉じる
  38. rs.Close
  39. cn.Close
  40.  
  41. '/ 定義の開放
  42. Set rs = Nothing
  43. Set cn = Nothing
  44.  
  45. End Function
  46.  
  1. Public Function lngDMax(strFields As String, strTable As String, _
  2. Optional strWhere As String = "") As Long
  3. '概要: lngDMax :指定された条件の最大値を返します。
  4. '引数: strField :フィールド名と指定
  5. ' strTable :テーブル名を指定
  6. ' strWhere :条件を指定
  7. '返値: Long :件数
  8. '備考:
  9.  
  10. '/ On Error は省略
  11.  
  12. '/ 定義
  13. Dim cn As ADODB.Connection
  14. Dim rs As ADODB.Recordset
  15. Set cn = New ADODB.Connection
  16. Set rs = New ADODB.Recordset
  17.  
  18. '/ ConnectionStringとRecordsetの指定
  19. cn.Open CONNECTIONTEXT
  20. rs.CursorType = adOpenForwardOnly
  21. rs.LockType = adLockReadOnly
  22. rs.ActiveConnection = cn
  23. rs.Source = "SELECT MAX([" & strFields & "]) FROM " & strTable
  24.  
  25. '/ Where条件の存在
  26. If strWhere <> "" Then
  27. rs.Source = rs.Source & " WHERE " & strWhere
  28. End If
  29.  
  30. '/ Recordsetを開く
  31. rs.Open
  32.  
  33. '/ 値を返す
  34. lngDMax = CLng(rs.Fields(0))
  35.  
  36. '/ RecordsetとConnectionを閉じる
  37. rs.Close
  38. cn.Close
  39.  
  40. '/ 定義の開放
  41. Set rs = Nothing
  42. Set cn = Nothing
  43.  
  44. End Function
  45.  
  1. Public Function lngDMin(strFields As String, strTable As String, _
  2. Optional strWhere As String = "") As Long
  3. '概要: lngDMin :指定された条件の最小値を返します。
  4. '引数: strField :フィールド名と指定
  5. ' strTable :テーブル名を指定
  6. ' strWhere :条件を指定
  7. '返値: Long :件数
  8. '備考:
  9.  
  10. '/ On Error は省略
  11.  
  12. '/ 定義
  13. Dim cn As ADODB.Connection
  14. Dim rs As ADODB.Recordset
  15. Set cn = New ADODB.Connection
  16. Set rs = New ADODB.Recordset
  17.  
  18. '/ ConnectionStringとRecordsetの指定
  19. cn.Open CONNECTIONTEXT
  20. rs.CursorType = adOpenForwardOnly
  21. rs.LockType = adLockReadOnly
  22. rs.ActiveConnection = cn
  23. rs.Source = "SELECT MIN([" & strFields & "]) FROM " & strTable
  24.  
  25. '/ Where条件の存在
  26. If strWhere <> "" Then
  27. rs.Source = rs.Source & " WHERE " & strWhere
  28. End If
  29.  
  30. '/ Recordsetを開く
  31. rs.Open
  32.  
  33. '/ 値を返す
  34. lngDMin = CLng(rs.Fields(0))
  35.  
  36. '/ RecordsetとConnectionを閉じる
  37. rs.Close
  38. cn.Close
  39.  
  40. '/ 定義の開放
  41. Set rs = Nothing
  42. Set cn = Nothing
  43.  
  44. End Function
  45.  








名前:
コメント:
最終更新:2008年05月23日 15:39