我目前在Access中有一个用户界面(表格),其中有两个组合框,指的是一个季度的特定日期。从SQL SERVER 2008的传递查询中查询表单值。
我是否有办法编写传递查询,该查询将使用where where条件中的表单值。
例如: 插入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
答案 0 :(得分: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)
引用第二列。