if statment with var actual text - vba

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

Related

Comparing 2 Function String Results VB.Net

I have 2 separate functions, each one returns its own string value.
I am wanting to check that both return a specific string value and if they do then perform a task.
Example:
If function Apples returns "Apples" and Function Pears () returns "Pears" then do something
I am currently using 2 nested If statements and I want to know if this is the best way to do it.
If Apples() = "Apples" Then
If Pears() = "Pears" Then
"Do something here"
End If
End If
Using ANDALSO, if the first comparison fails, the second comparison is not made:
If String.Compare(Apples(),"Apples", True) = 0 ANDALSO String.Compare(Pears(),"Pears", True) = 0 Then
'Do Something Here
End If
Be careful with string comparisons, the comparison is case sensitive. "Apples" <> "APPLES".
Another version:
If Apples().ToUpper = "APPLES" ANDALSO Pears().ToUpper = "PEARS" Then
'Do Something Here
End If

Two If statements inside each other in 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

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

Access VBA - OrderBy with multiple field

I wonder how I should do this. Actually, I have some subform and, by clicking on the title, I want the recordset of this subform to orderby. This is an example of what I do:
Private Sub sigle_cours_Label_Click()
If (Me.OrderBy = "COU.sigle_cours") Then
Me.OrderBy = "COU.sigle_cours DESC"
Else
Me.OrderBy = "COU.sigle_cours"
End If
Me.OrderByOn = True
End Sub
My problem is this one : I want to OrderBy with sigle_cours AND num_cours but it don't work. I try this but there's no way to sort by DESC :
Private Sub sigle_cours_Label_Click()
If (Me.OrderBy = "COU.sigle_cours,COU.num_cours") Then
Me.OrderBy = "COU.sigle_cours DESC,COU.num_cours DESC"
Else
Me.OrderBy = "COU.sigle_cours,COU.num_cours"
End If
Me.OrderByOn = True
End Sub
How should I do this?
When you assign a string value to the OrderBy property, Access may transform it ... so won't exactly store what you expect. In your case, I suspect Access adds a space after the comma, so if you include this in your Form's code ...
Me.OrderBy = "COU.sigle_cours,COU.num_cours"
Debug.Print "Me.OrderBy='" & Me.OrderBy & "'"
You might see this in the Immediate Window ...
Me.OrderBy = 'COU.sigle_cours, COU.num_cours'
Actually I'm not positive that is the explanation for your problem. Nevertheless I suspect you're more likely to find joy by pattern-matching the current OrderBy value instead of testing for an exact match to a fixed string. Try it this way:
Private Sub sigle_cours_Label_Click()
If (Me.OrderBy Like "*DESC*") Then
Me.OrderBy = "COU.sigle_cours, COU.num_cours"
Else
Me.OrderBy = "COU.sigle_cours DESC, COU.num_cours DESC"
End If
Me.OrderByOn = True
End Sub