5

フィールドの[説明]ボックスから説明を取得する方法についていくつかのVBAコードを調べましたが、フォームのプロパティでそれを使用する方法はわかりませんでした。

すべての説明を書き直すことなく、データベースの説明から取得したそのフィールドの説明とともにControlTipを表示したいと思います。すべてのコントロールチップに追加できるコードのコピーアンドペーストビットを望んでいますか?

のようなもの(しかし明らかにそうではない)

ControlTipText:  Me.ThisControl.ThisControlFieldDescription

誰かがコードを知っていますか、あるいはコードがあったとしても?

編集:

description = Forms("frmTrials").Controls("txtBox").StatusBarText
MsgBox description

上記はステータスバーのテキストを表示するために機能します。ただし、「frmTrials」にアクティブなフォームを入力し、「txtBox」に現在アクティブなコントロールを入力したいと思います。そうすれば、コントロールがアクティブになったときに、StatusBarTextを「説明ボックス」テキストフィールド(またはコントロールのヒントなど)に入れることができます。私は試した

description = Forms(Me).Controls(Me.ActiveControl).StatusBarText

それは私にエラーを投げました。

4

3 に答える 3

5

私は状況を理解しているControlTipTextので、フォームが読み込まれるたびにプロパティを動的に設定する必要があります。このアプリケーションはタブレットデバイスを対象としていることをコメントで示したので、フォームを開くときにプロセッサの負荷を制限することをお勧めします。ControlTipTextフォームのデザインでプロパティを保存することで、これを行うことができます。

次のようなフォームの名前で次の手順を試してください。

SetControlTipText "YourFormName"

手順は次のとおりです。限られたテストでは問題は見つかりませんでした。ControlTipTextチェックボックス、コンボ、リストボックス、テキストボックスを設定します。最初のCase行を変更して、別のコントロールのセットをターゲットにします。

Public Sub SetControlTipText(ByVal pFormName As String)
    Dim ctl As Control
    Dim db As DAO.Database
    Dim frm As Form
    Dim rs As DAO.Recordset

    DoCmd.OpenForm pFormName, acDesign
    Set frm = Forms(pFormName)
    If Len(frm.RecordSource) > 0 Then
        Set db = CurrentDb
        Set rs = db.OpenRecordset(frm.RecordSource)
        For Each ctl In frm.Controls
            Select Case ctl.ControlType
            Case acCheckBox, acComboBox, acListBox, acTextBox
                If Len(ctl.ControlSource) > 0 _
                        And Not ctl.ControlSource Like "=*" Then
                    ctl.ControlTipText = _
                        GetDescription(rs.Fields(ctl.ControlSource))
                End If
            Case Else
                ' pass '
            End Select
        Next ctl
        rs.Close
    End If
    Set ctl = Nothing
    Set rs = Nothing
    Set db = Nothing
    Set frm = Nothing
    DoCmd.Close acForm, pFormName, acSaveYes
End Sub

SetControlTipTextこの関数を呼び出します:

Public Function GetDescription(ByRef pObject As Object) As String
    Dim strReturn As String

On Error GoTo ErrorHandler

    strReturn = pObject.Properties("Description")

ExitHere:
    GetDescription = strReturn
    On Error GoTo 0
    Exit Function

ErrorHandler:
    strReturn = vbNullString ' make it explicit '
    GoTo ExitHere
End Function

このSetControlTipText手順では、バインドされていないフォームは無視されます。Descriptionバインドされたフィールドの制御ソースにプロパティが割り当てられていない場合、そのプロパティControlTipTextは空の文字列に設定されます。

このアプローチでは、フォームが読み込まれるたびに他のプロシージャを実行するのではなく、フォームに対してプロシージャを1回実行する必要があります。後でDescriptionフォームのレコードソースフィールドのいずれかのプロパティを変更した場合は、再実行SetControlTipTextしてを更新できますControlTipText

または、アプリケーションの新しいバージョンをリリースするための準備の一環として、すべてのアプリケーションのフォームに対してプロシージャを実行することもできます。

Dim frm As Object
For Each frm in CurrentProject.AllForms
    SetControlTipText frm.Name
Next frm
于 2012-05-18T16:19:04.487 に答える
2

これのバリエーションを試して、フォーム上のすべてのコントロールを調べ、バインドされたデータソースに一致するフィールドにツールチップを設定できます。

Private Sub Form_Load()
    ' Load tooltips for the current form '
    ' Place this in all subforms as well '
    SetToolTips Me
    ' If the form is bound at runtime, you can call use instead '
    SetToolTips Me, myDataRecordSet
End Sub

Private Sub SetToolTips(frm As Form, Optional rs As dao.Recordset)
    Dim ctls As Controls
    Dim ctl As Control
    Dim sourceField As String
    Dim description As String

    On Error Resume Next

    Set ctls = frm.Controls
    If rs Is Nothing Then Set rs = frm.Recordset

    For Each ctl In ctls
        sourceField = ctl.ControlSource
        If Len(sourceField) > 0 Then
            description = rs.Fields(sourceField).Properties("Description")
            If Len(description) > 0 Then
                ctl.ControlTipText = description
            End If
        End If
    Next ctl
    Set ctls = Nothing
End Sub
于 2012-05-18T06:01:32.770 に答える
1

コントロールチップの代わりにテキストフィールドを使用して説明を表示することになりました。また、有益な写真を表示します(指定されたフォルダーにそのように名前が付けられている写真がある場合)。PNG形式ではない画像については、追加できると確信していますが、処理を行っていないことに注意してください。

Public Function pushInfo(frm As Form)
'On Error Resume Next
Dim desc As String 'description from the status bar of the active control
Dim path As String 'path to image
Dim dbPath As String 'path to the database
Dim hyperPath As String 'path to hyperlink

'Take the statusbar text and push it into the description box caption.
desc = Forms(frm.Name).Controls(frm.ActiveControl.Name).StatusBarText 'Put statusbar text into var "desc"
frm.txtInfo.Caption = vbNewLine & vbNewLine & desc 'Put the text (with linefeeds) into the box
frm.lblInfo.Caption = frm.ActiveControl.Name & " Description:" 'Put the database name of the field into the label

'Set the image in the imgbox
dbPath = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "\"))   'path to the DB.
path = dbPath & "img\" 'add the img directory
path = path & frm.Name & "\" 'add the form name
path = path & frm.ActiveControl.Name 'add the control's name
path = path & ".png" 'add the jpg suffix
hyperPath = path

If (Len(Dir(path)) = 0) Then 'if the image doesn't exist (this field has no image..)
    path = dbPath & "img\GenericLogo.png" 'set to the logo
    hyperPath = ""
End If

Forms(frm.Name).Controls("imgInfo").Picture = path 'set the picture to the defined path
Forms(frm.Name).Controls("imgInfo").HyperlinkAddress = hyperPath 'set the picture to link to the file
End Function
于 2012-05-30T19:04:19.660 に答える