Okay, I am unable to find an answer for this, so if it's already been answered, I apologize. Any help being nudged in the right direction here would be very appreciated.
What I am trying to do is run a series of SQL Queries looped based on a Variable.
The Variable in question is VN, and it's pulled from a Function. Below is the code that I'm using. This all works great when I am not looping. But I now need it to run through the code. The SQL Code is working here, I just need to figure out how to loop it, and I'm stumped because all examples that I've been able to find are only using 1 recordset, and I'm using 4. (That final RecordSet Step 4 is a saved query based on a Crosstab Query, which I couldn't figure out how to do through VBA. )
I have all the functions that I use. So this is the complete code so far. (I'm very new at this, so it's possible I'm doing something in an inefficient way).
What I am trying to do is run a series of SQL Queries looped based on a Variable.
The Variable in question is VN, and it's pulled from a Function. Below is the code that I'm using. This all works great when I am not looping. But I now need it to run through the code. The SQL Code is working here, I just need to figure out how to loop it, and I'm stumped because all examples that I've been able to find are only using 1 recordset, and I'm using 4. (That final RecordSet Step 4 is a saved query based on a Crosstab Query, which I couldn't figure out how to do through VBA. )
I have all the functions that I use. So this is the complete code so far. (I'm very new at this, so it's possible I'm doing something in an inefficient way).
Code:
Public Function GetGST() As String
Dim db As Database
Dim Lrs As DAO.Recordset
Dim LSQL As String
Dim LGST As String
'Open connection to current Access database
Set db = CurrentDb()
'Create SQL statement to retrieve value from GST table
LSQL = "select VAR_ID from SVNames"
Set Lrs = db.OpenRecordset(LSQL)
'Retrieve value if data is found
If Lrs.EOF = False Then
LGST = Lrs("Var_ID")
Else
LGST = "Not found"
End If
Lrs.Close
Set Lrs = Nothing
GetGST = LGST
End Function
Public Function GetDateRange()
TempVars("S_Date").Value = InputBox("Enter Starting Year:")
TempVars("E_Date").Value = InputBox("Enter Ending Year:")
End Function
Public Function CreateSVTable()
Dim myConnection As ADODB.Connection
Set myConnection = CurrentProject.Connection
Dim myRecordset As New ADODB.Recordset
Dim StrSQL As String
myRecordset.ActiveConnection = myConnection
Dim Step1 As String
Dim Step2 As String
Dim Step3 As String
Dim Step4 As String
Dim VN As String
Dim SDate As Integer
Dim EDate As Integer
Dim D_Count As Integer
VN = GetGST()
SDate = TempVars("S_Date").Value
EDate = TempVars("E_Date").Value
D_Count = (EDate - SDate) + 1
Debug.Print SDate
Debug.Print EDate
Debug.Print D_Count
Step1 = "SELECT [Temp].SCH_NUM INTO [1V Base Schools] " & _
"FROM (SELECT DISTINCT dbo_REPD.SCH_NUM as SCH_NUM, dbo_REPD.VAR_VALUE as VAR_VALUE, dbo_REPD.VAR_ID & ' ' & dbo_REPD.RPT_YR AS [Year/Variable] " & _
"FROM dbo_REPD " & _
"WHERE (((VAR_VALUE)<>'' And (VAR_VALUE)<>'0') AND ((VAR_ID)= " & VN & ") AND ((RPT_YR) Between '" & SDate & "' And '" & EDate & "' )) " & _
"ORDER BY SCH_NUM) AS [Temp] " & _
"GROUP BY [Temp].SCH_NUM " & _
"HAVING (((Count(*))=" & D_Count & "));"
Step2 = "SELECT DISTINCT dbo_REPD.SCH_NUM AS [School Number], dbo_REPD.VAR_VALUE AS [Variable Value], [VAR_ID] & ' ' & [dbo_REPD]![RPT_YR] AS [Year/Variable], dbo_SchoolStatus.RGN_CODE INTO [1V Data Table] " & _
"FROM (dbo_REPD INNER JOIN dbo_SchoolStatus ON dbo_REPD.SCH_NUM = dbo_SchoolStatus.SCH_NUM) INNER JOIN [1V Base Schools] ON dbo_REPD.SCH_NUM = [1V Base Schools].SCH_NUM " & _
"WHERE (((dbo_REPD.VAR_VALUE)<>'' And (dbo_REPD.VAR_VALUE)<>'0') AND ((dbo_REPD.VAR_ID)=" & VN & ") AND ((dbo_REPD.RPT_YR) Between '" & SDate & "' And '" & EDate & "') AND ((dbo_SchoolStatus.RPT_YR)='2010'));"
Step3 = "Alter TABLE [1V Data Table] " & _
"Alter COLUMN [Variable Value] NUMBER;"
Step4 = "Select * INTO [" & VN & "] " & _
"From [1V-5 Data for Export];"
DoCmd.DeleteObject acTable, "" & VN & ""
DoCmd.DeleteObject acTable, "1v Base Schools"
DoCmd.DeleteObject acTable, "1v Data Table"
myRecordset.Open Step1
myRecordset.Open Step2
myRecordset.Open Step3
myRecordset.Open Step4
End Function
Last edited: