【问题标题】:Use VBA to add multiple fields to one record while also adding a category to each field使用 VBA 为一条记录添加多个字段,同时为每个字段添加一个类别
【发布时间】:2018-02-14 03:35:43
【问题描述】:

我在别处找不到这个问题的答案。我是 VBA 新用户。

我有一个非常简单的表格用于输入植物标本的数据。只有 3 个字段具有下拉框。 “物种”和“代码”两个字段引用另一个名为“MasterVegList”的表中的信息,以方便字段数据输入。

为了让最终用户可以将新物种添加到表单和“MasterVegList”表中,我编写了以下 VBA 代码:

Private Sub Code_NotInList(NewData As String, Response As Integer)
Dim strTmp As String

    'Get confirmation that this is not just a spelling error.
    strTmp = "Add '" & NewData & "' as a new category?"
    If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in list") = vbYes Then

        'Append the NewData as a record in the Categories table.
        strTmp = "INSERT INTO MasterVegList ( Code ) " & _
            "SELECT """ & NewData & """ AS Code;"
        DBEngine(0)(0).Execute strTmp, dbFailOnError

        'Notify Access about the new record, so it requeries the combo.
        Response = acDataErrAdded
    End If
End Sub

Private Sub Species_NotInList(NewData As String, Response As Integer)
Dim strTmp As String

    'Get confirmation that this is not just a spelling error.
    strTmp = "Add '" & NewData & "' as a new category?"
    If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in list") = vbYes Then

        'Append the NewData as a record in the Categories table.
        strTmp = "INSERT INTO MasterVegList ( Species ) " & _
            "SELECT """ & NewData & """ AS Species;"
        DBEngine(0)(0).Execute strTmp, dbFailOnError

        'Notify Access about the new record, so it requeries the combo.
        Response = acDataErrAdded
    End If
End Sub

问题在于,当输入一个新物种时,会为 MasterVegList 表中的物种和代码创建一个新记录。我需要将物种和代码输入到同一记录中。请注意,我指的是 MasterVegList 表,而不是打算使用表单填充的表。

【问题讨论】:

    标签: vba ms-access dropdown


    【解决方案1】:

    如果输入新物种,您可以提示用户输入代码,如果输入新代码,则提示物种:

    Private Sub Code_NotInList(NewData As String, Response As Integer)
    Dim strTmp As String
    
        'Get confirmation that this is not just a spelling error.
        strTmp = "Add '" & NewData & "' as a new category?"
        If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in list") = vbYes Then
            Dim strInput As String
            strInput = InputBox("What's the species?")
            'Append the NewData as a record in the Categories table.
            strTmp = "INSERT INTO MasterVegList ( Code, Species ) " & _
                "VALUES(""" & NewData  & """, """ & strInput  & """);"
            CurrentDb.Execute strTmp, dbFailOnError
    
            'Notify Access about the new record, so it requeries the combo.
            Response = acDataErrAdded
            Me.Species.Requery
        End If
    End Sub
    
    Private Sub Species_NotInList(NewData As String, Response As Integer)
    Dim strTmp As String
    
        'Get confirmation that this is not just a spelling error.
        strTmp = "Add '" & NewData & "' as a new category?"
        If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in list") = vbYes Then
            Dim strInput As String
            strInput = InputBox("What's the code?")
            'Append the NewData as a record in the Categories table.
            strTmp = "INSERT INTO MasterVegList ( Code, Species ) " & _
                "VALUES(""" & strInput  & """, """ & NewData  & """);"
            CurrentDb.Execute strTmp, dbFailOnError
    
            'Notify Access about the new record, so it requeries the combo.
            Response = acDataErrAdded
            Me.Code.Requery
        End If
    End Sub
    

    您可能想检查用户是否确实输入了有效的输入,但我将把它作为练习留给读者。

    我还更改了以下内容:使用INSERT INTO ... VALUES ... 代替INSERT INTO ... SELECT,使用CurrentDb 代替DbEngine(0)(0)

    此外,您可能希望参数化这些查询,以便人们能够在字段中输入引号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多