MS Access VBA启用/禁用文本框

时间:2017-03-09 04:47:10

标签: vba ms-access

我在访问时有以下VBA代码来启用/禁用文本框。

执行代码时,吨文本框保持禁用状态。

我错过了额外的财产吗?

Private Sub EnableTonnes()

  Dim sCode As String
  sCode ="xx"


  ' set default values for tonnes enabled and locked properties
  Tonnes.enabled = False
  Tonnes.Locked = True

 If sCode = "xx" Then
    ' enable tonnes field
    Tonnes.enabled = True
    Tonnes.Locked = False
  End If

End Sub

1 个答案:

答案 0 :(得分:0)

您的代码看起来不错。 TextBox属性在表单设计模式下设置,只能在表单设计模式下永久更改。您可以使用复杂的编码在设计模式下打开表单,更改属性然后保存表单...或手动执行。您始终可以使用代码在运行时控制这些属性。

Option Compare Database
Option Explicit

Private Sub cmdGo_Click()

    Dim sCode As String
    sCode = "xx"

    ' set default values for tonnes enabled and locked properties
    txtTonnes.Enabled = False
    txtTonnes.Locked = True

    If sCode = "xx" Then
        ' enable tonnes field
        txtTonnes.Enabled = True
        txtTonnes.Locked = False
    End If

    MsgBox txtTonnes.Name & " Enabled status is " & txtTonnes.Enabled
    MsgBox txtTonnes.Name & " Locked status is " & txtTonnes.Locked

End Sub

Private Sub Form_Load()
    MsgBox txtTonnes.Name & " Enabled status is " & txtTonnes.Enabled
    MsgBox txtTonnes.Name & " Locked status is " & txtTonnes.Locked
End Sub