0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Access mdbを全検索

Posted at
Sub SearchAllTables()
    Dim db As DAO.Database
    Dim tdf As DAO.TableDef
    Dim rs As DAO.Recordset
    Dim fld As DAO.Field
    Dim searchTerm As String
    Dim sql As String
    Dim found As Boolean

    searchTerm = InputBox("検索したい値を入力してください")

    Set db = CurrentDb

    For Each tdf In db.TableDefs
        ' システムテーブルや隠しテーブルを除外
        If Left(tdf.Name, 4) <> "MSys" Then
            On Error Resume Next
            Set rs = db.OpenRecordset("SELECT * FROM [" & tdf.Name & "]", dbOpenSnapshot)
            If Err.Number <> 0 Then
                Err.Clear
                GoTo NextTable
            End If
            On Error GoTo 0

            Do While Not rs.EOF
                For Each fld In rs.Fields
                    If Not IsNull(fld.Value) Then
                        If InStr(CStr(fld.Value), searchTerm) > 0 Then
                            Debug.Print "見つかりました:" & vbCrLf & _
                                        "テーブル: " & tdf.Name & vbCrLf & _
                                        "フィールド: " & fld.Name & vbCrLf & _
                                        "値: " & fld.Value & vbCrLf & _
                                        "--------------------"
                            found = True
                        End If
                    End If
                Next fld
                rs.MoveNext
            Loop
            rs.Close
NextTable:
        End If
    Next tdf

    If Not found Then
        MsgBox "該当データは見つかりませんでした。", vbInformation
    Else
        MsgBox "検索完了。結果は[イミディエイトウィンドウ]で確認してください。", vbInformation
    End If

    Set rs = Nothing
    Set tdf = Nothing
    Set db = Nothing
End Sub

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?