SELECT MSysObjects.Name
FROM MSysObjects
WHERE (((MSysObjects.Name) Not Like "msys*") AND ((MSysObjects.Type)=1));
Sub Average()
Dim Table As String
Dim mindate As String
Dim maxdate As String
Dim ssql As String
Table = apollo
mindate = 11 / 6 / 2003
maxdate = 11 / 7 / 2003
ssql = "SELECT Avg([Value]) AS " & Table & "_AVG FROM " & Table & "WHERE (((" & Table & ".Date) Between #" & mindate & "# And #" & maxdate & "#));"
End Sub
Sub Average()
Dim dbs As Database
Dim rs As Recordset
Dim qdf As QueryDef
Dim productName As String
Dim strSql As String
Set dbs = CurrentDb()
strSql = "SELECT Avg([Value]) AS Apollo_AVG FROM apollo WHERE (((apollo.Date) Between #11/6/2003# And #11/7/2003#));"
Set rs = dbs.OpenRecordset(strSql, dbOpenSnapshot)
With dbs
Set qdf = .CreateQueryDef("tmpProductInfo", strSql)
DoCmd.OpenQuery "tmpProductInfo"
.QueryDefs.Delete "tmpProductInfo"
End With
dbs.Close
qdf.Close
End Sub
I have no clue what is wrong hereSub Average()
Dim dbs As Database
Dim rs As Recordset
Dim qdf As QueryDef
Dim productName As String
Dim strSql As String
Dim Table As String
Dim mindate As String
Dim maxdate As String
Set dbs = CurrentDb()
Table = apollo
mindate = 11 / 6 / 2003
maxdate = 11 / 7 / 2003
strSql = "SELECT Avg([Value]) AS " & Table & "_AVG FROM " & Table & " WHERE (((" & Table & ".Date) Between #" & mindate & "# And #" & maxdate & "#));"
Set rs = dbs.OpenRecordset(strSql, dbOpenSnapshot)
With dbs
Set qdf = .CreateQueryDef("tmpAvgInfodynamic", strSql)
DoCmd.OpenQuery "tmpAvgInfodynamic"
'.QueryDefs.Delete "tmpAvgInfodynamic"
End With
dbs.Close
qdf.Close
End Sub
strSql = "SELECT Avg([Value]) AS " & Table & "_AVG FROM " & Table & " WHERE (((" & Table & ".Date) Between #" & mindate & "# And #" & maxdate & "#));"
Still getting an error.............i'll make a few tries with the msgbox to see what i can find out hereSub Average_dynamic()
Dim dbs As Database
Dim rs As Recordset
Dim qdf As QueryDef
Dim productName As String
Dim strSql As String
Dim Table As String
Dim mindate As Date
Dim maxdate As Date
Set dbs = CurrentDb()
Table = apollo
mindate = DateValue("11/06/2003")
maxdate = DateValue("11/07/2003")
strSql = "SELECT Avg([Value]) AS " & Table & "_AVG FROM " & Table & "WHERE (((" & Table & ".Date) Between #" & mindate & "# And #" & maxdate & "#));"
Set rs = dbs.OpenRecordset(strSql, dbOpenSnapshot)
With dbs
Set qdf = .CreateQueryDef("tmpAvgInfodynamic", strSql)
DoCmd.OpenQuery "tmpAvgInfodynamic"
'.QueryDefs.Delete "tmpAvgInfodynamic"
End With
dbs.Close
qdf.Close
End Sub
And the dynamic is acting strangestrSql = "SELECT Avg([Value]) AS Apollo_AVG FROM apollo WHERE (((apollo.Date) Between #11/6/2003# And #11/7/2003#));"
And it works. The problem seems to be after the BetweenstrSql = "SELECT Avg([Value]) AS " & Table & "_AVG FROM " & Table & " WHERE (((" & Table & ".Date) Between #11/6/2003# And #11/7/2003#));"
The data is being passed correctly. I checked with the MsgBoxSub Average_dynamic_loop()
Dim dbs As Database
Dim rs As Recordset
Dim qdf As QueryDef
Dim strSql As String
Dim mindate As String
Dim maxdate As String
Dim nFiles As Integer
Dim FileName As String
ReDim Files(1 To 10) As String
Const Path As String = "G:\xTemp\txt.NET\"
Set dbs = CurrentDb()
FileName = Dir(Path & "*.*")
mindate = "09/29/2006"
maxdate = "10/01/2006"
Do While (Len(FileName) > 0)
nFiles = nFiles + 1
' If array is full...
If nFiles = UBound(Files) Then
' Make room for 10 more files
ReDim Preserve Files(1 To nFiles + 10)
End If
Files(nFiles) = FileName
'If no more files exist exit loop
If FileName = "" Then Exit Do
'Make FileName without .txt extension
FileName = Left(FileName, InStr(1, FileName, ".") - 1)
'Make The SQL query
strSql = "SELECT Avg([Value]) AS [" & FileName & "_AVG] FROM [" & FileName & "] WHERE ((([" & FileName & "].Date) Between #" & mindate & "# And #" & maxdate & "#));"
'a = MsgBox(FileName, 1, "a")
Set rs = dbs.OpenRecordset(strSql, dbOpenSnapshot)
With dbs
Set qdf = .CreateQueryDef(FileName & "AVG", strSql)
DoCmd.OpenQuery FileName & "AVG"
'Close the query window
DoCmd.Close acQuery, FileName & "AVG"
End With
'Get next file
FileName = Dir
Loop
dbs.Close
qdf.Close
End Sub
But i still dont know how to call it in my codeFunction Transposer(strSource As String, strTarget As String)
Dim db As DAO.Database
Dim tdfNewDef As DAO.TableDef
Dim fldNewField As DAO.Field
Dim rstSource As DAO.Recordset, rstTarget As DAO.Recordset
Dim i As Integer, j As Integer
On Error GoTo Transposer_Err
Set db = CurrentDb()
Set rstSource = db.OpenRecordset(strSource)
rstSource.MoveLast
' Create a new table to hold the transposed data.
' Create a field for each record in the original table.
Set tdfNewDef = db.CreateTableDef(strTarget)
For i = 0 To rstSource.RecordCount
Set fldNewField = tdfNewDef.CreateField(CStr(i + 1), dbText)
tdfNewDef.Fields.Append fldNewField
Next i
db.TableDefs.Append tdfNewDef
' Open the new table and fill the first field with
' field names from the original table.
Set rstTarget = db.OpenRecordset(strTarget)
For i = 0 To rstSource.Fields.Count - 1
With rstTarget
.AddNew
.Fields(0) = rstSource.Fields(i).Name
.Update
End With
Next i
rstSource.MoveFirst
rstTarget.MoveFirst
' Fill each column of the new table
' with a record from the original table.
For j = 0 To rstSource.Fields.Count - 1
' Begin with the second field, because the first field
' already contains the field names.
For i = 1 To rstTarget.Fields.Count - 1
With rstTarget
.Edit
.Fields(i) = rstSource.Fields(j)
rstSource.MoveNext
.Update
End With
Next i
rstSource.MoveFirst
rstTarget.MoveNext
Next j
db.Close
Exit Function
Transposer_Err:
Select Case Err
Case 3010
MsgBox "The table " & strTarget & " already exists."
Case 3078
MsgBox "The table " & strSource & " doesn't exist."
Case Else
MsgBox CStr(Err) & " " & Err.Description
End Select
Exit Function
End Function