为什么我的MS Access控件不接受我的“不在列表中"?事件代码 [英] Why is is my MS Access control not accepting my "on not in list" event code

查看:153
本文介绍了为什么我的MS Access控件不接受我的“不在列表中"?事件代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Access不在列表中"事件已有很长时间了.它允许您将组合框中的项目限制为一个特定的列表,但是如果用户想输入尚不存在的项目,则可以允许用户将项目即时添加到组合的记录源中.您将组合框的限制到列表"属性设置为是,但是然后在不在列表中"事件后面放置一些代码.间歇性地,我在Access 2016中遇到一种情况,该情况似乎不起作用.我得到标准的该项目不在列表中".尝试输入新项目时出现错误,而没有看到并调用我的代码隐藏逻辑.怎么了?

I've been using the Access "On Not In List" event for a long time. It allows you to limit the items in your combo box to a particular list, but allows the user to add an item to combo's record source on the fly if they want to enter something that isn't already there. You set the "Limit To List" property of the combo box to Yes, but then you put some code behind the "On Not In List" event. Intermittently, I get a situation in Access 2016 where this doesn't seem to work. I get the standard "The item is not in the list." error when trying to enter a new item, without my code-behind logic being seen and called. What's up?

推荐答案

我的头撞墙了很长时间之后,我相信这是Access 2016中的错误,我认为我偶然发现了一个修复程序.我将窗体的RecordSetType属性设置为Snapshot,关闭并保存了窗体,在设计视图中重新打开了该窗体,将RecordSetType属性设置回Dynaset.这似乎已经解决了问题.我不知道为什么.

After banging my head against the wall for a long time, I believe this is bug in Access 2016 and I think I stumbled on a fix. I set the form's RecordSetType property to Snapshot, closed and saved form, reopened the form in design view, set the RecordSetType property back to Dynaset. This seems to have cleared up the problem. I have no idea why.

但是因为我在这里. . .一些其他细节:在每个控件的代码后面,我使用如下代码:

But since I'm here. . . some additional details: In the code-behind for each control I use code like this:

Private Sub VendorID_NotInList(NewData As String, Response As Integer)
    Response = RU_NotInList("Lookup Vendor", "Description", NewData, gc_strMsgCap)
End Sub

当您单击代码生成器"选项时,将在不在列表中"事件后自动创建此类子例程.我有一个很久以前写的实用程序函数. ("RU"是指代码库.)

This type of subroutine gets created automatically behind the "On Not In List" event, when you click on the 'code builder' option. I have mine call a utility function that I wrote a long time ago. ("RU" refers a code library.)

该函数返回一个固有的Access整数常量,该常量将直接传递回Access进行处理.

The function returns an intrinsic Access integer constant that gets passed straight back to Access to handle.

该例程的内部看起来像这样:

The inside of that routine looks like this:

Function RU_NotInList(TableName As String, FieldName As String, newdata As String, Optional pstrTile As String) As Integer
    Dim rs As DAO.Recordset, db As DAO.Database, n1 As Integer

    RU_NotInList = DATA_ERRCONTINUE
    On Error GoTo RU_NotInList_Error
    If Len(Trim(newdata)) = 0 Then Exit Function

    n1 = MsgBox(newdata & " is not in the list.  Do you wish to add it?", MB_ICONQUESTION + MB_YESNO, pstrTile)
    If n1 = IDNO Then Exit Function

    Dim strSQL As String
    strSQL = "INSERT INTO [" & TableName & "] ([" & FieldName & "]) VALUES (""" & newdata & """)"
    WarningsHour True 'Turns hourglass cursor on, warning messages off.
    DoCmd.RunSQL strSQL
    WarningsHour False 'Undoes the above.

    RU_NotInList = DATA_ERRADDED
    Exit Function

RU_NotInList_Error:
    RUError "RU_NotInList", Err 'generic error-handling routine in the RU library
    Exit Function

End Function

上面的代码中的所有大写字母均为Access内部常量.

All the all-caps items in the code above are Access intrinsic constants.

这篇关于为什么我的MS Access控件不接受我的“不在列表中"?事件代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