提问人:Sorcha 提问时间:7/6/2023 最后编辑:topsailSorcha 更新时间:7/12/2023 访问量:47
为什么我不能在excel VBA表单的组合框中选择值?
Why can't I select values in a combo box in an excel VBA form?
问:
我在excel VBA表单中有一个组合下拉框,它不允许我在框中选择一个值,然后可以将其上传到SQL中。 我可以看到值,但我无法选择一个值,并且组合框仅在输入内容时显示下拉选项。 此外,这些值都对应于将上传到 SQL 表中的某个 ID 号。当未选择任何内容时,这会将值 0 上传到表中,但是没有零值,我想防止这种情况发生。如果没有选择任何内容,我不希望将任何内容上传到 SQL 中。
Private Sub cmb_Cause_Change()
Dim conn As New Connection
Dim rec As New Recordset
Dim comm As New Command
If Me.cmb_KPI.ListIndex <> -1 Then
' Clear the existing items in the cmb_Cause combo box
Me.cmb_Cause.Clear
' Open the connection and set up the command
conn.Open KPconnectionString
Set comm.ActiveConnection = conn
' Retrieve the cause_code and cause_id values from the Cause table
comm.CommandText = _
"SELECT Cause_code, Cause_id " & _
"FROM Cause " & _
"WHERE Kpi_applicable = '" & Me.cmb_KPI & "'"
rec.Open comm
' Populate the ComboBox with cause_code values
While Not rec.EOF
Me.cmb_Cause.AddItem rec!Cause_code
Me.cmb_Cause.ItemData(Me.cmb_Cause.ListCount - 1) = rec!Cause_id
rec.MoveNext
Wend
rec.Close
conn.Close
End If
' Select the PotentialFailure_ID value from the form
Dim PotentialFailure_ID As Integer
PotentialFailure_ID = Me.txt_PFID.value
' Open a new connection and set up the command
conn.Open KPconnectionString
Set comm.ActiveConnection = conn
' Insert the selected Cause_ID and PotentialFailure_ID into the CauseFailures table
comm.CommandText = _
"INSERT INTO CauseFailures (Cause_ID, PotentialFailure_ID) " & _
"VALUES (" & Me.cmb_Cause.ItemData(Me.cmb_Cause.ListIndex) & "," & PotentialFailure_ID & ")"
comm.Execute
conn.Close
End Sub
我正在尝试在 VBA 表单中创建一个组合下拉框,该下拉框从 SQL 表中提取值,然后在选择值后将结果上传到 SQL 中的另一个表中。该框已启用。 有谁知道为什么会这样,或者需要一行代码才能允许这种情况发生?那么为什么它只会在我输入字母时显示选项呢? 任何帮助将不胜感激!
答: 暂无答案
评论