(Access)フィールドに「標題」を設定する

 


Option Compare Database
Option Explicit
'指定されたテーブルの、指定されたフィールドに、指定された「標題」を設定する
サンプル
Function AddCaptionOfField(strTname As String, strFname As String,
strCaption As String) As Boolean
    Dim db As DAO.database
    Dim td As DAO.TableDef, fd As DAO.Field
    Dim newpr As DAO.Property
    
    AddCaptionOfField = False
    Set db = CurrentDb()
    
    For Each td In db.TableDefs 'Tabledefをまわす
        If td.Name = strTname Then
            For Each fd In td.Fields 'Fieldをまわす
                If fd.Name = strFname Then
                    'Caption プロパティが存在するかチェックし、無ければ作成する
                    If ChkFldPropertyExists(fd, "Caption") = False Then
                        Debug.Print "Create New Property (Caption)"
                        Set newpr = fd.CreateProperty("Caption", dbText,strCaption)
                        fd.Properties.Append newpr
                    Else
                        Debug.Print "Overwrite Property (Caption)"
                        fd.Properties("Caption") = strCaption
                    End If
                    AddCaptionOfField = True
                End If
            Next fd
        End If
    Next td
    
    db.Close
    Set db = Nothing
    
End Function
'指定されたFieldに、指定されたプロパティが存在するかチェックする
Function ChkFldPropertyExists(fd As DAO.Field, strPname As String) As
Boolean
    Dim pr As DAO.Property
    Dim FlgExists As Boolean
    
    FlgExists = False
    
    For Each pr In fd.Properties
        If pr.Name = strPname Then
            FlgExists = True
        End If
    Next pr
    
    Set pr = Nothing
    ChkFldPropertyExists = FlgExists
End Function




トップページに戻る

Tips一覧に戻る