【问题标题】:Combo box in form (Access 2010) - Retrieving values from 2 different fields表单中的组合框 (Access 2010) - 从 2 个不同的字段中检索值
【发布时间】:2014-07-30 19:13:21
【问题描述】:

我需要在 access 2010 上创建一个表单,用于在表中的记录之间导航 (t_main)

但问题是我需要在 param1_old 和 new 以及 param2_old 和 new 之间进行选择。

此组合将有 2 个值,每个参数的旧值和新值。最后,在我选择了要为该用户保留的参数后,我将单击一个按钮并将此信息保存到一个新表中(t_saved)。

t_main 具有以下结构:

user; date; name; param1_old; param1_new; param2_old; param2_new

t_saved 具有以下结构:

user; date; name; param1; param2

知道如何做到这一点吗?我应该使用记录集吗?有没有办法避免它,只是强制组合将来自 2 个不同字段的值放入一个值列表中?

非常感谢您的帮助!

编辑: 我知道理解我需要什么是相当复杂的,我将尝试在屏幕截图中显示:

表中数据如下:

user; date; name; param1_old; param1_new; param2_old; param2_new 1234568789;"21/07/2014";"John Smith";'Lemon street 125';'Avocado avenue 123'; '...'; '..'

【问题讨论】:

  • 你的描述对我来说意义不大。您能否使用一些示例数据以及您希望看到的结果来编辑您的帖子。

标签: ms-access vba ms-access-2010


【解决方案1】:

你没有提到每个表中的主键是什么,所以我假设它是user 字段,并且t_maint_saved 中的每条记录都有一个唯一的user
如果不是,则将其替换为实际的主键。

请注意,date 是保留关键字,您不能将字段命名为 date,因此我将其重命名为 thedate

我创建了一个small sample database,它的工作方式与您描述的一样(至少据我了解)。

我用一些示例数据创建了这些表:

我创建了一个绑定到t_main 表的表单:

请注意,下拉框 cbParam1cbParam2 未绑定到任何字段,它们是未绑定

下拉框的行源有点小技巧,但效果很好。
例如,对于cbParam1.RowSource

SELECT param1_old 
FROM t_main 
WHERE user=Forms![FormMain]![User]

UNION ALL 

SELECT param1_new 
FROM t_main 
WHERE user=Forms![FormMain]![User];

此查询从t_main 记录中选择与当前显示的具有相同user 的旧字段和新字段。实际上,它会在组合框中显示当前记录的旧参数和新参数。

FormMain后面的代码主要用于管理显示。
如果下拉框之一为空,或者我们之前已经添加过该记录,我们将阻止用户将数据添加到 t_saved

Option Compare Database
Option Explicit

' We use this variable to keep track of whether the
' record was already found in the t_saved table
Private alreadysaved As Boolean

'-----------------------------------------------------------------------------
' Update the UI after we change our selection of parameter
'-----------------------------------------------------------------------------
Private Sub cbParam1_AfterUpdate()
    UpdateUI
End Sub

Private Sub cbParam2_AfterUpdate()
    UpdateUI
End Sub

'-----------------------------------------------------------------------------
' Enable/Disable the save button.
' The button is only enabled if the user selected both parameters
'-----------------------------------------------------------------------------
Private Sub UpdateUI()
    btAddData.Enabled = Not (IsNull(cbParam1) Or IsNull(cbParam2)) _
                        And Not alreadysaved
End Sub

'-----------------------------------------------------------------------------
' Refresh teh data every time we change record
'-----------------------------------------------------------------------------
Private Sub Form_Current()
    ' Reset the values of the parameters comboboxes
    cbParam1 = Null
    cbParam2 = Null
    cbParam1.Requery
    cbParam2.Requery

    ' Check if there is already a record for the same user in the t_save table
    alreadysaved = DCount("user", "t_saved", "user='" & user & "'") > 0

    ' Display a warning to tell the user the current record cannot be saved again
    lblInfo.Visible = alreadysaved

    UpdateUI
End Sub

重要的代码实际上是将数据添加到t_saved表中的新记录:

'-----------------------------------------------------------------------------
' The button was clicked.
' Save the current record data to the t_save table
'-----------------------------------------------------------------------------
Private Sub btAddData_Click()
    ' We create a new new record in t_save and copy our data
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("t_saved", dbOpenDynaset, dbFailOnError)
    With rs
        .AddNew
            !user = user
            !thedate = thedate
            !name = name
            !param1 = cbParam1
            !param2 = cbParam2
        .Update
        .Close
    End With
    Set rs = Nothing
    Set db = Nothing

    ' Force the form to refresh itself
    ' This will cause the Form's OnCurrent event to be triggered
    Me.Requery
End Sub

【讨论】:

  • 哇!非常感谢雷诺!我还没有时间实现所有东西,但这正是我想要的,我希望在接下来的几天里我能完成它。再次感谢您为记录和明确所有内容所做的所有努力。非常感谢!
猜你喜欢
  • 2017-04-09
  • 1970-01-01
  • 2012-01-24
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多