What does "& _" mean in VB? - vb.net

I'm copying some query statements from a legacy VB app to a C# app. I am not familiar with VB, although looking at it makes me want a VB (Victoria Bitter). I have come across queries constructed like this:
*SELECT dp_duckbill_accounts.platypus_no AS duckbill, t_accounts.name AS Name " & _
"FROM t_accounts INNER JOIN dp_duckbill_accounts ON t_accounts.account_no = dp_duckbill_accounts.account_no " & _
"ORDER BY dp_duckbill_accounts.platypus_no*
The "& _" give me pause. If it was just "&" I would think it corresponds to "+" in C# to concatenate strings. But what in the world is the point of the underscore? Note the ampersand and the underscore are separated by a space.

The underscore is the line continuation character. It allows the concatenation to include a different line. Like so:
x = "Hello " & "World"
x = "Hello " & _
"World"
'this won't compile (pre vb.net 2010, anyway)
x = "Hello " &
"World"
Line Continuation on MSDN
How to: Break and Combine Statements in Code (Visual Basic)

_ means continue the statement on the following line.
so ... & _ means continue concatenating the string on the following line.
text = "One line string"
text = "Two line " & _
"string"

That is just a line continuation character that lets you continue to the next line.

& - is used for string concatenation in same line.
example - sConcatenatedString = "First" & "Second"
& _ - is used For string concatenation in different lines.
example - sConcatenatedString = "First" &_
"Second"

Related

RDLC - IIF expression error - Argument not specified for parameter 'FalsePart'

I have a RDLC report with following expression.
=Iif(
(Fields!EnvironmentalAuditorCompany.Value = "" and Fields!EnvironmentalAuditorPerson.Value<>""),
(Fields!EnvironmentalAuditorPerson.Value),
(
Iif(Fields!EnvironmentalAuditorPerson.Value = "" and Fields!EnvironmentalAuditorCompany.Value<>""),
(Fields!EnvironmentalAuditorCompany.Value),
Iif(Fields!EnvironmentalAuditorPerson.Value="" and Fields!EnvironmentalAuditorCompany.Value=""), " - ",
(Fields!EnvironmentalAuditorPerson.Value) & "," & vbCr & vbLf & (Fields!EnvironmentalAuditorCompany.Value)
)
)
However, I am getting the below error message
The Value expression for the textrun ‘EEnvAuditorPerson.Paragraphs[0].TextRuns[0]’ contains an error: [BC30455] Argument not specified for parameter 'FalsePart' of 'Public Function IIf(Expression As Boolean, TruePart As Object, FalsePart As Object) As Object'.
Please assist on this to resolve.
Below are the cases for the expression:
1. Might be a chance either one of the fields having value (AuditPerson/AuditCompany), if so fill the corresponding fields
2. Both of the fields will be empty, if so put - (Hypen)
3. Both of the fields having value, if so handle the value with comma separated in new line
You have mess of brackets in your expression and FalsePart was indeed missing for some of the IIFs. I'm not going to attempt to understand the meaning of your expression, but simply copying it into text editor and lining up your IIFs and fixing corresponding brackets, I got this:
=Iif(
(Fields!EnvironmentalAuditorCompany.Value = "" and Fields!EnvironmentalAuditorPerson.Value<>""),
(Fields!EnvironmentalAuditorPerson.Value),
Iif(
(Fields!EnvironmentalAuditorPerson.Value = "" and Fields!EnvironmentalAuditorCompany.Value<>""),
(Fields!EnvironmentalAuditorCompany.Value),
Iif(
(Fields!EnvironmentalAuditorPerson.Value="" and Fields!EnvironmentalAuditorCompany.Value=""),
" - ",
(Fields!EnvironmentalAuditorPerson.Value) & "," & vbCr & vbLf & (Fields!EnvironmentalAuditorCompany.Value)
)
)
)

ms access form fields into a string vba

