Results 1 to 7 of 7

Thread: Simple if-else statement not working

  1. #1

    Thread Starter
    Addicted Member CoMMiE's Avatar
    Join Date
    Jul 2000
    Location
    Malaysia, Kuala Lumpur
    Posts
    179

    Angry

    Hi

    Private Sub Command2_Click()
    If Text1.Text = " " And Text2.Text = "" Then
    MsgBox "textbox is empty "
    End If
    End Sub


    The above if-else statement doesnt seems to work
    Anyone can detect anything wrong with the code?


  2. #2
    Guest
    Well, there isn't anything wrong, other than:

    Code:
    If Text1.Text = " " And Text2.Text = "" Then
    You have a space at Text1.Text = " "

    Even if you were meant to use the space, it seems to work fine.

    Sunny

  3. #3
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    The code is ok, but it will only fire the MessageBox when both Textboxes are empty, to make the code work if one of (or both) the textboxes are empty replace the AND with OR
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  4. #4

    Thread Starter
    Addicted Member CoMMiE's Avatar
    Join Date
    Jul 2000
    Location
    Malaysia, Kuala Lumpur
    Posts
    179
    Thanks for the reply
    I tried a simpler if else statement that goes:

    Code:
    if text1.text=" " then
    msgbox"textbox 1 is empty"
    endif

    even thought i fufill the condition above no mesage box is display

  5. #5
    Guest
    CoMMIE, " " doesn't mean it is empty, "" means it is empty. " " is the same as pressing the space bar once but thats not the same as nothing. " " still takes space to represent.

    Your code should be
    Code:
    Private Sub Command2_Click() 
    If Text1.Text = "" And Text2.Text = "" Then 'space deleted
    MsgBox "textbox is empty " 
    End If 
    End Sub
    Sunny

  6. #6
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    I'm sorry but what are you trying to do?!
    does the text1 exist at all and what's in it when pressing the button? when you delete all the contents in a textbox, the value of the textbox is "", not " " (< space)
    Where do you fire the If Then Statement?

    Code:
    Private Sub Command1_Click()
    'Will check if both boxes are empty
    If Text1.Text = "" And Text2.Text = "" Then
    MsgBox "textbox 1 is empty"
    End If
    End Sub
    
    Private Sub Command1_Click()
    'Will check if one of the boxes are empty
    If Text1.Text = "" And Text2.Text = "" Then
    MsgBox "textbox 1 is empty"
    End If
    End Sub
    I don't get it, what's not working for you?

    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  7. #7

    Thread Starter
    Addicted Member CoMMiE's Avatar
    Join Date
    Jul 2000
    Location
    Malaysia, Kuala Lumpur
    Posts
    179
    Hi
    I already found my solution thanks to Yonatan

    Code:
    Private Sub Command1_Click()
    If Trim(Text1.Text) = vbNullString Then
    MsgBox " "
    Else
    
    End If
    Anyway thanks for spending time reading my post


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width