This should be the easiest question all day!
All I want to see is how to condense some code.
Example:
If textbox.text = "0000" then
'do something
End If
If textbox.text = "0001" then
'do something
End If
What I want to do, is have that in 1 statement.
You can use the .Contains from an array of data. Here is a simple example.
Dim choices = {"0000","0001","0002"}
If choices.Contains(textbox.text) Then
'do something
End If
If you want each condition to do something different:
If testbox.text = "0000" Then Do.Something Else If testbox.text = "0001" Then Do.SomethingDifferent
If you have multiple conditions to test to do the same thing:
If testbox.text = "0000" OR testbox.text = "0001" OR testbox.text = "0002" Then Do.Something
Related
Can i make short that my code IF statement in one single IF Statement?
If randomNumber = strWords2(StrwrVal.Text) Then
Else
If randomNumber = strWords3(StrwrVal.Text) Then
Else
If randomNumber = strWords4(StrwrVal.Text) Then
Else
If randomNumber = strWords5(StrwrVal.Text) Then
Else
TxtRnd1.Text = TxtRnd1.Text & vbNewLine & randomNumber
End if
End if
End if
End if
You should NEVER have an empty If block. If you don't want to do something if a condition is True, don't test whether that condition is True in the first place. Test for the inverse condition. In your case, you should be doing this:
If randomNumber <> strWords2(StrwrVal.Text) AndAlso
randomNumber <> strWords3(StrwrVal.Text) AndAlso
randomNumber <> strWords4(StrwrVal.Text) AndAlso
randomNumber <> strWords5(StrwrVal.Text) Then
TxtRnd1.AppendText(Environment.NewLine & randomNumber)
End if
Instead of a bunch of Else and If, or a bunch of ElseIf, you can use a Select Case. It's easier to read, among other things.
The Select Case will evaluate a variable and you can choose the outcome depending on what you find. You can also test for True if you want to evaluate things more complicated than one variable.
Select Case randomNumber
Case strWords2(StrwrVal.Text)
'some code
Case strWords3(StrwrVal.Text)
'some other code
Case strWords4(StrwrVal.Text)
'you got the idea
Case Else
TxtRnd1.Text = TxtRnd1.Text & vbNewLine & randomNumber
End Select
For what I read this would be the cleanest answer, but it always depends on the algorithm. Have fun!
I have a very simple code and my is problem is that I want to return a string in different circumstances based on ElseIf but somehow it does not work at all.
If the score is 6 in cell A1 then, the code should return specific text in the cell next to ("Excellent") etc. The code does not want to return the text at all. Can somebody tell me why?
Sub ElseIf_ex()
Dim score As Integer, score_comment As String
note = Range("A1").Value
score_comment = Range("B1").Value
If note = 6 Then
score_comment = "Excellent"
ElseIf note = 5 Then
score_comment = " Good"
ElseIf note = 4 Then
score_comment = "Satisfactory"
Else
score_comment = "Zero"
End If
End Sub
You will have to assign score_comment back to some cell, otherwise your code might work, but not output anything. You missed to add something like
Range("B1").Value=score_comment
just before the End Sub line.
I need a little help with Access VBA.
I need to define a variable in a form from user input and, depending on the choice, create an If statement with that var.
Something like this:
'defining var
if text.value = 12 then
ST = "If Not HasValue(Forms!co.Theme) Or Not HasValue(Forms!co.cxlogin) Then"
else
ST = "If Not HasValue(Forms!co.message) Or Not HasValue(Forms!co.fone) Or Not HasValue(Forms!co.cxlogin) Then"
end if
Then, in another form, call the var and "inject" the var in the statement...
ST 'here i like to have the actual text in the var so i can build the statement
else
end if
The HasValue function is in a module and works well, also I declare the var Public so I can call it from any form I need to:
Option Compare Database
public ST as string
Am I shooting the moon, or is it real easy and I just can't see it?
You can store the result:
Dim b As Boolean
If text.value = 12 Then
b = Not HasValue(Forms!co.Theme) Or Not HasValue(Forms!co.cxlogin)
Else
b = Not HasValue(Forms!co.message) Or Not HasValue(Forms!co.fone) Or Not HasValue(Forms!co.cxlogin)
End If
and then use it :
If b Then
'...
Else
'...
End If
Technically you could write code on the fly, but it's complicated and can cause a lot of problems, and I can guarantee that there's another way to accomplish what you need to do.
Without seeing more of your code & the form I can't say for sure, but perhaps some nested If's would be better, something like this:
Option Compare Database
Option Explicit 'always try to use this option too (for unrelated reasons!)
Public myFlag As Boolean
...and then...
If Text.Value = 12 Then myFlag = True Else myFlag = False
...and then...
If myFlag Then
If Not HasValue(Forms!co.Theme) Or Not HasValue(Forms!co.cxlogin) Then
'…_do something here_…
Else
'…_do something here_…
End If
Else
If Not HasValue(Forms!co.Message) Or Not HasValue(Forms!co.fone) Or Not HasValue(Forms!co.cxlogin) Then
'…_do something here_…
Else
'…_do something here_…
End If
End If
Also, is HasValue just checking if a control on the form has a value? If so, another way to accomplish the same thing would be something like:
If myFlag Then
If Nz(Forms!co.Theme, "") = "" Or Nz(Forms!co.cxlogin, "") = "" Then
'…_do something here_…
Else
'…_do something here_…
End If
Else
If Nz(Forms!co.Message, "") = "" Or Nz(Forms!co.fone, "") = "" Or Nz(Forms!co.cxlogin, "") = "" Then
'…_do something here_…
Else
'…_do something here_…
End If
End If
I tried to write two If statements inside each other in VBA, but it gives me wrong answer. When I debug it, it shows that the program doesn't go through the ElseIf and keeps going with the else of the first If.
How can I have two If statements working properly inside each other?
Code:
If ConfigBox.Value <> ... Then
If ListBox1.Value = ... Then
DO SOMETHING
Else
DO SOMETHING
End If
ElseIf ListBox1.Value = ... Or ListBox1.Value = ... Then
DO SOMETHING
Else
DO SOMETHING
End If
Your nesting is a bit off. You would want to do something like this:
If ConfigBox.Value <> ... Then
If ListBox1.Value = ... Then
Code
ElseIF ListBox1.Value = ... Or ListBox1.Value = ... Then
Code
Else
Code 'if ListBox1.Value doesn't meet above criteria
End If
Else
Code 'if ConfigBox criteria is not met. You could start another nested If for the ListBox1 here too.
End If
try with below
If ((ConfigBox.Value <> "1") And (ListBox1.Value = "2")) Then
'Do Something
ElseIf ((ConfigBox.Value <> "1") And (ListBox1.Value <> "2")) Then
'Do Something
ElseIf ListBox1.Value = "3" Or ListBox1.Value = "4" Then
'Do Something
Else
'Do Something
End If
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.
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
....
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.