I have a form (say) Data, which contains text boxes A,B,C.
I wish to write an email based on the form data. In my email body, I want the following format:
A is : (actual value of A in text box in the form) (a newline)
B is : ((actual value of B in text box in the form) ( a newline)
C is : ((actual value of C in text box in the form).
I know I can access values by Forms!Data!A_value (assuming I named the box as A_value). I am not able to combine them into a string and add a newline too.
I have tried the following:
Dim body as String
body = "A is : & Forms!Data!A_value &" & "B is : & Forms!Data!B_value &" & "C is : & Forms!Data!C_value &"
It is because I read an & results to a new line somewhere.
However, when i do that, the whole thing is concatenated as written in the code and no values are obtained from the form field.
Please suggest options:
Thanks in advance
You probably want something like:
Dim body as String
body = "A is : " & Forms!Data!A_value & vbNewLine & _
"B is : " & Forms!Data!B_value & vbNewLine & _
"C is : " & Forms!Data!C_value
Note: the fact that I wrote that code on 3 lines, using line continuation characters, is nothing to do with the insertion of the new line characters in the output. It could have also been written as
body = "A is : " & Forms!Data!A_value & vbNewLine & "B is : " & Forms!Data!B_value & vbNewLine & "C is : " & Forms!Data!C_value
but I find that harder to read.
The ampersand is used to concatenate text "hello " & "there".
If you quote the ampersand it does nothing, just reproduces the ampersand "bits & bobs".
You can use the character vbCrLf (carriage return/linefeed) to add (concatenate) a linebreak, "time for " & vbCrLf & "a break".
Another trick you can use is to create a single string template with placeholders for the values, then use Replace statements to fill them in, like:
body = "A is: {A} & B is: {B} & C is: {C}"
body = Replace(body, "{A}", Forms!Data!A_value)
body = Replace(body, "{B}", Forms!Data!B_value)
body = Replace(body, "{C}", Forms!Data!C_value)
And break out to multiple lines, like:
body = "A is: {A}{CR}B is: {B}{CR}C is: {C}{CR}"
body = Replace(body, "{A}", Forms!Data!A_value)
body = Replace(body, "{B}", Forms!Data!B_value)
body = Replace(body, "{C}", Forms!Data!C_value)
body = Replace(body, "{CR}", vbCrLf)

recordset.GetString in Access VBA Query returns an extra character after the result

I have a query that I execute through VBA in Access 2010. The result of the query should be AFR, but it returns AFR with an extra line below it. I have added the "'" character to make the extra line visible.
TempHold = rs.GetString
Debug.Print "'" & TempHold & "'"
Returns this:
'AFR
'
But should return this:
'AFR'
I have tried using the below code, but none of the If statements evaluate as True. The code should check for a " ", a vbNewLine, or vbCrLf character but none evaluate as true. Does anyone know of any additional characters that would result in a new line?
If Right(TempHold, 1) = " " Then
TempHold = Left(TempHold, Len(TempHold) - 1)
ElseIf Right(TempHold, 2) = vbNewLine Or Right(TempHold, 2) = vbCrLf Then
TempHold = Left(TempHold, Len(TempHold) - 2)
End If
Use:
Asc(Right(TempHold, 1))
to get the Ascii character code.
Once you've found the character code (which, as you wrote in your comment, was 13), you can use your code to remove it:
If Right(TempHold, 1) = Chr(13) Then
TempHold = Left(TempHold, Len(TempHold) - 1)
End If
In this case, you can also use vbCr, which is the same as Chr(13).
The best way to get rid of the carriage return in my opinion is to stop it being created in the first place. This method is a lot tidier than having to remove the last character.
In the .GetString method there is a parameter for RowDelimiter which by default is set to be a carriage return. However you can change this to be whatever you want including a zero length string as follows:
rs.GetString(, , , "")
If you run your debug again with this code:
rs.GetString(, , , "")
Debug.Print "'" & TempHold & "'"
You will get this result:
'AFR'
Remember if you want something different to be placed between rows then just change the zero length string to whatever you need.

Sql queries in VBA code - how to comment after Sql string

I have some Sql queries inside VBA code, this is an example of a line of code:
strQry = strQry & Worksheets("Data").Cells(9, 7) & """, " & _
I need to add a comment at the end of this line. Like this:
strQry = strQry & Worksheets("Data").Cells(9, 7) & """, " & _ 'comment hjjkk
I usually comment using an apostrophe but the character is not accepted on that line. Please advise. Also, I need the comment on that exact line if possible, at the end of it.
Thank you!
This has nothing to do with the SQL query itself, you are trying to add a comment after a line continuation, which is not allowed.
From the MSDN documentation on comments:
Comments cannot follow a line-continuation sequence on the same line.
If you do & _ it means you got code on the next line and that instruction is not finished.
So if you type a comment right after & _ it's like if your inserting a comment in the middle of your instruction
For example:
myVar = 3 + 5 & _ 'comment at the wrong place
+2
=
myVar = 3 +5 'comment at the wrong place + 2
So all you can do is
myVar = 3 + 5 'comment
myVar = myvar + 2
or in your case it would look like this
strQry = strQry & Worksheets("Data").Cells(9, 7) & """, " 'Comment
strQry = styQry &...
with the line break _ you cannot comment on that line as for VB the line doesn't stop yet (it is a feature to improve readability for us programmers). Get accustomed to comment before or after the complete string
Alternatively you can build the string up in parts and after each part you can add comments:
strQry = strQry & Worksheets("Data").Cells(9, 7) 'comment 1
strQry = strQry & ", " & <next part> 'comment2
etc...
The underscore at the end of the line signifies to VB that the code continues on the next line. You can't put anything after the underscore. If you need a comment at the end of the line, you will need to restructure your code to end that line, put your comment and continue your string concatenation on the next line.

How do I automate the built in "find and replace" function in MS-Access?

Is there a way to automate the find and replace function for MS Access?
I've got a lot of data I need to obscure (names and addresses), in a non-reversible way. It's going to an outside contractor that can't see the information (no NDA, etc. will do). But otherwise, I want the data to look as real as possible.
My plan right now is to do a find-n-replace on each character a-z and replace it with a random character. I recognise that chances are, I'll likely end up mapping two or more characters to the same value (which is not a bad thing in my books).
Ideally I'd like to have some kind of function that looks something like:
autoFindNReplace ("table name", "field name", _
"search char", random_alpha_generator(), _
DO_ALL_RECORDS)
And then I can run loop that on each field on each table that I have obscure.
My alternate methods are to:
walk each table and obscure each field individually.
try to come up with some sql statement that will do the same as the mythical autoFindNReplace I describe above.
You can just write a quick hash function in a VBA module and call it from a SQL Update query. Here's an example with table "Table1", and the field "address". The hashField code was taken from here.
Sub MaskAddress()
'Change 1234 to whatever key you'd like.
DoCmd.RunSQL "UPDATE Table1 SET address = hashField(address, 1234)"
End Sub
Public Function hashField(strIn As String, lngKey As Long) as String
Dim i As Integer
Dim strchr As String
For i = 1 To Len(strIn)
strchr = strchr & CStr(Asc(Mid(strIn, i, 1)) Xor lngKey)
Next i
hashField = strchr
End Function
Here was my solution:
Sub autoFindAndReplace(TableName As String, _
FieldName As String, _
Search As String, _
Replace As String)
Dim UpdateString As String
UpdateString = ("update " & TableName & _
" set " & FieldName & _
" = replace (" & FieldName & ", " & _
"""" & Search & """, """ & Replace & """)")
CurrentDb.Execute (UpdateString)
End Sub
Then I loop on autoFindAndReplace with my random character generator, once for alphas and once for numerics.
Yes, I could have done it with multiple Update statements - however, I had a lot of tables and fields to deal with, and this made it look cleaner.