Microsoft Access - 将新记录保存到链接数据库

时间:2016-02-18 16:30:39

标签: ms-access combobox

我正在基于Access 2013中的一个内部和两个链接(外部)Access数据表创建用户界面。

我的UI上的两个字段是组合框,它们从链接的数据表中读取并显示选项。这样就可以一致地调出供应商和材料类型的条目,避免拼写错误。但是,我想添加以下功能:

- 如果在组合框中输入新值,将提示用户填写有关新值的必要信息。随后,此信息将保存到相应的链接数据表中。

如何从组合框中自行设置提示?它需要Access来打开一个表单或子表单,然后将其保存到链接的数据表中。

我更喜欢它是自动的,而不是最终用户提示,因此不会跳过它。我玩VB的时候已经好几年了,所以如果可能的话我想避免使用Access'内置函数(即使它需要更多时间)。提前谢谢!

1 个答案:

答案 0 :(得分:1)

好吧,所以我能够在研究" OnNotInList"之后做到这一点。功能和一点VB代码。

在'事件'的OnNotInList部分属性表,我选择了Code Builder'并输入以下内容:

Private Sub Supplier_NotInList(NewData As String, Response As Integer)
Dim ctl As Control
Dim dbsCustomerDatabase As Database

On Error GoTo Supplier_NotInList_Err
Dim intAnswer As Integer
Dim strSQL As String
intAnswer = MsgBox("The supplier " & Chr(34) & NewData & _
    Chr(34) & " is not currently listed." & vbCrLf & _
    "Would you like to add it to the list now?" _
    , vbQuestion + vbYesNo, "Spire Manufacturing Solutions")
' Adding the new entry to the list:
If intAnswer = vbYes Then
    strSQL = "INSERT INTO CustomerList([CustomerName]) " & _
             "VALUES ('" & NewData & "');"
    DoCmd.SetWarnings False
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True
    MsgBox "The new supplier has been added to the list." _
        , vbInformation, "Spire Manufacturing Solutions"
    Response = acDataErrAdded
' Opening the Supplier datasheet to add details 
' to the new entry:
    MsgBox "Opening Supplier database for new entry..."
    DoCmd.OpenTable "CustomerList", acViewNormal, acEdit

End If
Supplier_NotInList_Exit:
Exit Sub
Supplier_NotInList_Err:
MsgBox Err.Description, vbCritical, "Error"
Resume Supplier_NotInList_Exit
End Sub

这允许我自动提示用户在输入新供应商名称时添加新供应商的详细信息。或者,如果他们只是拼写错误,请取消该条目。我完全忘记了VB的多​​才多艺。谢谢大家的帮助,让我朝着正确的方向前进!

相关问题