如何更新用户表单中的条目?

Posted

技术标签:

【中文标题】如何更新用户表单中的条目?【英文标题】:How to Update Entry In User Form? 【发布时间】:2021-04-13 14:14:23 【问题描述】:

我创建了一个用户输入表单,它获取表格中的数据并将其显示为 VBA 表单中的列表框。我有两个切换按钮,可以循环显示数据。我有文本框/组合框在表单上显示该数据。

我无法让表单更新数据。当表单中的数据发生更改时,我设法让第一列更新,但我无法让其他任何工作。所以主要是cmdUpdate_Click sub中的代码。

Private Sub cmdClose_Click()
    Unload Me
End Sub

Private Sub cmdRefresh_Click()

    Dim x As Integer

    For x = 0 To Me.lstMyData.ListCount - 1
        If Me.lstMyData.Selected(x) Then Me.lstMyData.Selected(x) = False
    Next x

End Sub

Private Sub ComboBox1_DropButtonClick()
    microParaNameButt Me.ComboBox1
End Sub

'Private Sub ComboBox1_Change()
'    microParaName Me.ComboBox1
'End Sub

Private Sub lstMyData_Click()

    Dim i As Integer

    i = Me.lstMyData.ListIndex
    Me.lstMyData.Selected(i) = True
    
    Me.TextBox1.Value = Me.lstMyData.Column(0, i)
    Me.ComboBox1.Value = Me.lstMyData.Column(0, i)
    
    Me.TextBox2.Value = Me.lstMyData.Column(1, i)
    Me.ComboBox2.Value = Me.lstMyData.Column(1, i)
    
    Me.TextBox3.Value = Me.lstMyData.Column(2, i)
    Me.ComboBox3.Value = Me.lstMyData.Column(2, i)
    
    Me.TextBox4.Value = Me.lstMyData.Column(3, i)
    Me.ComboBox4.Value = Me.lstMyData.Column(3, i)
    
    Me.TextBox5.Value = Me.lstMyData.Column(4, i)
    Me.ComboBox5.Value = Me.lstMyData.Column(4, i)
    
    Me.TextBox6.Value = Me.lstMyData.Column(5, i)
    Me.ComboBox6.Value = Me.lstMyData.Column(5, i)
    
    Me.TextBox7.Value = Me.lstMyData.Column(6, i)
    Me.ComboBox7.Value = Me.lstMyData.Column(6, i)
    
    Me.TextBox8.Value = Me.lstMyData.Column(7, i)
    Me.ComboBox8.Value = Me.lstMyData.Column(7, i)
    
    Me.TextBox9.Value = Me.lstMyData.Column(8, i)
    Me.ComboBox9.Value = Me.lstMyData.Column(8, i)
    
    Me.TextBox10.Value = Me.lstMyData.Column(9, i)
    Me.ComboBox10.Value = Me.lstMyData.Column(9, i)

End Sub

Private Sub ToggleButton1_Click()

    Dim i As Integer

    i = Me.lstMyData.ListIndex
    Me.lstMyData.Selected(i) = True
    
    Me.TextBox1.Value = Me.lstMyData.Column(0, i - 1)
    Me.ComboBox1.Value = Me.lstMyData.Column(0, i - 1)
    
    Me.TextBox2.Value = Me.lstMyData.Column(0, i - 1)
    Me.ComboBox2.Value = Me.lstMyData.Column(0, i - 1)

    Me.TextBox3.Value = Me.lstMyData.Column(0, i - 1)
    Me.ComboBox3.Value = Me.lstMyData.Column(0, i - 1)
    
    Me.TextBox4.Value = Me.lstMyData.Column(0, i - 1)
    Me.ComboBox4.Value = Me.lstMyData.Column(0, i - 1)
    
    Me.TextBox5.Value = Me.lstMyData.Column(0, i - 1)
    Me.ComboBox5.Value = Me.lstMyData.Column(0, i - 1)
    
    Me.TextBox6.Value = Me.lstMyData.Column(0, i - 1)
    Me.ComboBox6.Value = Me.lstMyData.Column(0, i - 1)
    
    Me.TextBox7.Value = Me.lstMyData.Column(0, i - 1)
    Me.ComboBox7.Value = Me.lstMyData.Column(0, i - 1)
    
    Me.TextBox8.Value = Me.lstMyData.Column(0, i - 1)
    Me.ComboBox8.Value = Me.lstMyData.Column(0, i - 1)
    
    Me.TextBox9.Value = Me.lstMyData.Column(0, i - 1)
    Me.ComboBox9.Value = Me.lstMyData.Column(0, i - 1)
    
    Me.TextBox10.Value = Me.lstMyData.Column(0, i - 1)
    Me.ComboBox10.Value = Me.lstMyData.Column(0, i - 1)

    Me.lstMyData.Selected(i - 1) = True

