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则将其设置为空字符串。

这种方法将要求您为表单运行一次该过程,而不是在每次加载表单时运行一些其他过程。如果您稍后更改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

Ended up using a text field to show the descriptions instead of the control tip. I also have an informative photo appear (if there is one so named in the given folder). I should note that I do not have any handling in place for images which are not PNG format, though I'm sure that could be added.

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 回答