访问。如何创建记录并在CBO中以另一种形式选择该记录?

时间:2019-02-15 12:36:30

标签: vba ms-access

我有两种形式var val = [14, 20 , 40 , 40,9]; var time = ["6:28 PM","7:28 PM","8:28 PM","9:28 PM"]; var resultArr = []; for(var i=0;i<val.length;i++){ resultArr.push({x:val[i],y: ((time[i] !== undefined)?time[i]:'')}); } console.log(resultArr);frmProductCreate

frmColourCreate中,我有:

  • 组合框:frmProductCreate
  • 按钮:colourID

该想法是,如果用户需要创建新颜色,则可以单击创建按钮,打开btnColCreate,命名新颜色,然后单击保存按钮。它将新颜色保存在颜色表中(这是frmColourCreate中cbo ColourID的记录源)。然后在frmProductCreate中重新查询colourID,然后关闭frmProductCreate

我还要此保存按钮执行的操作是在重新查询后选择cbo frmColourCreate并转到最后创建的颜色。即最后一条记录。我尝试了一些代码,但未能使其正常工作。任何帮助将不胜感激。

colourID

1 个答案:

答案 0 :(得分:0)

一些评论:

  • 变量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