I am currently have an USER INTERFACE (a Form) in Access which has two combo boxes referring to a specific date of a quarter. The form values are queried in from a pass through query from 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.
For eg : 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. 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.
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. 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
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"
Also, if you need a value from a combo column other than the bound column, use Me.DateCombo.Column(1)
or similar. Access uses 0-based indexes, so Me.DateCombo.Column(1)
refers to the second column.