Mightyginger
Programmer
I have a database which stores hourly data. The field "Date" is the index field for the database but is a Double. It stores the date as a double where the integer part is the excel serial for todays date and the decimal part is the hour. So today would be 39580 and the decimal .17 for the hour. So the entry in the field would be 39580.17. So in the database I have the following;
Date Value 1 Value 2
39580.15 101.1 102.1
39580.16 101.2 102.1
39580.17 101.3 102.0
What I want to do is query the database and grab all data for the nth hour of the day. So for example 06 hour. I have tried to do this using an SQL statment as follows
strSQL = "SELECT * FROM " & strTableName & " WHERE (Date-Rnd(Date))=0.06;"
It returns no data however and when I try creating a query in Access to play around with it I get "Data type mismatch in criteria expression". I think it may because Date isa double then I round that and it gives me an Integer. I'm a little stuck to be honest. Maybe I should try to change the type for my Date from Double to actual Date and then I beleive this is a lot easier to achieve. Sorry for the huge post but I'm somewhat stuck as to what to do now.
Sub dumpdata()
Dim strWorksheet As Worksheet
Dim thisWorkbook As Workbook
Dim strTableName As String
Dim strTypeName As String
Dim strPeriodName As String
Dim strDbSource As String
Dim strTempTblName As String
Dim i As Integer
Dim j As Integer
Dim dtDate As Double
Dim rs As Recordset
Dim strSQL As String
strDbSource = "L:\DB2.mdb"
Set db = OpenDatabase(strDbSource)
Set thisWorkbook = ActiveWorkbook
Dim x As Integer
Dim y As Integer
x = 6
y = 2
Do Until Sheet12.Cells(x, y) = ""
strTableName = Sheet12.Cells(x, y)
strSQL = "SELECT * FROM " & strTableName & " WHERE (Date-Rnd(Date))=0.06;"
thisWorkbook.Sheets.Add
ActiveSheet.Name = strTableName
ActiveSheet.Range("A2:IH1000").Clear
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
With ActiveSheet.Range("a1")
i = 0
For Each dbField In rs.Fields
.Offset(0, i) = dbField.Name
i = i + 1
Next dbField
End With
'Copy data
ActiveSheet.Range("a2").CopyFromRecordset rs
ActiveSheet.Select
ActiveSheet.Range("A1").Select
Selection.Sort Key1:=Range("A1"), Order1:=xlDescending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
rs.Close
Set rs = Nothing
x = x + 1
Loop
db.Close
Set db = Nothing
End Sub
Date Value 1 Value 2
39580.15 101.1 102.1
39580.16 101.2 102.1
39580.17 101.3 102.0
What I want to do is query the database and grab all data for the nth hour of the day. So for example 06 hour. I have tried to do this using an SQL statment as follows
strSQL = "SELECT * FROM " & strTableName & " WHERE (Date-Rnd(Date))=0.06;"
It returns no data however and when I try creating a query in Access to play around with it I get "Data type mismatch in criteria expression". I think it may because Date isa double then I round that and it gives me an Integer. I'm a little stuck to be honest. Maybe I should try to change the type for my Date from Double to actual Date and then I beleive this is a lot easier to achieve. Sorry for the huge post but I'm somewhat stuck as to what to do now.
Sub dumpdata()
Dim strWorksheet As Worksheet
Dim thisWorkbook As Workbook
Dim strTableName As String
Dim strTypeName As String
Dim strPeriodName As String
Dim strDbSource As String
Dim strTempTblName As String
Dim i As Integer
Dim j As Integer
Dim dtDate As Double
Dim rs As Recordset
Dim strSQL As String
strDbSource = "L:\DB2.mdb"
Set db = OpenDatabase(strDbSource)
Set thisWorkbook = ActiveWorkbook
Dim x As Integer
Dim y As Integer
x = 6
y = 2
Do Until Sheet12.Cells(x, y) = ""
strTableName = Sheet12.Cells(x, y)
strSQL = "SELECT * FROM " & strTableName & " WHERE (Date-Rnd(Date))=0.06;"
thisWorkbook.Sheets.Add
ActiveSheet.Name = strTableName
ActiveSheet.Range("A2:IH1000").Clear
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
With ActiveSheet.Range("a1")
i = 0
For Each dbField In rs.Fields
.Offset(0, i) = dbField.Name
i = i + 1
Next dbField
End With
'Copy data
ActiveSheet.Range("a2").CopyFromRecordset rs
ActiveSheet.Select
ActiveSheet.Range("A1").Select
Selection.Sort Key1:=Range("A1"), Order1:=xlDescending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
rs.Close
Set rs = Nothing
x = x + 1
Loop
db.Close
Set db = Nothing
End Sub