Concatenating Title and Name Strings in Access - ms-access

I'm trying to concatenate the title, first name, and last name of a contact into one string in an Access report. Currently I am using a text field with the control set to
=[ContactTitle] & ' ' & [FirstName] & ' ' & [LastName]
However, some contacts don't have a title associated with them and my method leaves a leading space which makes the text alignment on my report look sloppy. So I'm wondering, is there a way to concatenate but only include that first space if the contact title is not null? I am aware of the plus operator but not experienced enough to see a way to use it in this case without just making my entire string null.

You can use the + operator for concatenation.
Concatenating with + yields Null if any of the values you're concatenating are Null:
=([ContactTitle] + " ") & ([FirstName] + " ") & [LastName]
Do note that some devs frown upon using + for concatenation, stating that & is the concatenation operator in VBA.
Also note that if one of the parameters is a zero-length string, this won't work. Only a real Null will lead to the result being Null.

You could use the IIf() method to check if the title is null and then depending on the answer insert or don't insert a space.
You can also use the Nz() method to check for zero-length strings at the same time as null strings by setting all null values to be a zero-length string and then comparing the result to "".
The code I would recommend you use here is:
=IIf(Nz([ContactTitle],"") = "", "", [ContactTitle] & " ") & [FirstName] & " " & [LastName]
If you you have any problems with this or need a better explaination leave a comment and I'll get back to you.

Related

MySQL 8.0 Generated Columns Problems

I'm upgrading an Access 2016 back-end database to MySQL 8.0 and having problems with the generated columns. I used a third-party tool to migrate my Access tables to MySQL, and for the most part it worked fine. The Access calculated columns are not migrated. Or rather, they are, but as standard columns. I need to go in and redefine the calculations in MySQL.
In Access, I have a Heading column which combines title, last name, first name, spouse name, spouse last name into a single heading. Here is the Access calculation which works just fine.
IIf([UseTitle],[Title] & " ","") & IIf(IsNull([DonorFirstName]),[DonorLastName],[DonorFirstName] & IIf(IsNull([SpouseName])," " & [DonorLastName],IIf(IsNull([SpouseLastName])," and " & [SpouseName] & " " & [DonorLastName]," " & [DonorLastName] & " and " & [SpouseName] & " " & [SpouseLastName])))
If this person wants to use a title (such as "Mr. & Mrs."), insert the title. If no first name, the last name's a company, so add the LastName field, otherwise add the FirstName field. If no spouse, add space and LastName field. If spouse with same last name, add " and " plus SpouseLastName. Otherwise, add LastName plus " and " plus SpouseFirstName plus space plus SpouseLastName. So now you either have "Mr. & Mrs. John and Jane Doe" or "Mr. & Mrs. John Doe and Jane Smith".
I tried doing this in MySQL using a combination of CONCATs and IFs like this:
`Heading` varchar(509) CHARACTER SET utf8 COLLATE utf8_general_ci GENERATED ALWAYS AS (concat(if(((`UseTitle` = 0) or (`Title` is null)),_utf8mb3'',concat(`Title`,_utf8mb3' ')),if((ifnull(`DonorFirstName`,_utf8mb3'') = _utf8mb3''),`DonorLastName`,concat(`DonorFirstName`,_utf8mb3' ',if((ifnull(`SpouseName`,_utf8mb3'') = _utf8mb3''),`DonorLastName`,if((ifnull(`SpouseLastName`,_utf8mb3'') = _utf8mb3''),concat(_utf8mb3'and ',`SpouseName`,_utf8mb3' ',`DonorLastName`),concat(`DonorLastName`,_utf8mb3' and ',`SpouseName`,_utf8mb3' ',`SpouseLastName`))))))) STORED,
This does NOT work, and in fact causes an error in the table DDL. It seems to update properly, but when I try to open the table design in MySQL Workbench, it gives me an error saying Error parsing DDL for tableName
When I choose to view the DDL in another tab, the generated field line is identified with an asterisk, and shows this:
`Heading` varchar(509) CHARACTER SET utf8 COLLATE utf8_general_ci GENERATED ALWAYS AS (concat(if(((`UseTitle` = 0) or (`Title` is null)),_utf8mb3'',concat(`Title`,_utf8mb3' ')), `DonorFirstName`,_utf8mb3' ',`DonorLastName`)) VIRTUAL,
The '' after the _utf8mb3 is underlined, and hovering over it shows:
"Syntax error: extraneous input found - expected 'comma'"
Luckily, this is a brand new database, and I can simply drop and recreate the table, but not if I cannot figure out how to properly generate that column.
I'm obviously doing something wrong here. Is there a correct way to make this generated column properly work?
Your alter table command is incorrect. A valid command would look like this. Remove all _utf8mb3 and correct the if and concat native functions.
ALTER TABLE tableName
`Heading` varchar(509) CHARACTER SET utf8 COLLATE utf8_general_ci GENERATED ALWAYS AS (
concat(
if( ((not `UseTitle`) or IsNull(`Title`)),
'',
concat(`Title`,' ', `DonorFirstName`, ' ',`DonorLastName`)
)
)
) VIRTUAL,
PS: concat(value1, value2) will return null if any value within the function is null.
concat_ws('', value1, value2) will ignore nulls and add all non null values.
Thanks to those who answered or commented.
If I make the changes in the SQL editor, it works just fine. The changes are accepted as expected. Still, I get a hard error trying to view the table definition in MySQL Workbench. So, the problem seems to be there instead. I will re-post the question differently to solve that issue.

