如果Statement更改field.control源

时间:2016-07-29 14:59:07

标签: vba if-statement access

我有一个小的If语句,如果另一个字段为空,则会更改字段的controlsource。 “就在之前=是不正确的,我不知道该用什么来确保包含以=开头的整个字符串。

Private Sub Report_Load()
If IsNull(FirstName2) Then
OwnersNames.ControlSource = "=FirstName1] & " " & [LastName1]"
Else
OwnersNames.ControlSource = "=[FirstName1] & " " & [LastName1] & " and " & [FirstName2] & " " & [LastName2]"
End If
End Sub

4 个答案:

答案 0 :(得分:1)

如果我正确读取此内容,请尝试使用更多引号来连接该语句:

Private Sub Report_Load()
If IsNull(FirstName2) Then
    OwnersNames.ControlSource = "=[FirstName1]" & " " & "[LastName1]"
Else
    OwnersNames.ControlSource = "=[FirstName1]" & " " & "[LastName1]" & " and " & "[FirstName2]" & " " & "[LastName2]"
End If
End Sub

答案 1 :(得分:0)

如果您尝试连接字段(例如'将它们添加到一起)。你需要这样的东西:

Private Sub Report_Load()
    If IsNull(FirstName2) Then
        OwnersNames.ControlSource = "=[FirstName1] & ' ' & [LastName1]"
    Else
        OwnersNames.ControlSource = "=[FirstName1] & ' ' & [LastName1] & ' ' & [FirstName2] & ' ' & [LastName2]"
    End If
End Sub

请记住,您可以将文本字段(例如文本框)连接在一起。

我不确定组合字段的控制源是否以相同的方式工作。可以肯定的是,一个选项是将每个文本框重命名为与其控件源不同的内容。

答案 2 :(得分:0)

这里是现在的代码,加载后会生成John Smith和'即使Firstname2字段为空。

 Private Sub Report_Load()
 If Me.FirstName2 = vbNullString Then
 OwnersNames.ControlSource = "=[FirstName1] & ' ' & [LastName1]"
 Else
 OwnersNames.ControlSource = "=[FirstName1] & ' ' & [LastName1] & ' and ' & [FirstName2] & ' ' & [LastName2]"

End If
End Sub

答案 3 :(得分:0)

你真的有意义设置控制源或只是字段的值吗? 我想这可能是你想要完成的事情????

Private Sub Report_Load()
 If Me.FirstName2 = "" Then
 me.OwnersNames = [FirstName1] & " " & [LastName1]
 Else
 me.OwnersNames = [FirstName1] & " " & [LastName1] & _  
" and " & [FirstName2] & " " & [LastName2]

End If
End Sub