You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

新增客户时出现Run-time error '3075'错误的排查求助

问题分析与解决方案

错误原因

这个运行时错误的核心是新增客户时,当前表单的CustomerID为空值。当你用"CustomerID=" & Me.CustomerID拼接查询条件时,空值会让条件变成CustomerID=,缺少了匹配的目标值,直接触发语法错误。

解决方案

核心思路

  1. 先判断CustomerID是否为空:新增客户时ID未赋值,关联表不可能有对应数据,直接将所有按钮设为非粗体。
  2. 根据CustomerID的数据类型(文本/数值)正确拼接查询条件,避免语法错误。

重构后的代码

首先写一个通用子过程,减少重复代码:

Private Sub SetButtonBold(btn As Control, tableName As String)
    Dim criteria As String
    
    ' 若CustomerID为空,直接设为非粗体
    If IsNull(Me.CustomerID) Then
        btn.FontBold = False
        Exit Sub
    End If
    
    ' 文本型CustomerID需加单引号,数值型则去掉单引号
    criteria = "CustomerID='" & Me.CustomerID & "'"
    ' 数值型ID用此行替换:criteria = "CustomerID=" & Me.CustomerID
    
    ' DCount返回大于0则设为粗体,否则非粗体
    btn.FontBold = (DCount("*", tableName, criteria) > 0)
End Sub

然后在Form_Current事件中调用:

Private Sub Form_Current()
    ' 先处理CustomerID为空的情况
    If IsNull(Me.CustomerID) Then
        Me.[CMDCounty Address Form Open Button].FontBold = False
        Me.[CMD BOA History Button].FontBold = False
        Me.[CMD Sanitary History Button].FontBold = False
        Me.[CountyOrdersOpen].FontBold = False
        Me.[CMD Land Use History Button].FontBold = False
        Me.[PLATHISTORYBUTTON].FontBold = False
        Me.[CMD REZONE HISTORY BUTTON].FontBold = False
        Me.[CMD Land Division History Button].FontBold = False
        Exit Sub
    End If
    
    ' 逐个设置按钮状态
    SetButtonBold Me.[CMDCounty Address Form Open Button], "County Address Table"
    SetButtonBold Me.[CMD BOA History Button], "BOA Table"
    SetButtonBold Me.[CMD Sanitary History Button], "Sanitary Permit table"
    SetButtonBold Me.[CountyOrdersOpen], "WI Fund Table"
    SetButtonBold Me.[CMD Land Use History Button], "Land Use Permit Table"
    SetButtonBold Me.[PLATHISTORYBUTTON], "Plat Approval Table"
    SetButtonBold Me.[CMD REZONE HISTORY BUTTON], "Rezoning Table"
    SetButtonBold Me.[CMD Land Division History Button], "Land Division Table"
End Sub

关键说明

  • 你之前修改按钮名称空格的操作无效,因为问题根源不在按钮命名,而在于空值导致的查询条件拼接错误。
  • 把双引号改单引号的思路是正确的,但仅适用于文本型CustomerID,数值型ID不需要加引号;同时必须先处理空值,否则空值时依然会报错。

内容的提问来源于stack exchange,提问作者dulster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 09:45:26