SSRS [BC30277] - Character type '&' does not coincide declared type 'String'

I have this expression in Reporting Services 16, which return an error as follows:
="--> " & SUM(Fields!entrace_money_value.Value)
& Microsoft.VisualBasic.Constants.vbcrlf
& "<-- " & SUM(Fields!exit_money_value.Value)
[rsCompilerErrorInExpression] The Value expression for the textrun
‘Textbox8.Paragraphs[0].TextRuns[0]’ contains an error: [BC30277] Znak
typu & neodpovídá deklarovanému datovému typu String. --in Czech
Any help would be appreciated.
I had a very similar looking expression and got the same error code, though the error message mentioned Object instead of String:
[BC30277] Type character '&' does not match declared data type 'Object'
The issue for me wasn't that I was combining strings and integers (which works fine for me, without manual conversion as in the other answer). The issue was that the expression editor does not see line breaks as whitespace.
"-->"
& Fields!test.Value
& "<--"
In the example above the expression is read as a single line:
"-->" & Fields!test.Value& "<--"
This fails because Fields!test.Value& is invalid syntax.
Adding space characters to the beginning of the next lines (because they are easy to miss at the end of the line) fixes this issue:
"-->"
& Fields!test.Value
& "<--"
I can't read Czech but I've made this mistake enough times to know the problem.
Try it this way:
"--> " + CStr(SUM(Fields!entrace_money_value.Value))
SSRS is complaining that you are trying to add together int and a string and it doesn't know what to do. Cast your ints to a string using the CStr function. I also use the '+' operator for these kinds of operations...

VB.Net error to restore picture path using MySQL AES_ENCRYPT / AES_DECRYPT

this my first question I hope post it in the right format.
I using MySQL AES_ENCRYPT / AES_DECRYPT from vb.Net to store and get users picture and files location. All works fine even the special spanish characters, except when I get the paths.
e.g
saved path = C:\Users\User\Pictures and get = C:UsersUserPictures
I try with many codes also search on MySQL documentation without success still skip the backslash.
Please if can give me a orientation to fix this.
Dim MySQLQuery As String = "INSERT INTO `Agents` (`User_Name`, `User_Pic`)
VALUES (AES_ENCRYPT('" & txtUserName.Text & "', '" & MyPass & "'),
AES_ENCRYPT('" & txtUserPic.Text & "', '" & MyPass & "'"
MySQLQuery = "SELECT AES_DECRYPT(`User_Name`, '" & MyPass & "')
AS UName, AES_DECRYPT(`User_Pic`, '" & MyPass & "') AS UPic
FROM `Agents`"
MsgBox(MySQLReader.GetString("UName") & vbCrLf &
MySQLReader.GetString("UPic")
Even I try to use other data types on MySQL as VARBINARY, VARCHAR, TINYTEXT with and without UTF8, LATIN1 and other and get allways the same, skip the backslash.
In AES_ENCRYPT the data entered is treated as binary string with as escape character the . So when you encrypt \ and decrypt it again, you get an empty string as result. To preserver the \, you will have to pre-parse your string and replace the \ with \\. This will preserve your \ in the decrypt (however the decrypted string will show only one \).
Example:
SELECT AES_DECRYPT(AES_ENCRYPT("s\ad",'test'),'test');
Returns: sad (so not a nice return).
SELECT AES_DECRYPT(AES_ENCRYPT(REPLACE("s\ad","\","\\"),'test'),'test');
Returns: s\ad
Still very sad.
In the end: 100% mysql related, 0% vb related.

query with IIF statement

I am responsible for producing a set of name badges for an upcoming class reunion. Have everything set up and have been able to produce the correct output for those attending the reunion.
What I want to do is place parenthesis around the maiden names of the married women attending the reunion. Right now the maiden name is being displayed on the badge, but without parenthesis.
I have placed the following expression in the MaidenName field of the query I have written, but nothing is happening, at least this expression did not produce any error messages when I ran it.
IIf([MaidenName]="IsNull",[MaidenName]=" ",([MaidenName]=("("+[MaidenName]+")")))
When I entered the expression the first time the IsNull was without quotes. When I ran the
the query the quotes were place around the IsNull statement. The query ran, but there were no parenthesis around the maiden name on the output.
You would probably like to use the IsNull() function, and clean a little bit that Iif syntax:
IIf(IsNull([MaidenName]), " ", "(" & [MaidenName] & ")")
I recommend using the SQL-native is null comparison instead of the IsNull() function. A SQL-native way will always be faster and more portable than a VBA function.
iif(MaidenName is null, '', ' (' & MaidenName & ')')
Plus I think it's easier to read.

What is the correct syntax for SQL Statement using MySQL

In access i used this and its works fine:
(tblReservations_Dates.Date) Between #" & dteBegDate & "# And #" & dteEndDate & "#
Using MySQL I used this:
(tblReservations_Dates.Date) Between '" & dteBegDate & "' And '" & dteEndDate & "'
However the data is not displayed I was just wondering if this is the correct syntax for SQL Statement for comparing dates?
You don't need the brackets around the table.column reference
You need to make sure the query is submitting the start & end values in the proper datattype - date in this case.
Use STR_TO_DATE to safely convert whatever you're providing into a date datatype for MySQL. If you provide the date format, I'd be happy to update my answer with an example.
You need to make certain that the date strings (dteBegDate and dteEndDate) are in the format expected by MySQL, which would be YYYY-MM-DD HH:MM:SS. So, right now would be
2009-11-25 21:45:52
(in Pacific time). Other than that, it should work fine.