双引号出现在Select SQL Statement中的String中

时间:2014-02-24 18:51:56

标签: sql-server-2008 access-vba

我已经向一些同事询问了删除双引号的问题,但无济于事我们没有运气。如果我只是在下面的实际SQL选择查询中放入'Q3','2013'和'B50931',这段代码可以完美运行。一旦我尝试通过字符串传递表单字段,就出现双引号,我得到运行时错误'3061':参数太少。预计3。 在第一个strQtr,我尝试了这个:

 strQtr = Mid([Qtr_ADD], 2, Len([Qtr_ADD]) - 2)

这让我知道strQtr =“” 其他人网络strYear =“2013”​​和strGL_BU =“B50931”

也许我偏离基础,但为了运行选择查询,这些必须是'Q3','2013'和'B50931'。 再次,任何输入将不胜感激。谢谢!

 Private Const cSQLConn = "DRIVER=SQL Server;Etc    
 Private Sub Image96_Click()

 Dim dbs As DAO.Recordset
 Dim strRecSetSubQuery, strRecSetMaster As String
  Set DB = CurrentDb

 strQtr = Mid([Qtr_ADD], 2, Len([Qtr_ADD]) - 2)
 strYear = [Year_ADD]
 strGL_BU = [BU_Selected_Add]

 strRecSetQuery = ""
 strRecSetQuery = strRecSetQuery + "SELECT  "
 strRecSetQuery = strRecSetQuery + "       [QTR] , "
 strRecSetQuery = strRecSetQuery + "       [Year] , "
 strRecSetQuery = strRecSetQuery + "       [GL_BU]  "
 strRecSetQuery = strRecSetQuery + "FROM   dbo_tTbl_Admin_RxRebate"
 strRecSetQuery = strRecSetQuery + " WHERE  ( [QTR] = strQtr ) "
 strRecSetQuery = strRecSetQuery + "       AND ( [Year] = strYear ) "
 strRecSetQuery = strRecSetQuery + "       AND ( [GL_BU] = StrGL_BU ) "


  Debug.Print strRecSetQuery
    Set dbs = DB.OpenRecordset(strRecSetQuery, dbOpenDynaset)
     If dbs.RecordCount = 1 Then
     MsgBox "This Business Unit already exists within the database." & Chr(13)      & "Please make sure to enter a Non-DS related Business" & Chr(13) & "Unit and resubmit?",      vbExclamation, "Business Unit Check"
   Else
     End If
 'dbs.Close
 'Set dbs = Nothing
 End Sub

1 个答案:

答案 0 :(得分:1)

我认为这是你想要的SQL格式 - 假设Quarter,year和GL_BU都是字符串,否则删除单引号:它产生以下内容:

SELECT  [QTR], [Year], [GL_BU]  
FROM dbo_tTbl_Admin_RxRebate 
WHERE ([QTR] = 'Q3') AND ([Year] = '2013') AND ([GL_BU] = 'B50931') 


 strRecSetQuery = ""
 strRecSetQuery = strRecSetQuery + "SELECT  "
 strRecSetQuery = strRecSetQuery + "[QTR], "
 strRecSetQuery = strRecSetQuery + "[Year], "
 strRecSetQuery = strRecSetQuery + "[GL_BU]  "
 strRecSetQuery = strRecSetQuery + "FROM dbo_tTbl_Admin_RxRebate"
 strRecSetQuery = strRecSetQuery & " WHERE ([QTR] = '" & strQtr & "') "
 strRecSetQuery = strRecSetQuery + "AND ([Year] = '" & strYear & "') "
 strRecSetQuery = strRecSetQuery + "AND ([GL_BU] = '" & strGL_BU & "');"