仍在处理一个项目的查询,我的合作伙伴已经设法提出了一个漂亮的SQL语句,什么在运行时有效,但似乎无法在VBA中工作,它让我质疑支持多少SQL语句VBA
这是我的工作伙伴掀起的原始查询,在SQL中运行查询时效果很好
SELECT
crm_clients.`id`,
crm_clients.`national_insurance`,
crm_clients.`total_hmrc`,
(SELECT
crm_crmuseractions.title
FROM
dev_pfands.`crm_crmuseractions`
WHERE crm_crmuseractions.`id` = crm_clients.`status`) AS `status`
FROM
dev_pfands.`crm_clients`
INNER JOIN crm_client_cheques
ON crm_clients.id = crm_client_cheques.`client_id`
INNER JOIN dev_pfands.`crm_payments`
ON crm_clients.id = crm_payments.`client_id`
INNER JOIN dev_pfands.`crm_self_assesments`
ON crm_clients.id = crm_self_assesments.`client_id`
WHERE crm_clients.`status` = 9
OR crm_clients.`status` = 8
OR crm_clients.`status` = 7
OR crm_clients.`national_insurance` != ''
OR crm_clients.`id` != ''
我知道VBA喜欢SQL结构有点不同,所以我对它进行了调整,这可能是错误的,所以如果它因为我需要学习而随意烧掉它。
sql = "SELECT crm_clients.id, crm_clients.national_insurance, crm_clients.total_hmrc _
(SELECT _
crm_crmuseractions.title _
FROM _
crm_crmuseractions _
WHERE crm_crmuseractions.id = crm_clients.status ) AS 'status _
FROM _
crm_clients _
INNER JOIN crm_client_cheques _
ON crm_clients.id = crm_client_cheques.client_id _
INNER JOIN crm_payments _
ON crm_clients.id = crm_payments.client_id _
INNER JOIN crm_self_assesments.client_id _
WHERE crm_clients.status = 9 _
OR crm_clients.status = 8 _
OR crm_clients.status = 7 _
OR crm_clients.national_insurance != '' _
OR crm_clients.id != '' "
提前道歉,如果它的东西我错过了除了简单的选择或插入/删除和更新SQL的其他一些功能,如连接等似乎在VBA中对我不起作用
如果有人知道我哪里出错,那么这将是伟大的,如果我做得很好,其不支持的功能甚至解释为什么会很好,所以我可以把它与我的工作联系起来认为有效的朋友。
感谢高级人员。
答案 0 :(得分:2)
Dim query As String
query = "SELECT " & _
"crm_clients.id, " & _
"crm_clients.national_insurance, " & _
"crm_clients.total_hmrc, " & _
"(SELECT " & _
" crm_crmuseractions.Title " & _
"FROM " & _
" dev_pfands.crm_crmuseractions " & _
"WHERE crm_crmuseractions.`id` = crm_clients.status) AS 'status' " & _
"FROM " & _
"dev_pfands.crm_clients " & _
"INNER JOIN crm_client_cheques " & _
" ON crm_clients.id = crm_client_cheques.client_id " & _
"INNER JOIN dev_pfands.crm_payments " & _
" ON crm_clients.id = crm_payments.client_id " & _
"INNER JOIN dev_pfands.crm_self_assesments " & _
" ON crm_clients.id = crm_self_assesments.client_id " & _
"WHERE crm_clients.status = 9 " & _
"OR crm_clients.status = 8 " & _
"OR crm_clients.status = 7 " & _
"OR crm_clients.national_insurance != '' " & _
"OR crm_clients.id != ''"
答案 1 :(得分:2)
您需要跨多行连接数据字符串,如下所示:
strText = "This is the first line " & _
"This is the second line"
如果您拥有大量文本,则可能会遇到错误Too many line continuations
(因为最大续行数为25)。在这种情况下,您可以连接字符串,如下所示:
strText = "This is the first line"
strText = strText & "This is the second line"
注意强> 在编写SQL语句时,需要确保在正确的位置包含空格,即您最有可能需要在每行的末尾留一个空格。
答案 2 :(得分:1)
在没有单一刻度的情况下编写它并避免单引号,在VBA中使用双引号,总是。
类似这样的事情
Sql = "SELECT" & _
" crm_clients.""id""," & _
" crm_clients.""national_insurance"","