首页 文章

使用用户输入作为过滤条件的传递查询 - MS Access

提问于
浏览
1

我目前在Access中有一个用户界面(一个表格),它有两个组合框,指的是一个季度的特定日期 . 从SQL SERVER 2008的传递查询中查询表单值 .

有没有什么方法可以编写传递查询,它将在where条件中使用表单值 .

例如:INSERT INTO TBL.ABC SELECT * FROM TBL.DEF其中[Date] = Formvalue

经过所有的研究,我甚至已经在Stackoverflow中发布了几个问题,但无法找到答案 . 这有可能吗?

这样做的主要动机是根据输入将数据分成两个不同的表作为“FormValue”的形式,然后根据日期执行不同的操作 .

如果您需要更多信息,请告诉我 . 任何帮助深表感谢!!

Private Sub Command13_Click()
Dim St001, St002 As String
Dim conn As ADODB.Connection
Dim strPath As String
Dim strDate As String
Set conn = CurrentProject.Connection

strPath = "ServerName"
'conn.Open = "ODBC;DRIVER=SQL Server;SERVER=" & strpath & ";
'             DATABASE=DB;UID=ABC;PWD=DEF;Trusted_Connection=No;"

'DoCmd.OpenQuery "003a Drop Curr_Qtr"
strDate = curQtr & ""
StrDate2 = prevQtr & ""

       ' If combo box is empty?
If strDate = "" Then
        MsgBox "The Curr Qtr Date value is Empty, Please select the date"
ElseIf StrDate2 = "" Then
        MsgBox "The Date Prev Qtr Date value is Empty, Please select the date"
Else
    ' Append values

DoCmd.OpenQuery "003a Drop Curr_Qtr"

'On Error Resume Next
St002 = "SELECT COLUMNS into TblB from TblA where ColA='" & strDate & "'
DoCmd.RunSQL St002

因为scuh,我在代码中遇到的所有表都是链接表 . 我尝试使用以下格式之一的代码格式,但它始终弹出相同的错误:

Dim St001, St002 As String
    Dim conn As ADODB.Connection
    Dim strPath As String
    Dim strDate As String
    Set conn = CurrentProject.Connection

    strPath = "ServerName"
    'conn.Open = "ODBC;DRIVER=SQL Server;SERVER=" & strpath & ";DATABASE=DBName;
    '       UID=Username;PWD=password;Trusted_Connection=No;"

    'DoCmd.OpenQuery "003a Drop table"
    strDate = curQtr & ""
    StrDate2 = prevQtr & ""


       ' If combo box is empty?
    If strDate = "" Then
            MsgBox "The Curr Date value is Empty, Please select the date"
    ElseIf StrDate2 = "" Then
            MsgBox "The Prev  Date value is Empty, Please select the date"
    Else
        ' Append values

    DoCmd.OpenQuery "003a truncate table"

    'conn.Open = "ODBC;DRIVER=SQL Server;SERVER=" & strPath & ";DATABASE=009;
    '       UID=GM_SA;PWD=gmsa;Trusted_Connection=No;"

    'On Error Resume Next

 St002 = "Insert Into [Tabl B] ([Tabl B].[ColA]" & _
 "Select [Tabl A].[Col A] from [tabl A].[Col A] where [Tabl A].[Col z]='" & strDate & "'"

 strCon = "ODBC;DRIVER=SQL Server;SERVER=" & strPath & ";DATABASE=DBName;UID=UserName;" _
    & "PWD=Password;Trusted_Connection=No"
    Set wksp = DBEngine(0)
    Set dabs = wksp.opendatabase("", False, False, strCon)
    dabs.Execute St002, dbSQLpassThrough

    End If
    End Sub

1 回答

  • 1

    在确保您引用 Microsoft ActiveX Data Objects 2.8 Library 之后,请尝试以下操作

    Dim adoConn As ADODB.Connection
    ...
    St002 = "Insert Into [Tabl B] ([ColA]) Select [Tabl A].[Col A] from [tabl A].[Col A] where [Tabl A].[Col z]='" & FORMAT(strDate,"yyyy-mm-dd") & "'"
    strCon = "ODBC;DRIVER=SQL Server;SERVER=" & strPath  & ";DATABASE=DBName;UID=UserName;PWD=Password;Trusted_Connection=No"
    adoConn.Open(strCon)
    adoConn.Execute St002
    

    在将查询直接传递给服务器时,使用ADO而不是DAO通常是更好的选择,它应该完全避免任何类似于“RUNTIME ERROR 3024 - 无法找到文件'H:\ TableName.Mdb”的错误 .

    此外,如果您需要来自绑定列以外的组合列的值,请使用 Me.DateCombo.Column(1) 或类似的 . Access使用从0开始的索引,因此 Me.DateCombo.Column(1) 指的是第二列 .

相关问题