End Sub

Private Sub ToggleButton2_Click()

    Dim i As Integer

    i = Me.lstMyData.ListIndex
    Me.lstMyData.Selected(i) = True
    
    Me.TextBox1.Value = Me.lstMyData.Column(0, i + 1)
    Me.ComboBox1.Value = Me.lstMyData.Column(0, i + 1)
    
    Me.TextBox2.Value = Me.lstMyData.Column(0, i + 1)
    Me.ComboBox2.Value = Me.lstMyData.Column(0, i + 1)

    Me.TextBox3.Value = Me.lstMyData.Column(0, i + 1)
    Me.ComboBox3.Value = Me.lstMyData.Column(0, i + 1)
    
    Me.TextBox4.Value = Me.lstMyData.Column(0, i + 1)
    Me.ComboBox4.Value = Me.lstMyData.Column(0, i + 1)
    
    Me.TextBox5.Value = Me.lstMyData.Column(0, i + 1)
    Me.ComboBox5.Value = Me.lstMyData.Column(0, i + 1)
    
    Me.TextBox6.Value = Me.lstMyData.Column(0, i + 1)
    Me.ComboBox6.Value = Me.lstMyData.Column(0, i + 1)
    
    Me.TextBox7.Value = Me.lstMyData.Column(0, i + 1)
    Me.ComboBox7.Value = Me.lstMyData.Column(0, i + 1)
    
    Me.TextBox8.Value = Me.lstMyData.Column(0, i + 1)
    Me.ComboBox8.Value = Me.lstMyData.Column(0, i + 1)
    
    Me.TextBox9.Value = Me.lstMyData.Column(0, i + 1)
    Me.ComboBox9.Value = Me.lstMyData.Column(0, i + 1)
    
    Me.TextBox10.Value = Me.lstMyData.Column(0, i + 1)
    Me.ComboBox10.Value = Me.lstMyData.Column(0, i + 1)
    
    Me.lstMyData.Selected(i + 1) = True

End Sub

Private Sub UserForm_Initialize()

    Dim x As Integer

    For x = 0 To Me.lstMyData.ListCount - 1
        If Me.lstMyData.Selected(c) Then Me.lstMyData.Selected(x) = False
    Next x
    
End Sub

Private Sub cmdUpdate_Click()

Dim rowslect As Double
Dim i As Integer
Dim wb As Workbook
Dim ws As Worksheet

Set ws = Worksheets("Sheet3")
i = Me.lstMyData.ListIndex
rowselect = i + 1

With ws
    .Cells((i + 2), 1).Value = Me.TextBox1.Value
    .Cells((i + 2), 2).Value = Me.TextBox2.Value
    .Cells((i + 2), 3).Value = Me.TextBox3.Value
    .Cells((i + 2), 4).Value = Me.TextBox4.Value
    .Cells((i + 2), 5).Value = Me.TextBox5.Value
    .Cells((i + 2), 6).Value = Me.TextBox6.Value
    .Cells((i + 2), 7).Value = Me.TextBox7.Value
    .Cells((i + 2), 8).Value = Me.TextBox8.Value
    .Cells((i + 2), 9).Value = Me.TextBox9.Value
    .Cells((i + 2), 10).Value = Me.TextBox9.Value
