本文向您介绍两种方法来将新记录添加到窗体上组合框中使用不在列表中事件。
Microsoft 提供的编程示例只,用于说明不附带任何明示或暗示保证。这包括,但不限于对适销性或针对特定用途的适用性的暗示的担保。本文假定您熟悉演示了正在使用的编程语言以及用于创建和调试过程的工具。Microsoft 支持工程师可以帮助解释某个特定过程的功能,但他们不会修改这些示例以提供额外的功能或构建过程来满足您的具体要求。
下面的示例演示两种方法的使用不在列表中事件将记录添加到组合框使用在罗斯文示例数据库中的订单窗体。不在列表中事件被触发时您键入新的公司在客户 id (标记为帐单寄往:) 字段订单窗体上。
第一种方法使用 Visual Basic 应用程序代码以编程方式将一条新记录添加到客户表。第二种方法打开客户窗体,并允许您自己添加一条新记录。
注意: 如果您按照这些步骤在此示例中,您修改示例数据库 Northwind.mdb。您可能希望备份 Northwind.mdb 文件并在该数据库的副本,请按照下列步骤。
方法 1: 使用代码添加到表的记录
注: 在这篇文章中的示例代码使用 Microsoft 数据访问对象。对于正常运行的此代码,必须引用在 Microsoft DAO 3.6 对象库中。若要执行此操作,在 Visual Basic 编辑器中
工具 菜单上单击
引用,并确保已选中
Microsoft DAO 3.6 对象库 的复选框。
- 打开示例数据库 Northwind.mdb。
- 在设计视图中打开订单窗体。
- 请注意客户 id 组合框的 限于列表 属性设置为 是。
- 将 不在列表 中的客户 id 组合框属性设置为下列事件过程:
Private Sub CustomerID_NotInList(NewData As String, _
Response As Integer)
Dim Db As DAO.Database
Dim Rs As DAO.Recordset
Dim Msg As String
Dim NewID As String
On Error GoTo Err_CustomerID_NotInList
' Exit this subroutine if the combo box was cleared.
If NewData = "" Then Exit Sub
' Confirm that the user wants to add the new customer.
Msg = "'" & NewData & "' is not in the list." & vbCr & vbCr
Msg = Msg & "Do you want to add it?"
If MsgBox(Msg, vbQuestion + vbYesNo) = vbNo Then
' If the user chose not to add a customer, set the Response
' argument to suppress an error message and undo changes.
Response = acDataErrContinue
' Display a customized message.
MsgBox "Please try again."
Else
' If the user chose to add a new customer, open a recordset
' using the Customers table.
Set Db = CurrentDb
Set Rs = Db.OpenRecordset("Customers", dbOpenDynaset)
' Ask the user to input a new Customer ID.
Msg = "Please enter a unique 5-character" & vbCr & "Customer ID."
NewID = InputBox(Msg)
Rs.FindFirst BuildCriteria("CustomerID", dbText, NewID)
' If the NewID already exists, ask for another new unique
' CustomerID
Do Until Rs.NoMatch
NewID = InputBox("Customer ID " & NewID & " already exists." & _
vbCr & vbCr & Msg, NewID & " Already Exists")
Rs.FindFirst BuildCriteria("CustomerID", dbText, NewID)
Loop
' Create a new record.
Rs.AddNew
' Assign the NewID to the CustomerID field.
Rs![CustomerID] = NewID
' Assign the NewData argument to the CompanyName field.
Rs![CompanyName] = NewData
' Save the record.
Rs.Update
' Set Response argument to indicate that new data is being added.
Response = acDataErrAdded
End If
Exit_CustomerID_NotInList:
Exit Sub
Err_CustomerID_NotInList:
' An unexpected error occurred, display the normal error message.
MsgBox Err.Description
' Set the Response argument to suppress an error message and undo
' changes.
Response = acDataErrContinue
End Sub
- 在窗体视图中打开订单窗体。
- 添加新的订单,方法是: 在付款人字段中键入 ABC Wholesalers 和输入 ABCWH 时系统会提示您输入客户的 id。OnNotInList 事件过程中的代码运行,并将新客户添加到客户表。
方法 2: 使用窗体中添加新记录
- 打开示例数据库 Northwind.mdb。
- 在设计视图中打开订单窗体。
- 请注意客户 id 组合框的 限于列表 属性设置为 是。
- 将 不在列表 中的客户 id 组合框属性设置为下列事件过程:
Private Sub CustomerID_NotInList (NewData As String, Response As _
Integer)
Dim Result
Dim Msg As String
Dim CR As String
CR = Chr$(13)
' Exit this subroutine if the combo box was cleared.
If NewData = "" Then Exit Sub
' Ask the user if he or she wishes to add the new customer.
Msg = "'" & NewData & "' is not in the list." & CR & CR
Msg = Msg & "Do you want to add it?"
If MsgBox(Msg, vbQuestion + vbYesNo) = vbYes Then
' If the user chose Yes, start the Customers form in data entry
' mode as a dialog form, passing the new company name in
' NewData to the OpenForm method's OpenArgs argument. The
' OpenArgs argument is used in Customer form's Form_Load event
' procedure.
DoCmd.OpenForm "Customers", , , , acAdd, acDialog, NewData
End If
' Look for the customer the user created in the Customers form.
Result = DLookup("[CompanyName]", "Customers", _
"[CompanyName]='" & NewData & "'")
If IsNull(Result) Then
' If the customer was not created, set the Response argument
' to suppress an error message and undo changes.
Response = acDataErrContinue
' Display a customized message.
MsgBox "Please try again!"
Else
' If the customer was created, set the Response argument to
' indicate that new data is being added.
Response = acDataErrAdded
End If
End Sub
- 保存并关闭订单窗体。
- 在设计视图中打开客户窗体。
- 将表单的 OnLoad 属性设置为下列事件过程:
Private Sub Form_Load ()
If Not IsNull(Me.OpenArgs) Then
' If form's OpenArgs property has a value, assign the contents
' of OpenArgs to the CompanyName field. OpenArgs will contain
' a company name if this form is opened using the OpenForm
' method with an OpenArgs argument, as done in the Orders
' form's CustomerID_NotInList event procedure.
Me![CompanyName] = Me.OpenArgs
End If
End Sub
- 保存并关闭客户窗体,然后在窗体视图中打开订单窗体。
- 通过在付款人字段中键入 ABC 分销商 添加新的订单。将打开客户窗体时在客户 ID 字段中输入 ABCDI 和键入任何您喜欢的剩余的客户信息的内容。
有关 NotInList 事件的在 Visual Basic 编辑器中的详细信息单击
帮助 菜单上的
Microsoft Visual Basic 帮助、 在 Office 助手或应答向导中,键入
NotInListevent,然后单击
搜索 以查看相关主题。
有关限于列表属性的详细信息,单击
帮助 菜单上的
Microsoft Access 帮助、 在 Office 助手或应答向导中,键入
限于列表属性,然后单击
搜索 以查看相关主题。
文章编号: 197526 - 最后修改: 2006年10月11日 - 修订: 3.2
这篇文章中的信息适用于:
- Microsoft Access 2000 标准版
- Microsoft Access 2002 标准版
| kbmt kbdta kbhowto kbofficeprog kbprogramming kbvba KB197526 KbMtzh |
机器翻译注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。
点击这里察看该文章的英文版:
197526
(http://support.microsoft.com/kb/197526/en-us/
)
Microsoft和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性,不作任何声明。 所有该等文件及有关图形均"依样"提供,而不带任何性质的保证。Microsoft和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于适销性、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,Microsoft和/或其各供应商就因丧失使用、数据或利润所导致的任何特别的、间接的、衍生性的损害或任何因使用而丧失所导致的之损害、数据或利润不负任何责任。