VBAでパラメータクエリを実行する方法

以下のようにクエリパラメータを指定することで、VBAでパラメータクエリを実行できます。
 
Dim dbs As Database
Dim qdf As QueryDef
Dim rst As Recordset

Set dbs = CurrentDb
‘クエリーのオブジェクトを取得します

Set qdf = dbs.QueryDefs(“クエリー名称”)
With qdf
  ‘クエリーのオブジェクトに対して設定を行います。”抽出する顧客ID” などがパラメータ名です
  .Parameters(“クエリのフィールド名1”) = 2 ・・・※
  .Parameters(“クエリのフィールド名2”) = 3
  
‘設定後のクエリーオブジェクトを元にRecordsetを開きます
  Set rst = .OpenRecordset
End With

ここで式※の右辺をフォームから[forms]![フォーム名]![テキストボックス名]などとすればフォームからパラメータを取得できます。

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny

タイトルとURLをコピーしました