带 ComboBox 的用户窗体 - 根据所选选项向数据库输入值
UserForm w/ ComboBox - enter value to database based on selected option
我有一个用于输入调查数据的用户窗体,有 32 个组合框。每个 ComboBox 都有相同的一组响应,每个响应都有相应的分数。当用户点击保存时,而不是将响应保存到数据库中,我想要分数。以下是分数:
最大损失:-2
死亡人数:-1
Ni de acuerdo/en desacuerdo: 0
De acuerdo: 1个
Muy de acuerdo: 2
但是,在某些情况下,回复会被反向评分(因此 "muy de acuerdo" 会收到 -2 而不是 2)。
早些时候有人给了我一个循环遍历并将正确的选项列表分配给每个 ComboBox 的代码:
comboItems() = 数组("Muy en desacuerdo", _
"En desacuerdo",_
"Ni de acuerdo/en desacuerdo",_
"De acuerdo",_
"Muy de acuerdo")
For Each ct In Me.Controls
If TypeName(ct) = "ComboBox" And _
ct.Name <> "cboGender" And _
ct.Name <> "cboDepartment" Then
For i = LBound(comboItems) To UBound(comboItems)
ct.AddItem comboItems(i)
Next i
End If
Next ct
下面是将每个字段添加到数据库的代码示例:
Private Sub cmdAdd_Click()
'Copy input values to sheet.
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("CRUDO")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
ws.Unprotect ""
.Cells(lRow, 1).Value = Me.txtFecha.Value
.Cells(lRow, 2).Value = Me.txtTime.Value
.Cells(lRow, 3).Value = Me.txtPlace.Value
.Cells(lRow, 4).Value = Me.cboDepartment.Value
.Cells(lRow, 5).Value = Me.txtMunicipality.Value
.Cells(lRow, 6).Value = Me.txtGroupName.Value
.Cells(lRow, 7).Value = Me.txtComponent.Value
.Cells(lRow, 8).Value = Me.txtComments.Value
.Cells(lRow, 9).Value = Me.txtName.Value
.Cells(lRow, 10).Value = Me.txtBirthDate.Value
.Cells(lRow, 11).Value = Me.cboGender.Value
.Cells(lRow, 12).Value = Me.cboAE1A.Value
.Cells(lRow, 13).Value = Me.cboAE2A.Value
.Cells(lRow, 14).Value = Me.cboAE3A.Value
.Cells(lRow, 15).Value = Me.cboAE4A.Value
.Cells(lRow, 16).Value = Me.cboAE5A.Value
因此,在上面的示例中,假设 cboAE1A 根据上面的等级正常评分。然而,AE2A 和 AE5A 是反向计分的。
这可能吗?最简单的方法是什么?
首先,为了保存分数而不是实际响应,那么你应该将代码更改为:
.Cells(lRow, 1).Value = Me.txtFecha.ListIndex -2
如果你想在 AE2A 上逆转比分:
.Cells(lRow, 2).Value = -(Me.txtTime.ListIndex -2)
我有一个用于输入调查数据的用户窗体,有 32 个组合框。每个 ComboBox 都有相同的一组响应,每个响应都有相应的分数。当用户点击保存时,而不是将响应保存到数据库中,我想要分数。以下是分数:
最大损失:-2 死亡人数:-1 Ni de acuerdo/en desacuerdo: 0 De acuerdo: 1个 Muy de acuerdo: 2
但是,在某些情况下,回复会被反向评分(因此 "muy de acuerdo" 会收到 -2 而不是 2)。
早些时候有人给了我一个循环遍历并将正确的选项列表分配给每个 ComboBox 的代码: comboItems() = 数组("Muy en desacuerdo", _ "En desacuerdo",_ "Ni de acuerdo/en desacuerdo",_ "De acuerdo",_ "Muy de acuerdo")
For Each ct In Me.Controls
If TypeName(ct) = "ComboBox" And _
ct.Name <> "cboGender" And _
ct.Name <> "cboDepartment" Then
For i = LBound(comboItems) To UBound(comboItems)
ct.AddItem comboItems(i)
Next i
End If
Next ct
下面是将每个字段添加到数据库的代码示例:
Private Sub cmdAdd_Click()
'Copy input values to sheet.
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("CRUDO")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
ws.Unprotect ""
.Cells(lRow, 1).Value = Me.txtFecha.Value
.Cells(lRow, 2).Value = Me.txtTime.Value
.Cells(lRow, 3).Value = Me.txtPlace.Value
.Cells(lRow, 4).Value = Me.cboDepartment.Value
.Cells(lRow, 5).Value = Me.txtMunicipality.Value
.Cells(lRow, 6).Value = Me.txtGroupName.Value
.Cells(lRow, 7).Value = Me.txtComponent.Value
.Cells(lRow, 8).Value = Me.txtComments.Value
.Cells(lRow, 9).Value = Me.txtName.Value
.Cells(lRow, 10).Value = Me.txtBirthDate.Value
.Cells(lRow, 11).Value = Me.cboGender.Value
.Cells(lRow, 12).Value = Me.cboAE1A.Value
.Cells(lRow, 13).Value = Me.cboAE2A.Value
.Cells(lRow, 14).Value = Me.cboAE3A.Value
.Cells(lRow, 15).Value = Me.cboAE4A.Value
.Cells(lRow, 16).Value = Me.cboAE5A.Value
因此,在上面的示例中,假设 cboAE1A 根据上面的等级正常评分。然而,AE2A 和 AE5A 是反向计分的。
这可能吗?最简单的方法是什么?
首先,为了保存分数而不是实际响应,那么你应该将代码更改为:
.Cells(lRow, 1).Value = Me.txtFecha.ListIndex -2
如果你想在 AE2A 上逆转比分:
.Cells(lRow, 2).Value = -(Me.txtTime.ListIndex -2)