1

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

有什么方法可以编写一个传递查询,它将在 where 条件下使用表单值。

例如:INSERT INTO TBL.ABC SELECT * FROM TBL.DEF where [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
4

1 回答 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)引用第二列。

于 2014-03-18T01:31:35.833 回答