Struggling with Another If And Or Statement

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
714
Office Version
  1. 365
Platform
  1. Windows
So, I'm struggling with yet another one of these statements. I've tried both sets of code below, and I keep getting the message box when each box is completed, except for the txt_Street2 box. The idea is that if any of the 5 are not null, but either Street1, City, ST or Zip are null, I should get the message box. How have I coded this incorrectly?

Code:
'If Not Len(Me.txt_Street1.Value) = 0 Or Not Len(Me.txt_Street2.Value) = 0 Or Not Len(Me.txt_City.Value) = 0 Or Not Len(Me.cobo_ST.Value) = 0 Or Not Val(Me.txt_Zip.Text) = 0 And _'(Len(Me.txt_Street1.Value) = 0 Or Len(Me.txt_City.Value) = 0 Or Len(Me.cobo_ST.Value) = 0 Or Val(Me.txt_Zip.Text) = 0) Then
'    Response = MsgBox("Please complete the partial address entered.")
'    If Response = vbOK Then Me.txt_Street1.SetFocus
'    Exit Sub
'End If


If Not Len(Me.txt_Street1.Value) = 0 Or Not Len(Me.txt_Street2.Value) = 0 Or Not Len(Me.txt_City.Value) = 0 Or Not Len(Me.cobo_ST.Value) = 0 Or Not Len(Me.txt_Zip.Value) = 0 And _
(Len(Me.txt_Street1.Value) = 0 Or Len(Me.txt_City.Value) = 0 Or Len(Me.cobo_ST.Value) = 0 Or Len(Me.txt_Zip.Value) = 0) Then
    Response = MsgBox("Please complete the partial address entered.")
    If Response = vbOK Then Me.txt_Street1.SetFocus
    Exit Sub
End If
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
So, I'm struggling with yet another one of these statements. I've tried both sets of code below, and I keep getting the message box when each box is completed, except for the txt_Street2 box. The idea is that if any of the 5 are not null, but either Street1, City, ST or Zip are null, I should get the message box. How have I coded this incorrectly?

Code:
'If Not Len(Me.txt_Street1.Value) = 0 Or Not Len(Me.txt_Street2.Value) = 0 Or Not Len(Me.txt_City.Value) = 0 Or Not Len(Me.cobo_ST.Value) = 0 Or Not Val(Me.txt_Zip.Text) = 0 And _'(Len(Me.txt_Street1.Value) = 0 Or Len(Me.txt_City.Value) = 0 Or Len(Me.cobo_ST.Value) = 0 Or Val(Me.txt_Zip.Text) = 0) Then
'    Response = MsgBox("Please complete the partial address entered.")
'    If Response = vbOK Then Me.txt_Street1.SetFocus
'    Exit Sub
'End If


If Not Len(Me.txt_Street1.Value) = 0 Or Not Len(Me.txt_Street2.Value) = 0 Or Not Len(Me.txt_City.Value) = 0 Or Not Len(Me.cobo_ST.Value) = 0 Or Not Len(Me.txt_Zip.Value) = 0 And _
(Len(Me.txt_Street1.Value) = 0 Or Len(Me.txt_City.Value) = 0 Or Len(Me.cobo_ST.Value) = 0 Or Len(Me.txt_Zip.Value) = 0) Then
    Response = MsgBox("Please complete the partial address entered.")
    If Response = vbOK Then Me.txt_Street1.SetFocus
    Exit Sub
End If

I like to error trap and append to string then display it once. Something like:
Code:
Dim errmsg as string
if trim(me.text1)="" then
errmsg = "Textbox1 data needed"
endif
if trim(me.textbox2)="" then
errmsg = errmsg & vbcr & "Textbox2 data needed"
endif
if trim(me.textbox3)="" then
errmsg = errmsg & vbcr & "Textbox3 data needed"
endif
if errmsg<>"" then
msgbox errmg, vbcritical,"FORM INCOMPLETE"
exit sub
endif
'if ok then continue with macro...

Obviously you can personalize your messages more.
 
Upvote 0
Finally realized that I hadn't enclosed the first part of the statement in parentheses.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top