表字段描述 - MS Access

时间:2012-05-17 20:33:26

标签: ms-access properties

我环顾四周,发现了一些关于如何从字段的“描述”框中获取描述的VBA代码,但不是我如何在表单属性中使用它。

我想要一个ControlTip显示,该字段的描述来自数据库中的描述,而不必重写所有描述;我希望能够添加到所有控制提示中的复制粘贴代码?

像(但显然不是)的东西

ControlTipText:  Me.ThisControl.ThisControlFieldDescription

有人知道代码,或者即使有代码吗?

编辑:

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

以上工作用于显示状态栏文本。但是我想用活动形式填充“frmTrials”,用当前活动控件填充“txtBox”;当控件变为活动状态时,我可以将StatusBarText放入“描述框”文本字段(或控件提示等)。我试过了

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

只是把错误扔给了我。

3 个答案:

答案 0 :(得分:4)

据我了解情况,您希望每次加载表单时动态设置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将设置为空字符串。

此方法将要求您为表单运行一次该过程,而不是每次加载表单时运行一些其他过程。如果您稍后更改了任何表单的记录源字段的Description属性,则可以重新运行SetControlTipText以更新ControlTipText

或者您可以为所有应用程序的表单运行该过程,作为准备发布新版本应用程序的一部分。

Dim frm As Object
For Each frm in CurrentProject.AllForms
    SetControlTipText frm.Name
Next frm

答案 1 :(得分: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

答案 2 :(得分: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