【问题标题】:Access. How to Create a record and select that record in CBO on another form?使用权。如何在另一个表单上创建记录并在 CBO 中选择该记录?
【发布时间】:2019-02-16 02:48:26
【问题描述】:

我有两个表单frmProductCreatefrmColourCreate

frmProductCreate 我有:

  • 组合框:colourID
  • 按钮:btnColCreate

这个想法是,如果用户需要创建一种新颜色,他们可以单击打开 frmColourCreate 的创建按钮,命名新颜色并单击保存按钮。这会将新颜色保存在颜色表中(这是frmProductCreate中cboColourID的记录源)。然后在frmProductCreate 中重新查询colourID 并关闭frmColourCreate

我还希望这个保存按钮在重新查询后选择 cbo colourID 并转到最后创建的颜色。即最后一条记录。我尝试了一些代码,但未能使其工作。任何帮助将不胜感激。

Private Sub btnSavecol_Click()
    Dim cancel As Integer

    If Me.ColName = "" Then
        MsgBox "You must enter a Colour Name."
        DoCmd.GoToControl "ColName"

        cancel = True
    Else
        If MsgBox("Are you sure you want to create new Colour?", vbYesNo) = vbNo Then
            cancel = True
        Else
            CurrentDb.Execute " INSERT INTO Colours (ColName) VALUES ('" & Me.ColName & "')"

            Me.ColName = ""

            DoCmd.Close

            If CurrentProject.AllForms("frmProductCreate").IsLoaded = False Then
                cancel = True
            Else
                Forms!frmproductCreate!ColourID.Requery
                'Forms!frmproductCreate!ColourID.SetFocus
                'Forms!frmproductCreate!ColourID.items.Count = -1
                'Forms!frmproductCreate!ColourID.Selected(Forms!frmproductCreate!ColourID.Count - 1) = False

                'YourListBox.SetFocus
                'YourListBox.ListIndex = YourListBox.ListCount - 1
                'YourListBox.Selected(YourListBox.ListCount - 1) = False
            End If

            If CurrentProject.AllForms("frmProductDetails").IsLoaded = False Then
                cancel = True
            Else
                Forms!frmproductDetails!ColourID.Requery
            End If
        End If
    End If
End Sub

【问题讨论】:

  • Requery之后添加以下代码Forms!frmproductDetails!ColourID = Me.ColName
  • 感谢您的快速回复。

标签: vba ms-access


【解决方案1】:

一些备注:

  • 变量cancel是什么?因为没用过,所以去掉了。
  • 我真的不知道你需要什么Me.ColName = ""
  • 为什么这么早关闭当前表单?我将DoCmd.Close 移到了末尾。
  • 我通过删除“箭头代码”(那些嵌套的 IFs)使您的代码更具可读性。

最后试试这个:

Private Sub btnSavecol_Click()
    If Me.ColName.Value = "" Then
        MsgBox "You must enter a Colour Name."
        DoCmd.GoToControl "ColName"
        Exit Sub
    End If

    If MsgBox("Are you sure you want to create new Colour?", vbYesNo) = vbNo Then Exit Sub

    CurrentDb.Execute "INSERT INTO Colours (ColName) VALUES ('" & Me.ColName.Value & "')"

    If Not CurrentProject.AllForms("frmProductCreate").IsLoaded Then GoTo Done

    Forms!frmproductCreate!ColourID.Requery

    'This sets the ComboBox 'ColourID' to the new colour:
    'Forms!frmproductCreate!ColourID.Value = Me.ColName.Value

    'If you use an automatic generated ID in the table 'Colours', then you will have to get that ID from the color and set it to the ComboBox:
    Forms!frmproductCreate!ColourID.Value = DLookup("ColID", "Colours", "ColName = '" & Me.ColName.Value & "'")

    Me.ColName.Value = ""

    If Not CurrentProject.AllForms("frmProductDetails").IsLoaded Then GoTo Done

    Forms!frmproductDetails!ColourID.Requery

Done:
    DoCmd.Close
End Sub

【讨论】:

  • 太棒了!感谢你能这么快回复。我会试试你的建议。非常感谢。
  • 我使用自动编号作为颜色 ID。我创建 frmColourCreate 表单的方式是 ColName 文本框未绑定。因此,当 SQL 语句将数据输入到 Color 表时,该表会为 ColID 生成一个 Autonumber。我相信它是我需要插入 Forms!frmproductCreate!ColourID.Value 的颜色表中的“最后一个”ColID 值。我这样想错了吗?
  • 组合框ColourIDRowSourceBound Column是什么?
  • ColourID CBO 未绑定。对于行源,我从构建器创建了一个查询。选择 ColID 和 ColourName。在格式上,我选择了 0cm、1cm 的 2 列,所以只显示名称。
  • 'Forms!frmproductCreate!ColourID.Value = DLookup("ID", "Colours", "ColName = '" & Me.ColName.Value & "'") 也许我可以为表 Colours 中 ID 的最后一个值?
猜你喜欢
  • 2013-12-21
  • 2021-08-05
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
相关资源
最近更新 更多