首页 > 解决方案 > 变量在没有被告知的情况下表示开头的引用

问题描述

我试图从 SQL 中提取一个显示“收件箱”的值。

Function MailboxInbox(ActiveEmail As String, ActiveEmailInbox As String, ValidMailbox, conn As ADODB.Connection, rs As ADODB.Recordset, sConnString As String)
Call OpenSQLCon.ConnectSqlServer(conn, rs, sConnString)
Dim SQL As String
SQL = "Select distinct coalesce(Mailbox_1,Mailbox_2,Mailbox_3) from [MAP].[Config].[TB_SU_MAP_Mailbox_Configuration] where Mailbox_Name ='" & ActiveEmail & "'"
Debug.Print SQL
' Open the connection and execute.
conn.Open sConnString
Set rs = conn.Execute(SQL)
    If rs.EOF = True Or rs.BOF = True Then
        ValidMailbox = 1
        Exit Function
    Else
        ActiveEmailInbox = rs(0)
        Debug.Print ActiveEmailInbox
    End If

'Close SQL
Call CloseSQLCon.CSC(conn, rs, sConnString)
End Function

我正在连接到 SQL 服务器,发送查询并拉回我放入记录集中的单个值。在 SQL 表中,值为 Inbox,五个字符,没有"。

但是,当我使用记录集中的值填充变量“ActiveEmailInbox”时,它将其设置为“收件箱

当我稍后在代码中使用此值时,我无法将邮箱设置为“收件箱”,因为它设置为“收件箱”。当我将值 ActiveEmailInbox 打印到调试窗口时,它显示为收件箱。

带有变量的代码设置为“收件箱”
带有变量的代码设置为

打印到调试窗口的变量值,显示时没有“
打印到调试窗口的变量值,显示时没有

标签: excelvba

解决方案


虽然这可能是解决症状而不是实际问题,但您可以尝试在返回字符串时从字符串中删除第一个字符,这意味着 " 被删除。

   ActiveEmailInbox = Right(ActiveEmailInbox , Len(ActiveEmailInbox) - 1)

推荐阅读