如果然后说明以评估组合框并打开用户形式



我试图让用户从组合框中选择一个表单,基于选择,打开关联的用户形式,并在新打开的用户表单中从textbox1打开Textbox1的输入文本。我已经尝试了某些案例,如果没有运气的话。帮助请

Private Sub PrintSheet_Click()
Dim calltype As Variant
calltype = ComboBox1.Value
If calltype = "fire" Then
    NewRunUserForm.Show
    PrintUserForm.TextBox1.Value = NewRunUserForm.TextBox1.Value
    ElseIf calltype = "accident" Then
    AccidentUserForm.Show
    PrintUserForm.TextBox1.Value = AccidentUserForm.TextBox1.Value
    ElseIf calltype = "training" Then
    TrainingUserForm.Show
    PrintUserForm.TextBox1.Value = TrainingUserForm.TextBox1.Value
    ElseIf calltype = "meeting" Then
    MeetingUserForm.Show
    PrintUserForm.TextBox1.Value = MeetingUserForm.TextBox1.Value

End If
End Sub

我还尝试更改Calltype,这似乎也没有用。当我单击命令按钮时,什么也不会发生。以上是我最近尝试过的代码。我想要一个与Combox选择的用户形式。

尝试以下代码。它至少会告诉您组合框中选择的值是多少。另外,在检查If条件时将值转换为较低的情况。

Private Sub PrintSheet_Click()
  Dim calltype As Variant
  calltype = ComboBox1.Value
  MsgBox "Value in ComboBox1: [" & CStr(calltype) & "]" 'remove this statement it later
  If LCase$(calltype) = "fire" Then
    NewRunUserForm.Show
    PrintUserForm.TextBox1.Value = NewRunUserForm.TextBox1.Value
  ElseIf LCase$(calltype) = "accident" Then
    AccidentUserForm.Show
    PrintUserForm.TextBox1.Value = AccidentUserForm.TextBox1.Value
  ElseIf LCase$(calltype) = "training" Then
    TrainingUserForm.Show
    PrintUserForm.TextBox1.Value = TrainingUserForm.TextBox1.Value
  ElseIf LCase$(calltype) = "meeting" Then
    MeetingUserForm.Show
    PrintUserForm.TextBox1.Value = MeetingUserForm.TextBox1.Value
  End If
End Sub

基本上,打开的形式为" me"。您的组合将是me.com.bobox1,但是当前表格可以省略ME指定。现在,您必须创建另一个表单对象。在您的代码中,您命名了这些表格,但您没有声明它们。在下面的代码中,我给这些表格较短,然后声明它们。

私有子printsheet_click()

Dim calltype As String
Dim SubForm As Object
calltype = ComboBox1.Value
Select Case calltype
    Case "accident"
        Set SubForm = New FrmAccident
    Case "fire"
        Set SubForm = New FrmFire
    Case "meeting"
    Case "training"
End Select
With SubForm
    .TextBox1.Value = Me.TextBox1.Value
    .Show
End With 

结束子

用户形式对象" subform"被声明为延迟绑定的对象。然后,根据组合中的选择,将现有表单之一分配给子形式对象。最后,从当前表单(即"我")中分配了子形式中的textbox1(无论是哪个),并显示了子图。您可以添加像我这样的代码。在此时hide hide以使ME形式在打开时暂时消失,然后在使用子形式完成后再次显示。

最新更新