Does the += operator just not exist in VBA? - vba

I'm trying to incriment the value in a cell, but despite documentation saying Visual Basic allows the += operator, it's just giving me "Compile error: Expected: expression".
Range("CellName").Value += 1
Is what's breaking, but if I do
Range("CellName") = Range("CellName") + 1
It works fine

No, it doesn't exist in VBA.
VB.NET might take += (though I'm not even sure about that).
You'll have to use
Range("CellName").Value = Range("CellName").Value+1
A good reference can be found here

An "official" list of VBA-operators can be found in VBA help here:
Help --> Microsoft Visual Basic Help --> Visual Basic for Applications Language Reference --> Visual Basic Language Reference --> Operators --> Arithmetic Operators (online here )
Maybe controversially, but the next advice would have saved me lots of time and headaches:
I'd say it's better to forget everything about VB and focus at VBA, as the two are different languages. People don't mention "Oh, OCaml, APL, PHP or Fortran have increment operators", as it's off-topic if the scope is VBA. In the same way, what VB has or has not is off-topic, and usually it only adds to the confusion because it's resemblance. Better use the MS-Office provided "local" Visual Basic for Applications Language Reference as provided by the help system, or online:
http://msdn.microsoft.com/en-us/library/office/gg264383%28v=office.15%29.aspx

It's really annoying. I made the following functions to make my life a bit easier:
' ++
Function pp(ByRef x, Optional y = 1)
x = x + y
pp = x
End Function
' --
Function mm(ByRef x, Optional y = 1)
x = x - y
mm = x
End Function
Doesn't really help your situation as you'd still have to type the same amount:
Range("CellName") = pp(Range("CellName"))
but for simple loop increments it helps :
do while cells(x,1) <> ""
pp x
loop

No it does not have it for numbers, e.g. see http://msdn.microsoft.com/de-de/library/215yacb6%28v=vs.80%29.aspx / http://msdn.microsoft.com/en-us/library/cey92b0t.aspx

Related

MS Project equivalent to "xlAnd". Enumeration for logical operators

I'm trying to write a language independent filter for MS Project in VBA. I'm using the syntax:
FilterEdit (Name, Taskfilter, Create, Fieldname, Test, Value, Operation...)
I have managed to get the Fieldnames and Tests to be language independent, but I struggle with the Operation:= expression. For an English locale one would write: Operation:="and" but that doesnt work for other locales.
Is there a way to write the logical operator (and/or) as an enumeration? (not as a string?)
For Excel one could write xlAnd, and Project has a lot of enumerations starting with Pj, ie. PjTaskStart. I also know there's a Filter.LogicalOperationType, but I haven't managed to figure out if this could work for me or not. I have also experimented with FieldConstantToFieldName, but I reckon there's no fieldname for the logical operator?
I know I could use If LocaleID = xxxx Then..., but I'd like to not assume what locales will be in use.
Edit: I solved the first part of my problem!
By leaving it blank Operation:="", Project returns "And". But I haven't figured out yet how to return "Or"...
Operation:="" works for FilterEdit, but not for SetAutoFilter.
So I ended up using the dreaded If LocaleID.
Teaching moment:
I found out most operators can be language independent, except for:
And, Or, Contains and Does Not Contain.
These needs to be translated for each locale. I'll get to those in a minute. First I'll list all the language independent operators:
< Less than <= Less than or equal to > Greater than >= Greater than or equal to = Equal to <> Not equal to
My trick for finding the translations I need for the language dependent operators is the following MS Office Support page.
Notice the category named "Filter for specific text" in the English support page. Here we can read all the "words" we need. Now go to the bottom of the web page and change the language:
This opens up a new page listing all the different languages (not locale specific). Remembering where you found the word for Contains in English, then changing the language to for instance "Magyar (Magyarorzág)", we can now see that Contains = "Tartalmazza" in Magyar.
Next step is to google "Magyar languge" and learn that this actually equals Hungarian. So now you can go to this MSDN web page to see that Hungarian = LocaleID: 1038.
Putting all this together inside VBA makes you have to write the following code:
Dim LocalContains As String
If LocaleID = 1038 Then
LocalContains = "Tartalmazza" 'Hungarian
ElseIf LocaleID = 1044 Then
LocalContains = "inneholder" 'Norwegian
Else
LocalContains = "contains" 'English
End If

Cannot convert string to type 'Double'

I'm starting to code in, and learn, VB.NET. And so far, it's been smooth sailing.
Until I try to run the program.
What I've done is, in a language I know and understand, wrote a hangman game. And in C#, the code works perfectly. Once I got it to this finished point where I can say that there is nothing else I wish to change about it, I started hand-converting it to VB.NET.
So far, no problem. But I just finished converting it, and now I have hit my snag.
On the bottom of the window is a status bar, telling you which puzzle set you're in and which puzzle you're on. When selecting a puzzle, this line of code throws an error:
stsPuzzles.Text = "Puzzle: " + regionPuzzles + "/" + maxPuzzles
The error is:
Conversion from string "Puzzle" to type 'Double' is not valid.
Of course, the easy answer would be to take this mechanic out, but at least for testing purposes, I'd like it in there so I can make sure the right puzzles are in the right sets.
Is there a way I can fix this so my two integer variables can be in the string? Or is there a work around that I can at least use long enough for testing purposes for the rest of the testing process?
I'm hoping to find a way to fix this, as there are other places, such as displaying stats, that need to be able to do this.
The direct fix for your existing code is this:
stsPuzzles.Text = "Puzzle: " + CStr(regionPuzzles) + "/" + CStr(maxPuzzles)
or this:
stsPuzzles.Text = "Puzzle: " & regionPuzzles & "/" & maxPuzzles
In VB.Net, &, rather than +, is the concatenation operator. + will often still work, but it also has a tendency to think you wanted arithmetic when an operand is numeric.
But what I would really do in this case, is this:
stsPuzzles.Text = String.Format("Puzzle: {0}/{1}", regionPuzzles, maxPuzzles)
or with Visual Studio 2015 or later:
stsPuzzles.Text = $"Puzzle: {regionPuzzles}/{maxPuzzles}"

When does = perform comparison instead of assignment?

In VB.NET, there's no == operator for comparison, so the = operator serves that purpose as well as assignment. I have a function, and I want it to return the boolean result of a comparison, without storing that result in a variable:
Private Function foo() As Boolean
Dim bar As Integer = 1
Return bar = 2
End Function
Returns: False
OK, but what's the value of bar?
Private Function foo() As KeyValuePair(Of Boolean, Integer)
Dim bar As Integer = 1
Return New KeyValuePair(Of Boolean, Integer)(bar = 2, bar)
End Function
Returns: False, 1
It looks like = will perform a comparison when the statement context demands it, but is this guaranteed? That is, can I be sure that bar will never be set to 2 in this situation?
Also, I know that VB.NET doesn't allow chained inline assignments, which may be for the best. Does this odd = behavior cause any other quirks I should be aware of?
You cannot do in-line assignments in VB, Assignment is an explicit statement:
[Let] <<target-reference>> = <<value-expression>>
The Let is optional and implicit, and hardly ever used anymore. The general rule that you can use to distinguish the [Let] command from equality testing is that for Let, no other keyword may come before the target-reference in the statement. AFAIK, in all cases of = as equality testing, there is one or more other keywords that precede it in the statement.
In your first example, the keyword Return precedes your =, so it's an equality test, and not an assignment.
In your first example you can do either:
Return 2
or
bar = 2
Return bar
As for your question "OK, but what's the value of bar?", bar still equals one.
= in VB cause no quirks. It works exactly as documented, and it always has (including its predecessor, BASIC back to 1968).
If you are starting to code in VB (coming from a language like C#), you should start getting used to the peculiar VB way of doing things; which is based on the idea: as simple and intuitive for the programmer as possible. "If assignation and comparison happen always in different contexts, why not using the same operator and let the context define its exact meaning?" -> VB-way of seeing things. "No, different realities have to be accounted for by different operators. End of the discussion" -> C#-way. :)
Is this reliable? Can you blindly trust on these not-always-clear-for-a-programmer bits? Sure, VB.NET peculiarities are highly-reliable and trustworthy. You can always use = (or Is on some contexts, but VS would tell you) and be completely sure that the code will do what is expected. But the question is: are you sure that you write exactly what you want?
This last question is what, perhaps, is more criticable of VB and what might give some problems to programmers from other languages: the higher the flexibility, the more likely is that you make an error; mainly if you are used to a different format.
Regarding the chained inline assignments, I honestly don't see its true utility (and never use them in C#). Regarding other differences with respect to C#, there are plenty of them; in some cases, I think that the C# approach is better; other times, the VB.NET one. On readability/length of code, I can refer to the With Statement I have always found somehow useful which is not present in C#.
One way to have 100% sure that the expression will be evaluated as an boolean expression is to use ()
e.g
Dim a = 2
Return (a = 1)
Since you cannot set a value to a variable wihtin the parenthesis.
What i want to say is: on an return statament for example you cant assing a value to a variable so, even if you use
a = 1
The compilator knows that this expression only can be an boolean expression.
The same to the if statament and so on..
Heh back in QB45 days we used to exploit the fact that "True" was the numeric value -1. So you would see code like x = 1 - x * (x < 6) (translation: increment x, but reset to 1 when it gets to 6)

Mathematica: Commands return no output, but itself. Bug?

I am working with Wolfram Mathematica 8 and have the following problem. I have an optimization problem under certain constraints and want to have an analytical (symbolical solution). I am maximizing function piA. My input is:
piA[a_, WA1_, WA0_] =
a/(1 + a)*(X - (y*WA1 + 1)^(1/y)) - 1/(1 + a) ((y*WA0 + 1)^(1/y));
Maximize[{piA[a, WA1, WA0], WA0 >= -1/y, WA1 >= -1/y}, WA0]
What I get most of the times is:
Maximize[{-((1 + WA0 y)^((1/y))/(1 + a)) + (
a (X - (1 + WA1 y)^(1/y)))/(1 + a), WA0 >= -(1/y), WA1 >= -(1/y)},a]
Basically, the command does nothing, but outputs itself. Only once I have managed to get the proper output (too long to paste here). I have tested it with simpler functions and it works. Unfortunately, I cannot understand what causes the problem. It is not a syntax problem, since it has worked like that several times. Any help would be very much appreciated.
P.S. Just checked again and my input ALWAYS generates the wrong output. The time it generated the solution was when I accidentally set parameters X and y to certain numbers.
The most likely reason is that given the function and constraints, Mathematica doesn't know how to maximize your function with respect to WA0. Note you also have a free variables X and a in there, and it might not have enough information about the domain of X and a to be able to properly form a solution to your equation.
I've had instances where I tried feeding in some equations and constraints and Mathematica simply couldn't do anything with them because they were too general. This may be the case here as well. Is there a specific problem you're trying to solve, and is there any way you could give Mathematica more context?
I don't think this is a bug at all, but it's unfortunate that sometimes Mathematica will just spit back your input when it doesn't have any rules for solving what you gave it.
The usual reason these things happens seems to be when the expressions given are too general for Mathematica to handle, or when it it's faced with a set of expressions that are ill formed.
Just as an example, I tried passing in fractions into a function I wrote that specifically looked for rational expressions, thinking it would work. It turned out that it needed to handle both Rational[a, b] and Times[a, Power[b, -1]]. It could be the case that Mathematica is not expecting a constraint to be of the form GreaterEqual[a, b].
Mathematica returns an answer if you assign the variable a some value. Maybe you could build your strategy on that? In fact it does provide an answer if you assign a value to any of the variables.
( I would need more background of the problem to go from there... )

Using math functions in Sql with MS Access

I designed a query in SQL View using MS Access:
select floor(num1) from t1;
When I run it, I get "undefined function floor".
I get similar errors for Ceil, Mod,Power, Sign, Sqrt, Trunc, and initcap functions.
Does the Access database engine's SQL syntax have equivalent functions?
Replace Floor() with Int(). I learned this by searching in the Access help files, in this case, hitting F1 while in the query designer, and searching for "functions." That took me to a help topic comparing VBA and T-SQL functions.
You should probably have a look at the Access database engine SQL Reference. I can't find a good online reference for functions that are supported through the Jet/ACE and Access expression services. For some unknown reason, the Access Help has not included Jet/ACE expressions since Jet 3.0 and this aged resource was finally removed from MSDN a year or two ago :(
Keep in mind that the Jet/ACE expression service for use outside Access supports a much smaller subset of functions that is possible using the Access Expression Service when running your SQL inside Access 2007. Broadly speaking, the VBA5 functions (as distinct from methods) that involve simple data types (as distinct from, say, arrays or objects) are supported outside of the Access user interface; for an approximate list of function names see the 'Use Sandbox mode operations with Jet 4.0 Service Pack 3 and later' section of this MSDN article.
Also, the functions reference in the VBE help should be a starting place.
The help files are not perfect, but a little searching ought to get you what you need.
Public Function Floor(ByVal x As Double) As Double
'Be Because VBA does not have a Floor function.
'Works for positive numbers
'Turns 3.9 -> 3
'Note: Round(3.9) = 4
Dim s As String, dPos As Integer
s = CStr(x)
dPos = InStr(s, ".")
Floor = CLng(Left(s, dPos - 1))
End Function
As mentioned, Floor isn't available in access, you should use int() as an alternative. If you insist on using Floor, you could always create a vba module function in your mdb file similar to below but that is probably overkill.
Public Function floor(dblIn As Double, dec As Integer) As Double
decPosition = InStr(Str(dblIn), ".")
x = Left(dblIn, decPosition + dec - 1)
floor = x
End Function
Similar to the other math operations you described above you may create additional functions to create this set of functionality.