【问题标题】:UserForm to update Database based upon textbox用户窗体根据文本框更新数据库
【发布时间】:2014-02-16 20:38:38
【问题描述】:

我正在尝试输入员工 ID。一旦输入并按下命令按钮,它将使用数据库中列出的当前数据自动填充用户表单。然后用户可以更改任何输入,然后“提交”将粘贴回数据库的表单。我四处搜索,发现一些代码应该适用于我想做的事情,但我只是在努力理解如何使其适应我的特定需求。

    Option Explicit
Public frmName As Variant 'store row of name
Public frmDate As Variant 'store column of date

'Subroutine when clicking the first ("find") button
Private Sub btnfind_Click()
'Defining variables
Dim pr01 As String
Dim dt01 As Date
Dim tsk01 As Integer

'Assigning variables to inputs
pr01 = UserForm1.TextBox1.Text
dt01 = UserForm1.TextBox2.Text
tsk01 = UserForm1.TextBox3.Text

'Looking for Name in column "A"
With ThisWorkbook.Worksheets("Sheet4")
    frmName = .Columns("A").Find(pr01).Row
End With


'Looking for inut Date in row "1"
With ThisWorkbook.Worksheets("Sheet4")
    frmDate = .Rows(1).Find(CDate(dt01)).Column
End With

If frmName Is Nothing Or frmDate Is Nothing Then
    'not found
    Exit Sub
  End If

'Retrieving the existing number of tasks according to name and date
'and showing number in the 'tasks' text input box in the user form
With ThisWorkbook.Worksheets("Sheet4")
    UserForm1.TextBox3.Text = .Cells(frmName, frmDate)
End With

End Sub

'Subroutine when clicking the Second ("update") button
Private Sub btnupdate_Click()

'Paste updated Number of tasks in appropriate cells according to name and date
'The new number of tasks should over write what was there previously
With ThisWorkbook.Worksheets("Sheet4")
    .Cells(frmName, frmDate) = UserForm1.TextBox3.Text
End With
End Sub

frmName 和 frmDate 是两个文本框的名称吗?对于理解此代码以应用于我的电子表格的任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: database excel userform vba


    【解决方案1】:

    "frmXxxxx" 通常表示 Xxxxx 是一个用户表单。这里代码的第 2 行和第 3 行将 frmName 和 frmDate 声明为 Variants,并且 cmets 说它们用于一行和一列。这些名字真的很无用。

    通常,保存行或列的变量将声明为Long。由于使用它们的伪劣代码,它们必须声明为Variants。

    考虑:

    'Looking for Name in column "A"
    With ThisWorkbook.Worksheets("Sheet4")
      frmName = .Columns("A").Find(pr01).Row
    End With
    

    If frmName Is Nothing Or frmDate Is Nothing Then
      'not found
      Exit Sub
    End If
    

    变量pr01 已设置为UserForm1.TextBox1.Text。从 cmets 我假设这个文本框是供用户输入名称的。如果为文本框和变量赋予有意义的名称,将会很有帮助。

    如果在 A 列中找不到 pr01 中的名称,则 Find 将返回 Nothing。这就是frmName 被定义为Variant 的原因,因为Long 不能设置为Nothing

    虽然这可行,但它几乎不是用户友好或维护者友好的代码。

    除了那些可怕的名字,我建议如下:

    Public frmName As Long      'store row of name
    Dim rng as Range        ' Temporary variable
    
    'Looking for Name in column "A"
    With ThisWorkbook.Worksheets("Sheet4")
    
      Set rng = .Columns("A").Find(pr01)
      If rng Is Nothing Then
        ' Tell user the name could not be found
        :   :
        Exit Sub
      Endif
      frmName = rng.Row      
    
    End With
    

    我希望以上内容可以帮助您破译这段代码。我也希望我的代码更容易理解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多