Public Sub CheckRecordExistence()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sql As String
' SQLクエリを準備します。ここではEmployeeIDが1のレコードを検索しています。
sql = "SELECT * FROM Employees WHERE EmployeeID = 1"
' 現在のデータベースを開きます。
Set db = CurrentDb()
' SQLクエリを実行し、結果をRecordsetオブジェクトに格納します。
Set rs = db.OpenRecordset(sql, dbOpenDynaset)
' レコードが存在するかどうかをチェックします。
If rs.EOF And rs.BOF Then
MsgBox "対象のレコードは存在しません。", vbInformation, "レコードなし"
Else
MsgBox "対象のレコードが見つかりました。", vbInformation, "レコードあり"
End If
' オブジェクトを閉じてリソースを解放します。
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
More than 1 year has passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme