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

MS Access表单中选择VendorName后联动更新QuotationCurrency组合框的问题

问题分析与修正方案

原代码的核心错误

  1. DLookup条件字符串拼接错误:你把控件引用Me!VendorName放在了单引号内部,导致Access把它当成普通字符串'Me!VendorName '去查询,而非读取组合框的实际选中值。
  2. 多余的Requery操作:Me.QuotationCurrency.Requery会重新加载组合框的行来源,反而会清空刚赋值的结果,完全没必要保留。

修正后的VBA代码

Private Sub VendorName_AfterUpdate()
    ' 正确拼接查询条件,读取VendorName控件的实际选中值
    Me.QuotationCurrency = DLookup("VendorCurrency", "Vendor", "VendorName = '" & Me!VendorName & "'")
End Sub

额外注意事项

  • 如果VendorName字段可能包含单引号(比如供应商名称是O'Neil),直接拼接会导致语法错误,需要转义单引号:
    Dim vendorName As String
    vendorName = Replace(Me!VendorName, "'", "''")
    Me.QuotationCurrency = DLookup("VendorCurrency", "Vendor", "VendorName = '" & vendorName & "'")
    
  • 确认QuotationCurrency组合框的绑定列与VendorCurrency字段的类型/值匹配:
    • 若VendorCurrency存储的是货币ID(对应Currency表的主键),需确保QuotationCurrency的绑定列设置为ID列;
    • 若存储的是货币名称,需确保QuotationCurrency的行来源包含该名称列,且绑定列设置正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 13:15:05