宣言 Typeプロパティ・ユーザー定義のデータ型宣言

※より実装に近く表示させる為、コードの改行を避けています。スマホ等で閲覧される際は向きを変えてご覧ください。

※実装するバージョンによってはバージョンアップの仕様により動作しないコードもあります。実装には動作確認の上ご使用下さい。


'次の例では、4 つの異なる Recordset オブジェクトに対応している Type プロパティの定数名を返して、Type プロパティを説明します。このプロシージャを実行するには、RecordsetType 関数が必要です。

Sub TypeX()

    Dim dbsNorthwind As Database
    Dim rstEmployees As Recordset

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")

    ' 既定値は dbOpenTable です。
    Set rstEmployees = _
        dbsNorthwind.OpenRecordset("Employees")
    Debug.Print _
        "Table-type recordset (Employees table): " & _
        RecordsetType(rstEmployees.Type)
    rstEmployees.Close

    Set rstEmployees = _
        dbsNorthwind.OpenRecordset("Employees", _
        dbOpenDynaset)
    Debug.Print _
        "Dynaset-type recordset (Employees table): " & _
        RecordsetType(rstEmployees.Type)
    rstEmployees.Close

    Set rstEmployees = _
        dbsNorthwind.OpenRecordset("Employees", _
        dbOpenSnapshot)
    Debug.Print _
        "Snapshot-type recordset (Employees table): " & _
        RecordsetType(rstEmployees.Type)
    rstEmployees.Close

    Set rstEmployees = _
        dbsNorthwind.OpenRecordset("Employees", _
        dbOpenForwardOnly)
    Debug.Print _
        "Forward-only-type recordset (Employees table): " & _
        RecordsetType(rstEmployees.Type)
    rstEmployees.Close

    dbsNorthwind.Close

End Sub

Function RecordsetType(intType As IntegerAs String

    Select Case intType
        Case dbOpenTable
            RecordsetType = "dbOpenTable"
        Case dbOpenDynaset
            RecordsetType = "dbOpenDynaset"
        Case dbOpenSnapshot
            RecordsetType = "dbOpenSnapshot"
        Case dbOpenForwardOnly
            RecordsetType = "dbOpenForwardOnly"
    End Select

End Function

'次の例では、Employees テーブル内のすべての Field オブジェクトの Type プロパティに対応している定数名を返して、Type プロパティを説明します。このプロシージャを実行するには、FieldType 関数が必要です。

Sub TypeX2()

    Dim dbsNorthwind As Database
    Dim fldLoop As Field

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")

    Debug.Print "Fields in Employees TableDef:"
    Debug.Print "    Type - Name"

    ' Employees テーブルの Fields コレクションを列挙します。
    For Each fldLoop In _
        dbsNorthwind.TableDefs Employees.Fields
        Debug.Print "        " & FieldType(fldLoop.Type) & _
            " - " & fldLoop.Name
    Next fldLoop

    dbsNorthwind.Close

End Sub

Function FieldType(intType As IntegerAs String

    Select Case intType
        Case dbBoolean
            FieldType = "dbBoolean"
        Case dbByte
            FieldType = "dbByte"
        Case dbInteger
            FieldType = "dbInteger"
        Case dbLong
            FieldType = "dbLong"
        Case dbCurrency
            FieldType = "dbCurrency"
        Case dbSingle
            FieldType = "dbSingle"
        Case dbDouble
            FieldType = "dbDouble"
        Case dbDate
            FieldType = "dbDate"
        Case dbText
            FieldType = "dbText"
        Case dbLongBinary
            FieldType = "dbLongBinary"
        Case dbMemo
            FieldType = "dbMemo"
        Case dbGUID
            FieldType = "dbGUID"
    End Select

End Function

'次の例では、データベース Northwind のすべての QueryDef オブジェクトの Type プロパティに対応している定数名を返して、Type プロパティを説明します。このプロシージャを実行するには、QueryDefType 関数が必要です。

Sub TypeX3()

    Dim dbsNorthwind As Database
    Dim qdfLoop As QueryDef

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")

    Debug.Print "QueryDefs in Northwind Database:"
    Debug.Print "    Type - Name"

    ' データベース Northwind の QueryDefs コレクションを列挙します。
    For Each qdfLoop In dbsNorthwind.QueryDefs
        Debug.Print "        " & _
            QueryDefType(qdfLoop.Type) & " - " & qdfLoop.Name
    Next qdfLoop

    dbsNorthwind.Close

End Sub

Function QueryDefType(intType As IntegerAs String

    Select Case intType
        Case dbQSelect
            QueryDefType = "dbQSelect"
        Case dbQAction
            QueryDefType = "dbQAction"
        Case dbQCrosstab
            QueryDefType = "dbQCrosstab"
        Case dbQDelete
            QueryDefType = "dbQDelete"
        Case dbQUpdate
            QueryDefType = "dbQUpdate"
        Case dbQAppend
            QueryDefType = "dbQAppend"
        Case dbQMakeTable
            QueryDefType = "dbQMakeTable"
        Case dbQDDL
            QueryDefType = "dbQDDL"
        Case dbQSQLPassThrough
            QueryDefType = "dbQSQLPassThrough"
        Case dbQSetOperation
            QueryDefType = "dbQSetOperation"
        Case dbQSPTBulk
            QueryDefType = "dbQSPTBulk"
    End Select

End Function

 

2000年01月01日|[VBサンプルコード]:[宣言]