Jerry
Jerry

Reputation: 23

Handling an empty combobox

Hi I am trying to autogenerate reports based on answers in comboboxes.

I am trying to check if the combobox is empty but not having success... Here is my code both address and username are comboboxes...

    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

Edited version:

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

Upvotes: 0

Views: 161

Answers (1)

Gustav
Gustav

Reputation: 55806

You will either have a Filter or a WhereCondition, so try:

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

Upvotes: 1

Related Questions