Mixing variable types (1 Viewer)

shaneucc

Registered User.
Local time
Today, 06:44
Joined
Jul 25, 2013
Messages
28
I was wondering if anyone can tell me why this doesn't work?

Dim MooringLines As String
Dim MooringLinesPrice As Double
MooringLines = DLookup("TotalComponent", "ComponentT", "[TotalComponent] = '" & Me.P_MooringLinesCmb & "'")
MooringLinesPrice = DLookup("EuroPerMetre", "ComponentT", "[TotalComponent] = '" & Me.P_MooringLinesCmb & "'")
Me.P_MooringLinesTxt = (MooringLines + " - €" + (MooringLinesPrice * Me.P_LengthMLTxt.Value))

In the first variable I'm trying to get a string and combine it with a double in the second. Is it that I can't mix a string and a double in the one variable and if so how do I convert the double to a string?
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 22:44
Joined
Aug 30, 2003
Messages
36,131
What does "doesn't work" mean? For starters, try using & instead of + to concatenate. Mixing data types in the last line shouldn't matter.
 

shaneucc

Registered User.
Local time
Today, 06:44
Joined
Jul 25, 2013
Messages
28
Sorry the question was a bit vague bit changing + to & worked. Thank you
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 22:44
Joined
Aug 30, 2003
Messages
36,131
No problem.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:44
Joined
Feb 19, 2002
Messages
43,196
The & is the standard VBA concatenation character. The + is an alternate but will not work in every situation and has different properties when it does work.

The + is an arithmetic operator first and foremost so if the two operands are numeric, they will be added rather than concatenated.

The other difference occurs when one of the operands is null. Most operations result in null if either operand is null and that is true when the + is used so a + null results in null. However, a & null results in a. You can use this to your advantage when concatenating strings where some operands are optional and you want to eliminate the separators when the operand is null.
 

Users who are viewing this thread

Top Bottom