Peter Mogford
Peter Mogford

Reputation: 496

creating a loop around my select Case

At present, I have a functioning Select Case that states that if a textbox is blank then it is to be highlighted red, but it only seems to be highlighting one of the textboxes. For instance, if 2 textboxes are left blank, it only highlights the first on it comes across.

Select Case True
    Case Me.CustName = ""
    Me.CustName.BackColor = &H8080FF
    Case Me.RegAddress = ""
    Me.RegAddress.BackColor = &H8080FF
    Case Me.PostInput = ""
    Me.PostInput.BackColor = &H8080FF
    Case Me.Landline = ""
    Me.Landline.BackColor = &H8080FF
    Case Me.Contact = ""
    Me.Contact.BackColor = &H8080FF
    Case Me.DOBInput = ""
    Me.DOBInput.BackColor = &H8080FF
End Select

Being new to VBA, my only thinking is to create a loop round my current code that state (loop until x,y or z is <> "") but I can't seem to figure out how to do this.

Any advice will be greatly appreciated.

Upvotes: 3

Views: 87

Answers (2)

rory.ap
rory.ap

Reputation: 35260

You are using Select Case for the wrong purpose. Its purpose is to test a single expression and execute one branch based on the value of that expression.

What you need to do is test each of your text boxes individually, using if statements:

If Me.CustName = "" Then Me.CustName.BackColor = &H8080FF

'etc.

Upvotes: 4

Bond
Bond

Reputation: 16311

Select Case runs the code block following the first matching Case statement only. If you need to check each of your conditions regardless, you should write them as individual If statements instead:

If Me.CustName   = "" Then Me.CustName.BackColor   = &H8080FF
If Me.RegAddress = "" Then Me.RegAddress.BackColor = &H8080FF
If Me.PostInput  = "" Then Me.PostInput.BackColor  = &H8080FF
....

Upvotes: 4

Related Questions