Stu*_*ork 5 ms-access properties
我环顾四周,发现了一些关于如何从字段的“描述”框中获取描述的 VBA 代码,但没有找到如何在表单的属性中使用它。
我希望有一个 ControlTip 出现,该字段的描述来自数据库中的描述,而不必重写所有描述;我希望复制粘贴的代码可以添加到所有控制提示中吗?
像(但显然不是)
ControlTipText: Me.ThisControl.ThisControlFieldDescription
Run Code Online (Sandbox Code Playgroud)
任何人都知道代码,或者即使有一个?
编辑:
description = Forms("frmTrials").Controls("txtBox").StatusBarText
MsgBox description
Run Code Online (Sandbox Code Playgroud)
以上作品用于显示状态栏文本。但是我想用活动表单填充“frmTrials”,用当前活动的控件填充“txtBox”;这样,当控件变为活动状态时,我可以将 StatusBarText 放入“描述框”文本字段(或控件提示等)。我试过
description = Forms(Me).Controls(Me.ActiveControl).StatusBarText
Run Code Online (Sandbox Code Playgroud)
这只是向我抛出错误。
据我了解,您希望在ControlTipText每次加载表单时动态设置属性。由于您在评论中指出此应用程序适用于平板电脑设备,因此您可能希望在打开表单时限制处理器负载。您可以通过保存ControlTipText表单设计的属性来实现。
使用您的表单名称尝试以下过程,如下所示:
SetControlTipText "YourFormName"
Run Code Online (Sandbox Code Playgroud)
这是程序。我在有限的测试中没有发现任何问题。它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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
该SetControlTipText过程忽略未绑定的表单。如果绑定字段的控件源没有Description分配属性,ControlTipText则将其设置为空字符串。
这种方法将要求您为表单运行一次该过程,而不是每次加载表单时都运行其他一些过程。如果您稍后更改Description了表单的任何记录源字段的属性,您可以重新运行SetControlTipText以更新ControlTipText的。
或者,您可以为所有应用程序的表单运行该过程,作为发布应用程序新版本的准备工作的一部分。
Dim frm As Object
For Each frm in CurrentProject.AllForms
SetControlTipText frm.Name
Next frm
Run Code Online (Sandbox Code Playgroud)
最终使用文本字段来显示描述而不是控制提示。我还出现了一张信息丰富的照片(如果给定文件夹中有一张如此命名的照片)。我应该注意的是,我没有对非 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5786 次 |
| 最近记录: |