在VBA中串联字符串 [英] Concatenating strings in VBA
问题描述
我正在维护用Microsoft Access和VBA编写的应用程序.
I'm maintaining an application written in Microsoft Access with VBA.
我浏览了一下代码,发现我下意识地将字符串与加号(+)而不是与号连接在一起.自从我用VB6编码以来已经有几年了.这会引起任何问题吗?
I'm glancing over my code and have just noticed I have subconsciously been concatenating strings together with the plus (+) symbol instead of the ampersand. It's been a few years since I've coded in VB6. Could this cause any issues?
一切似乎都很好,只需几分钟即可解决,我只是好奇我在技术上是否做错了什么.
Everything seems fine and it will only take a few minutes to fix, I'm just curious as to whether I'm technically doing anything wrong.
推荐答案
&"号是字符串操作,而加号则被重载:
The ampersand is explicitly a string operation, while the plus is overloaded:
Dim num1 As Integer
num1 = RandomNumberBetween(1, 9)
Dim num2 As Integer
num2 = RandomNumberBetween(1, 9)
Dim randomAge As String 'trying to get a random age between 11 and 99
' works
randomDate = "Your age is " & num1 & num2
'broken
randomDate = "Your age is " + num1 + num2
与数字一起使用时,将添加加号.
When used with numbers the plus sign will add.
这篇关于在VBA中串联字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!