Can anybody explain the uses of ampersand sign over here.Kindly let me know the idea.
Code:StrSql = "Select * from " & tablename
Printable View
Can anybody explain the uses of ampersand sign over here.Kindly let me know the idea.
Code:StrSql = "Select * from " & tablename
I concatenates what ever value is in tablename, if any, to the select statement. For example:
tablename = "Contacts"
Then StrSql will contain:
"Select * from Contacts"
& (ampersand) concatenates two strings. In other languages this is usually done with a + (plus) operator (which you can use here also).
So,
StrSql = "Select * from " & tablename
concatenates "Select * from " and tablename. Since tablename is a variable, the value of tablename is used.
I'd stay away from the plus sign for concatenations as a practice. Although it will work in the example given it can cause a type mis-match error with numerics. That's just my way and that doesn't make it right. This will cause an error (lngValue can probably be wrapped to make it work but why deal with that):
Code:Dim strsql As String
Dim lngValue As Long
lngValue = 10
strsql = "Select * from TableName where Value = " + lngValue