LoginSignup
0
0

More than 1 year has passed since last update.

AccessVBAでIDを1から最後まで順に取得する

Last updated at Posted at 2023-04-17

AccessでExcelみたいに1行目から順にデータがなくなるまで繰り返し処理するようなプログラムをAccessで実現する方法


テーブル名

ID フィールド1 フィールド2
1 a z
2 b y
3 c x
4 d w

vba
Sub Test()
    Dim i
    i = 1
    Do While DLookup("ID","テーブル名","ID=" & i) <> ""
       Debug.print DLookup("ID","テーブル名","ID=" & i)
       i = i + 1
    Loop
End Sub

実行結果
1
2
3
4

上の例だと、DLookupを使うことで「テーブル名」という名前のテーブルから「IDのフィールドの値がi」である「IDのフィールドの値を取得」することができる。
該当するIDがないときは「Null」を返すことを利用し、IDを1から順に最後まで繰り返し処理することが可能

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0