处理空的组合框
Posted
技术标签:
【中文标题】处理空的组合框【英文标题】:Handling an empty combobox 【发布时间】:2021-08-21 12:50:41 【问题描述】:您好,我正在尝试根据组合框中的答案自动生成报告。
我正在尝试检查组合框是否为空但没有成功... 这是我的代码,地址和用户名都是组合框...
Option Compare Database
Option Explicit
Private Sub generateReport_Click()
Dim address As String
Dim UserName As String
address = Me.custAddress
UserName = Me.UserName
If Len(address.Value Or UserName.Value) > 0 Then
MsgBox "Please Choose a Form Name!", vbOKOnly
Me.custAddress.SetFocus
End If
Debug.Print (address)
DoCmd.OpenReport "Generation", acViewPreview, address & UserName, _
WhereCondition:="AddressLine1 = '" & address & UserName & "'"
End Sub
编辑版本:
Option Compare Database
Option Explicit
Private Sub generateReport_Click()
If custAddress.Value = vbNullString Or UserName.Value = vbNullString Then
Debug.Print ("fail")
Else
DoCmd.OpenReport "Generation", acViewPreview, Me.custAddress & Me.UserName, _
WhereCondition:="AddressLine1 = '" & Me.custAddress & Me.UserName & "'"
End If
End Sub
【问题讨论】:
If address = vbNullString Or UserName = vbNullString Then
或者您可以取出字符串变量,只需 If custAddress.Value = vbNullString Or UserName.Value = vbNullString Then
谢谢它的工作,虽然我发现另一个错误“无效的限定符”是由于点击功能被私有化?你能解释一下原因吗
不完全确定,但如果没有,请删除 Dim UserName As String
语句,因为您也有一个名为 UserName
的组合框,或者将该变量重命名为其他名称,所以您会混淆它
哦,我明白了,但是我认为我刚刚将代码更改为更好一点,尽管它不会在 if 语句中跳转以在即时窗口中打印失败。我在编辑下的主要问题中发布了编辑后的代码。
试试IsNull(UserName.Value)
【参考方案1】:
您将有一个 Filter 或一个 WhereCondition,因此请尝试:
Option Compare Database
Option Explicit
Private Sub GenerateReport_Click()
Dim Wherecondition As String
If IsNull(Me!custAddress.Value + Me!UserName.Value) Then
MsgBox "Please Choose a Form Name!", vbOKOnly
Me!custAddress.SetFocus
Else
' Seems like a strange address line, though ...'
Wherecondition ="AddressLine1 = '" & Me!custAddress.Value & Me!UserName.Value & "'"
DoCmd.OpenReport "Generation", acViewPreview, , WhereCondition
End If
End Sub
【讨论】:
以上是关于处理空的组合框的主要内容,如果未能解决你的问题,请参考以下文章