Ms access 表单中的组合框(Access 2010)-从2个不同字段检索值
我需要在access 2010上创建一个表单,在表(Ms access 表单中的组合框(Access 2010)-从2个不同字段检索值,ms-access,vba,ms-access-2010,Ms Access,Vba,Ms Access 2010,我需要在access 2010上创建一个表单,在表(t_main)中的记录之间导航 但问题是,我需要一个组合来选择param1_old和new,以及param2_old和new 此组合将有两个值,每个参数有旧值和新值。最后,在选择要为该用户保留的参数后,我将单击一个按钮并将此信息保存到一个新表中(t\u saved) t_main具有以下结构: user; date; name; param1_old; param1_new; param2_old; param2_new user; date
t_main
)中的记录之间导航
但问题是,我需要一个组合来选择param1_old
和new,以及param2_old
和new
此组合将有两个值,每个参数有旧值和新值。最后,在选择要为该用户保留的参数后,我将单击一个按钮并将此信息保存到一个新表中(t\u saved
)
t_main
具有以下结构:
user; date; name; param1_old; param1_new; param2_old; param2_new
user; date; name; param1; param2
及
t\u saved
具有以下结构:
user; date; name; param1_old; param1_new; param2_old; param2_new
user; date; name; param1; param2
你知道怎么做吗?我应该使用记录集吗?有没有办法避免这种情况,只需强制组合将两个不同字段中的值放入值列表中
非常感谢你的帮助
编辑:
我知道理解我需要什么相当复杂,我将尝试在截图中显示:
表中数据如下:
用户;日期;名称帕拉穆奥;帕拉莫新;帕拉莫2岁;帕拉莫新酒店
1234568789;"21/07/2014";“约翰·史密斯”;“柠檬街125';'鳄梨大道123号‘’;‘……’;‘……’
您没有提到每个表中的主键是什么,因此我假设它是
user
字段,并且t\u main
和t\u saved
中的每个记录都有一个唯一的user
如果不是,则用实际的主键替换 请注意,
date
是一个保留关键字,您无法命名字段date
,因此我将其重命名为日期
我已经创建了一个如您所描述的(至少在我理解的情况下)工作的
我使用一些示例数据创建了这些表:
我创建了一个绑定到t\u main
表的表单:
'-----------------------------------------------------------------------------
' 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
请注意,下拉框cbParam1
和cbParam2
未绑定到任何字段,它们未绑定
下拉框的rowsource有点老套,但效果很好。例如,对于
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\u 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
代码的重要位实际上是将数据添加到保存的表中的新记录中的位:
'-----------------------------------------------------------------------------
' 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
你的描述对我来说毫无意义。你能用一些示例数据编辑你的帖子吗,以及你希望看到的结果。哇!非常感谢雷诺!我还没有时间去实现所有的事情,但这正是我想要的,我希望在接下来的几天里我能完成它。再次感谢您为记录和澄清一切所做的努力。谢谢!