End With

End Sub

【问题讨论】:

在您的 cmdUpdate_Click 子程序中设置断点或 Debug.Print 语句,并仔细检查文本框中的值。有时,表单上可见的值不是代码中访问的值。查看this answer 进行有趣的讨论。 嘿@PeterT - 感谢您的信息!我已将 Debug.Print 语句添加到子程序中,并且我可以看到其他文本框值没有被彻底提取。我可以更改表格第一列中的数据,但其他一切似乎都不起作用。我将 .value 更改为 .text,但没有任何区别 为什么要为每个项目同时填充文本框和组合框?而且您的代码在某些地方看起来很奇怪:例如。 lstMyData_ClickToggleButton1_Click 大概使用不同的代码来访问相同的数据? lstMyData 最初是如何填充的? 【参考方案1】:

看起来对工作表的第一次更新会触发lstMyData_Click() 事件,该事件会使用其原始值刷新其他文本框。为了防止这种情况,您可以使用全局变量作为标志来指示何时进行更新。

Option Explicit
Dim bUpdate As Boolean

Private Sub cmdUpdate_Click()

    Dim wb As Workbook, ws As Worksheet
    Dim iRow As Long, n As Integer
    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Sheet3")
    
    iRow = Me.lstMyData.ListIndex + 1
    If iRow > 0 Then
        bUpdate = True
        For n = 1 To 10
            ws.Cells(iRow, n) = Me.Controls("TextBox" & n).Value
        Next
        bUpdate = False
    End If
    
End Sub

Private Sub lstMyData_Click()

    If bUpdate Then Exit Sub

    Dim i As Long, n As Integer
    i = Me.lstMyData.ListIndex
    For n = 1 To 10
        Me.Controls("TextBox" & n).Value = Me.lstMyData.Column(n - 1, i)
        Me.Controls("ComboBox" & n).Value = Me.lstMyData.Column(n - 1, i)
    Next

End Sub

Private Sub cmdRefresh_Click()

    Dim X As Long, n As Integer
    For X = 0 To Me.lstMyData.ListCount - 1
        If Me.lstMyData.Selected(X) Then Me.lstMyData.Selected(X) = False
    Next X
    For n = 1 To 10
        Me.Controls("TextBox" & n).Value = ""
        Me.Controls("ComboBox" & n).Value = ""
    Next

End Sub

Private Sub ToggleButton1_Click()

    Dim i As Long, n As Integer
    i = Me.lstMyData.ListIndex
    If i < 0 Then
       i = 0
    ElseIf i > 0 Then
       i = i - 1
    End If
    Me.lstMyData.Selected(i) = True
    Call lstMyData_Click

End Sub

Private Sub ToggleButton2_Click()

    Dim i As Long, n As Integer
    i = Me.lstMyData.ListIndex
    If i < Me.lstMyData.ListCount - 1 Then
       i = i + 1
    End If
    Me.lstMyData.Selected(i) = True
    Call lstMyData_Click

End Sub

Private Sub UserForm_Initialize()
    Call cmdRefresh_Click
End Sub

Private Sub cmdClose_Click()
    Unload Me
End Sub

【讨论】:

以上是关于如何更新用户表单中的条目?的主要内容,如果未能解决你的问题,请参考以下文章

Ruby on Rails 复选框未在提交表单时更新

刷新表单以在 Visual Studio/C# 的组合框中更新和保存新条目

C#二维数组验证表单中的条目

读取动态表单的元素

如何在 Wordpress Gravity Forms 中使用 PHP 创建条目

在 Rails 中使用嵌套表单 - 如何使显示页面根据用户想要的呈现数量以表格格式显示每个新条目?