【问题标题】:VBA script in Access not workingAccess中的VBA脚本不起作用
【发布时间】:2014-07-07 23:14:42
【问题描述】:

我有一个简单的 MS Access 数据库(2010 版),只有 4 个字段:

数据库名称:测试

test1 = 文本

test2 = 是/否(勾选框)

test3 = 文本

test4 = 文本

然后我创建了一个名为 Form_Test 的公式并将代码分配给复选框:

Option Compare Database

Option Explicit

Private Declare Function apiGetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long


Function BenutzerName() As String

Dim lngLen As Long
Dim lngX As Long
Dim strBenutzerName As String

    strBenutzerName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strBenutzerName, lngLen)
    If lngX <> 0 Then
        BenutzerName = Left$(strBenutzerName, lngLen)
    Else
        BenutzerName = ""
    End If

End Function



Private Sub test2_Click()
If test2.Value = True Then
    test3 = BenutzerName()
Else
    test3 = ""

End If
End Sub

一旦用户单击任何行中的复选框,我想使用此代码将用户名添加到字段 test3。但是,脚本不起作用,我不明白为什么。有人可以帮忙吗?

【问题讨论】:

  • 尝试在代码中放置断点,看看错误发生在哪一行代码。这可能有助于找出问题的原因。
  • 当您说 “不起作用” 时,它也无助于其他任何人解决您的问题,除非错误非常明显。它没有做什么?
  • 如果您的问题是“我没有看到任何事情发生”,请从If test2.Value = True Then 行上的断点开始,以确保您的点击事件完全触发。然后检查 test2.Value 的值以验证它是否符合您的预期。

标签: ms-access vba


【解决方案1】:

确保您的代码在正确的位置,

在表单的设计模式中

Right click the Check box 并选择 Properties -> Event Tab->On Click->Code Builder 将打开代码编辑器

那是你的 on 代码必须驻留的地方

编辑:

另外,Follow this link在你的代码中设置Breakpoint(F9),看看你的代码是否被触发,链接中还包含调试信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多