Two If statements inside each other in VBA - vba

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

Related

How to add items in bunifudropdown control

How do you add items like for example in a combobox we use this code
//this is the code for combobox
If ComboBox1.SelectedItem = "A" Then
ComboBox2.Items.Add("101")
ComboBox2.Items.Add("102")
Else
If ComboBox1.SelectedItem = "B" Then
ComboBox2.Items.Add("201")
ComboBox2.Items.Add("202")
End If
End If
//this is the code i tried
If BunifuDropdown1.selectedValue = "A" Then
BunifuDropdown2.Items.add("101")
BunifuDropdown2.Items.add("102")
Else
If BunifuDropdown1.selectedValue = "B" Then
BunifuDropdown2.Items.add("201")
BunifuDropdown2.Items.add("202")
End If
End If
I need a code like this for BunifuDropdown1
please help!

if statment with var actual text

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

Limit to ElseIf Conditions?

So I have some VBA code designed to go through a few things based on a value in a cell. The value in the cell is read and the code runs accordingly. This works without issue with.
If Target.Value = "something" Then
End If
ElseIf Target.Value = "something2" Then
End If
ElseIf Target.Value = "something3" Then
End If
ElseIf Target.Value = "something4" Then
End If
That works perfectly, however if I add an additional ElseIf condition I get the compiler error of "Else without If". Changing ElseIf to Else on the fifth condition doesn't fix the issue. My question is that is there a limit to the amount of ElseIf conditions I can run? I literally only need this fifth one to be entirely done with what I am trying to do. I am positive the code within the condition has no errors.
The End If goes at the end only:
If Target.Value = "something" Then
'Do something
ElseIf Target.Value = "something2" Then
'Do something
ElseIf Target.Value = "something3" Then
'Do something
ElseIf Target.Value = "something4" Then
'Do something
End If
Definitely you should "upgrade" your multiple ElseIf and go with Select Case.
This will let you implement more scenarios in the future easier.
Select Case Target.Value
Case "something"
' code 1 here
Case "something2"
' code 2 here
Case "something3"
' code 3 here
Case "something4"
' code 4 here
Case "something5", "something6" ' <-- replaces using another If with And
' code 5 here
' Case ....
End Select

If textbox.text = more then 1 option

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

Remove repetition from if-else statement

I have written an if...else statement which uses an array myArr and a string myStr as follows:
If myArr.Length > 0 AndAlso myArr(0) = "-1" Then
'Do stuff 1
ElseIf myStr= "xyz" Then
'Do stuff 2
ElseIf myArr.Length > 0 Then
'Do Stuff 3
Else
'Do Nothing
End If
It works exactly as I need. But It looks really confusing, mostly because the array length is checked twice. There must be a clearer way to write it, but I can't think of one.
Just to clarify.... the order that each statement is executed is crucial. i.e. Stuff 1 has priority over stuff 2, which has priority over stuff 3.
I don't think you'll be able to get exactly the same flow in a simpler way.
Either you're going to end up doing things different things, or doing duplicate things. eg:
If myArr.Length > 0 Then
If myArr(0) = "-1" Then
'Do stuff
Else
'Do stuff
End If
ElseIf myStr= "xyz" Then
'Do stuff
Else
'Do Nothing
End If
This will cause mystr="xyz" not to happen whenn myArr(0) <> -1 whereas it may have before.
You might be able to get rid of multiple checks against myArr.Length by using nested If statements, but this makes the code less clear.
A clear approach (i.e. one that can be understood easily) is one that allows you to read a code fragment without having to remember the context in which the code is executed. By nesting If statements, more information must be kept in the readers working memory (or short term memory) to deduce the meaning of he code.
I guess this would do the trick:
If myArr.Length > 0 Then
If myArr(0) = "-1" Then
'Do stuff
Else
'Do stuff
End If
ElseIf myStr= "xyz" Then
'Do stuff
Else
'Do Nothing
End If
What you need is nested if
If myArr.Length > 0 Then
'Do stuff for non zero length
If myArr(0) = "-1" Then
'Do stuff for -1 and you already have checked for array length
End If
End If
If myArr.Length > 0 Then
If myArr(0) = "-1" Then
' Stuff 1
Else
' Stuff 3
End If
ElseIf myStr = "xyz"
' Stuff 2
End If
... no further else needed
You can decompose the statements like this:
If myArr.Length > 0 Then
If myArr(0) = "-1" Then
'Do stuff
Else
'Do sstuff
End If
Else
If myyStr= "xyz" Then
'Do stuff
End If
End If