简体   繁体   English

Excel VBA读取单元格中的SQL SELECT语句

[英]excel vba read SQL SELECT statement in cell

I have an SQL SELECT query in a cell (image) . 我在单元格(图像)中有一个SQL SELECT查询。 I am attempting to use VBA to use the string in my cell as my string for an SQL query, but it reads only part of the string. 我正在尝试使用VBA将单元格中的字符串用作SQL查询的字符串,但它仅读取字符串的一部分。 In the cell, the query (without quotes) is 685 chars long. 在单元格中,查询(不带引号)的长度为685个字符。 In the variable (at run time), the query is (with quotes) 185 chars long. 在变量中(在运行时),查询长度(带引号)为185个字符。 Here is the query in the cell: 这是单元格中的查询:

SELECT Contacts.[Last Name], 
       Contacts.[First Name], 
       Contacts.[Maricom E-Mail], 
       Contacts.[Project, Org, Team] 
FROM Contacts 
WHERE (((Contacts.[Project, Org, Team])=""Proj - MDM"") 
       AND ((Contacts.[Active?])=Yes)) 
    OR (((Contacts.[Project, Org, Team])=""Team%"") 
       AND ((Contacts.[Active?])=Yes)) 
    OR (((Contacts.[Project, Org, Team])=""Orgs - CM"") 
       AND ((Contacts.[Active?])=Yes)) 
    OR (((Contacts.[Project, Org, Team])=""Orgs - Document Team"") 
       AND ((Contacts.[Active?])=Yes)) 
    OR (((Contacts.[Project, Org, Team])=""Orgs - Process Engineering"") 
       AND ((Contacts.[Active?])=Yes)) 
    OR (((Contacts.[Project, Org, Team])=""Orgs - QA"") 
       AND ((Contacts.[Active?])=Yes)) 
ORDER BY Contacts.[Last Name];

Here is the code I use to read the cell (I used "dim myQuery as string" earlier in the code, and the cell is formatted as text): 这是我用来读取单元格的代码(我在代码前面使用“将myQuery as string变暗”,并且该单元格设置为文本格式):

myQuery = Range("Project1Query").Value

Here is the query in the variable that has been declared as a string: 这是已声明为字符串的变量中的查询:

"SELECT Contacts.[Last Name], 
        Contacts.[First Name], 
        Contacts.[Maricom E-Mail], 
        Contacts.[Project, Org, Team] 
FROM Contacts 
WHERE (((Contacts.[Project, Org, Team])=""Proj - MDM"") AND "

The variable is then used in the following request for data: 然后在以下数据请求中使用该变量:

ReviewersData.Open myQuery, ReviewersConn, adOpenStatic, adLockOptimistic

Any thoughts as to how 500 characters got cut off? 关于如何截断500个字符的任何想法?

在获取Range()。Value之前,尝试将工作表中的双引号替换为Chr(34)。

I found out that the way I was attempting to access the variable value was causing it to appear truncated. 我发现尝试访问变量值的方式导致它看起来被截断。 I was right-clicking on the variable value in the "locals" frame of the VB IDE, selecting copy, then pasting what was in the clipboard into notepad. 我在VB IDE的“本地”框中右键单击变量值,选择复制,然后将剪贴板中的内容粘贴到记事本中。 It was there that the string was truncated. 在那里字符串被截断了。 When I did a msgbox of the text, it was correct. 当我输入文字的msgbox时,它是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM