如果控件不可见,请将value设置为null(访问VBA)

时间:2017-08-10 17:00:01

标签: vba ms-access access-vba

我有一个包含许多控件的表单。但是,如果用户将选项按钮标记为“未到期”,则许多这些字段将设置为不可见。最终我不希望这些隐藏的字段存储任何值,但我不希望值保存,直到表单被保存(这样,如果有人意外点击“未到期”,它不会删除所有信息)。这是我的代码 - 设置为在表单更新之前在所有控件上运行。

告诉我“对象不支持此属性或方法”,并在调试时突出显示ctl.Value = null。知道我能做些什么来使这项工作吗?

Dim ctl As Control
    If Me.optPayDue = 2 Then
        For Each ctl In Me.Controls
            If ctl.Visible = False Then ctl.Value = Null
        Next ctl
    End If

2 个答案:

答案 0 :(得分:2)

您收到此错误的原因是因为MERGE INTO Location_CVT AS C USING ( SELECT deviceID, lastSeen, locationMapHierarchy, locationCoordinateX, locationCoordinateY, locationCoordinateUnit, geoCoordinateLat, geoCoordinateLong, geoCoordinateUnit FROM OPENJSON(@Location_NotificationJson) WITH ( deviceId nchar(17), lastSeen varchar(128), locationMapHierarchy nvarchar(256), locationCoordinateX float, locationCoordinateY float, locationCoordinateUnit nvarchar(64), geoCoordinateLat float, geoCoordinateLong float, geoCoordinateUnit nvarchar(64) ) AS InputJSON ) AS InputJSON -- ALIAS ADDED HERE ON (C.deviceId = InputJSON.deviceId) WHEN MATCHED THEN UPDATE SET C.deviceId = InputJSON.deviceId, C.lastSeen = InputJSON.lastSeen, C.locationMapHierarchy = InputJSON.locationMapHierarchy, C.locationCoordinateX = InputJSON.locationCoordinateX, C.locationCoordinateY = InputJSON.locationCoordinateY, C.locationCoordinateUnit = InputJSON.locationCoordinateUnit, C.geoCoordinateLat = InputJSON.geoCoordinateLat, C.geoCoordinateLong = InputJSON.geoCoordinateLong, C.geoCoordinateUnit = InputJSON.geoCoordinateUnit 集合中包含的内容类似于没有Controls属性的Subforms,Labels,EmptyCells等。

您希望在采取行动之前检查您为其设置值的事物是否具有Value属性。

Value

要使用Dim ValueControls As New Dictionary ValueControls.Add "TextBox", "TextBox" ValueControls.Add "CheckBox", "CheckBox" ValueControls.Add "ComboBox", "ComboBox" ValueControls.Add "ListBox", "ListBox" ValueControls.Add "OptionButton", "OptionButton" Dim ctl As Control If Me.optPayDue = 2 Then For Each ctl In Me.Controls If Not ctl.Visible and ValueControls.Exists(TypeName(ctl)) Then ctl.Value = Null End If Next ctl End If 类,您需要添加对Dictionary的引用(C:\ Windows \ SysWOW64 \ scrrun.dll)

enter image description here

答案 1 :(得分:1)

另一种方法是在for循环中清除任何没有.Value的控件。假设它只是文本框,您可以执行以下操作

    Dim ctl As Control
    If Me.optPayDue = 2 Then
        For Each ctl In Me.Controls
            If ctl.ControlType = acTextBox and ctl.Visible = False Then
            ctl.Value = Null
        Next ctl
    End If

对于像复选框这样的东西,控件类型将是acCheckBox,值将设置为0.