LoginSignup
3
10

More than 3 years have passed since last update.

【VBA】OpenRecordsetをまとめる(個人用)

Last updated at Posted at 2020-01-15

初めに

OpenRecordset をよく使うのによく忘れるので個人用にまとめます

・OpenRecordset についてのリファレンス
・BOF,EOF プロパティについてのリファレンス
[参考にするサイトまとめ]
https://tsware.jp/study/vol16/vbabegin_37.htm
http://www7b.biglobe.ne.jp/~cbcnet/DAO/database.html
http://www.accessclub.jp/dao/OpenRecordset.html

OpenRecordset とは

OpenRecordsetメソッドとは、新しいRecordsetオブジェクトを作成し、
Recordsetsコレクションに追加するメソッドです。

構文.
Dim RS As Recordset
Set RS = CurrentDB.OpenRecordset(Name, Type, Options, LockEdit)

戻り値

Recordsetオブジェクト

Recordesetオブジェクトとは?

Recordset オブジェクトは、ベース テーブルからのレコード、またはコマンドの実行の結果得られたレコードの集合全体を表すオブジェクトです。
Recordset オブジェクトは、レコードセット内部の単一のレコードだけをカレント レコードとして常に参照します。

引数パラメータ

Set RS = CurrentDB.OpenRecordset(Name, Type, Options, LockEdit)

name(必須)

Recordset のレコードの取得元。
テーブル名、クエリ名、またはレコードを返す SQL ステートメントを指定できます

Set RS = CurrentDB.OpenRecordset("テーブル名")
Set RS = CurrentDB.OpenRecordset("クエリ名")
Set RS = CurrentDB.OpenRecordset("SELECT * FROM テーブル名")

Type(省略可)

わかりやすいからこちら参照

Options(省略可)

わかりやすいからこちら参照

LockEdit(省略可)

わかりやすいからこちら参照

レコードの追加・編集

追加

追加.
Set RS = CurrentDB.OpenRecordset("テーブル名")
RS.AddNew '新しいレコードを作りますよ
RS("カラム名") = "データ"
RS.update '確定

便利な追加方法

DRS.AddNew
For i = 0 To DRS.Fields.Count - 1
 DRS(i) = SRS(DRS(i).NAME)
Next i
DRS.Update

編集

編集.
Set RS = CurrentDB.OpenRecordset("テーブル名")
Do Until RS.BOF Or RS.EOF 'カレントレコードがあるか
  RS.Edit '今から編集しますよ 
  RS("カラム名") = "データ"
  RS.update '確定
  RS.MoveNext '次のレコードに行くよ
Loop
RS.close

BOF,EOFプロパティ

カレントレコードがあるか確認することができる
BOF プロパティと EOF プロパティのいずれかが True になっている場合は、
カレント レコードが存在しません。
・ある時はFalse
・ない時はTrue

オブジェクトの解放

recordsetオブジェクトを使わくなった時に開放する。

RS.close
もしくは
Set RS = Nothing

メモリ的にはどちらも変わらないらいしので
どちらでもいい。

3
10
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
3
10