Combo box in form (Access 2010) - Retrieving values from 2 different fields
-
21-12-2019 - |
Вопрос
I need to create a Form on access 2010 that navigates among records from a table (t_main
)
but the problem is that I need a combo to select among param1_old
and new and also among param2_old
and new.
This combo will have 2 values, the old and the new for each parameter. In the end, after I select which parameters I want to keep for this user, I will click in a button and save this information into a new table (t_saved
).
t_main
has the following structure:
user; date; name; param1_old; param1_new; param2_old; param2_new
and
t_saved
has the following structure:
user; date; name; param1; param2
Any idea on how to do that? Should I work with recordsets? is there a way to avoid it and just force the combo to take values from 2 different fields into a value list?
Thanks a lot for any help!
edit: I know it is rather complex to understand what I need, I will try to show in a screenshot:
the data in the table are as following:
user; date; name; param1_old; param1_new; param2_old; param2_new
1234568789;"21/07/2014";"John Smith";'Lemon street 125';'Avocado avenue 123'; '...'; '..'
Решение
You don't mention what is the primary key in each tables, so I will assume it's the user
field and that each record in t_main
and t_saved
has a unique user
.
If not, then substitute that with the actual primary key.
Note that date
is a reserved keyword and you cannot name a field date
, so I renamed it thedate
instead.
I have created a small sample database that works as you describe (at least as I understand it).
I created these tables with some sample data:
I created a form bound to the t_main
table:
Note that the dropdown boxes cbParam1
and cbParam2
are not bound to any field, they are left unbound.
The rowsource for the dropdown boxes is a bit of a hack, but it works well.
For instance, for 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];
This query selects both the old and new fields from the t_main
record that has the same user
as the one currently displayed. In effect, it shows both old and new parameters for the current record in the combobox.
The code behind the FormMain
is mostly used to manage the display.
Here we prevent the user from adding the data to t_saved
if one f the dropdown boxes is empty, or if we've already added that record before.
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 important bit of code is actually the one that adds the data to a new record int he t_saved
table:
'-----------------------------------------------------------------------------
' 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