简体   繁体   English

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

[英]Pass-through query with user input as filter criteria - MS Access

I am currently have an USER INTERFACE (a Form) in Access which has two combo boxes referring to a specific date of a quarter.我目前在 Access 中有一个用户界面(一个表单),它有两个组合框,指的是一个季度的特定日期。 The form values are queried in from a pass through query from SQL SERVER 2008.从 SQL SERVER 2008 的传递查询中查询表单值。

Is there any way in which I can write a pass through query which will use the form values in where condition.有什么方法可以编写一个传递查询,该查询将使用 where 条件中的表单值。

For eg : INSERT INTO TBL.ABC SELECT * FROM TBL.DEF where [Date]=Formvalue例如:INSERT INTO TBL.ABC SELECT * FROM TBL.DEF where [Date]=Formvalue

After all of the research and I even have gone through couple of questions posted in Stackoverflow, but couldnt find the answer.经过所有的研究,我什至经历了 Stackoverflow 上发布的几个问题,但找不到答案。 Is this even possible ??这甚至可能吗?

The main motive behind doing this is to segregate the data into two different tables based on the input as a form of "FormValue" and then perform different operations based on the dates.这样做的主要动机是根据作为“FormValue”形式的输入将数据分离到两个不同的表中,然后根据日期执行不同的操作。

Please do let me kno if you need further information.如果您需要更多信息,请告诉我。 Any help is much appreciated!!任何帮助深表感谢!!

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

As scuh, all the tables that I am reffering to in the code are linked tables.作为 scuh,我在代码中引用的所有表都是链接表。 I tried using the below format of the code as suggested in one of the form, but it is the same error that pops up all the time :我尝试使用以下格式的代码作为其中一种形式的建议,但始终弹出相同的错误:

    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

Try the following, after making sure you have a reference to Microsoft ActiveX Data Objects 2.8 Library在确保您有对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

Using ADO instead of DAO is often a better option when passing queries directly to servers, it should completely bypass any possibility of errors similar to "RUNTIME ERROR 3024 - Could not find file 'H:\\TableName.Mdb"将查询直接传递给服务器时,使用 ADO 而不是 DAO 通常是更好的选择,它应该完全绕过类似于“运行时间错误 3024 - 找不到文件 'H:\\TableName.Mdb”的任何可能性的错误

Also, if you need a value from a combo column other than the bound column, use Me.DateCombo.Column(1) or similar.此外,如果您需要来自绑定列以外的组合列的值,请使用Me.DateCombo.Column(1)或类似的。 Access uses 0-based indexes, so Me.DateCombo.Column(1) refers to the second column. Access 使用基于 0 的索引,因此Me.DateCombo.Column(1)指的是第二列。

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

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