Conditional Cumulative - ms-access

I want to have a running sum in my MS ACCESS report, but with a condition.
The condition is IF (Gap>1,0, Cumulative minutes in Previous Row + Minutes in Current Row)
Is there any way to achieve this? I would appreciate any insights or solutions. Thanks

Option Compare Database
Option Explicit
Private lngTotal As Long
Public Function lngConditional_Cummulative(ByVal lngID As Long, _
ByVal lngNumber As Long) As Long
If lngID = 1& Or _
lngNumber = 0& Then
lngTotal = 0&
End If ' If lngID = 1& Or lngNumber = 0& Then
lngTotal = lngTotal + lngNumber
lngConditional_Cummulative = lngTotal
End Function

Related

Access cannot close after 100s of function calls - Recordset sees to be "open" although closed

I need to calculate roughly 100 medians in an access database and have a function (see code below)
After calling this many times with the code
Nz(DMedian("Price", "Qry91_Cad_by", "[Cad ID]='" & rst![Cad ID] & "'"), 0)
I cannot quit Access any more and need to kill it with the task manager (as there are open connections)
When compacting the database, I get the error "You attemted to open a database that is already open by user 'Admin' .....
You can close the database but Access itself needs to be killed
Any Ideas what's wrong?
...
Public Function DMedian( _
ByVal strField As String, ByVal strDomain As String, _
Optional ByVal strCriteria As String) As Variant
' Purpose:
' To calculate the median value
' for a field in a table or query.
' In:
' strField: the field.
' strDomain: the table or query.
' strCriteria: an optional WHERE clause to
' apply to the table or query.
' Out:
' Return value: the median, if successful;
' Otherwise, an Error value.
Dim db As DAO.Database
Dim rstDomain As DAO.Recordset
Dim strSQL As String
Dim varMedian As Variant
Dim intFieldType As Integer
Dim intRecords As Integer
Const errAppTypeError = 3169
'On Error GoTo HandleErr
Set db = CurrentDb()
' Initialize return value.
varMedian = Null
' Build SQL string for recordset.
strSQL = "SELECT " & strField & " FROM " & strDomain
' Only use a WHERE clause if one is passed in.
If Len(strCriteria) > 0 Then
strSQL = strSQL & " WHERE " & strCriteria
End If
strSQL = strSQL & " ORDER BY " & strField
Set rstDomain = db.OpenRecordset(strSQL, dbOpenSnapshot)
' Check the data type of the median field.
intFieldType = rstDomain.Fields(strField).Type
Select Case intFieldType
Case dbByte, dbInteger, dbLong, _
dbCurrency, dbSingle, dbDouble, dbDate
' Numeric field.
If Not rstDomain.EOF Then
rstDomain.MoveLast
intRecords = rstDomain.RecordCount
' Start from the first record.
rstDomain.MoveFirst
If (intRecords Mod 2) = 0 Then
' Even number of records.
' No middle record, so move to the
' record right before the middle.
rstDomain.Move ((intRecords \ 2) - 1)
varMedian = rstDomain.Fields(strField)
' Now move to the next record, the
' one right after the middle.
rstDomain.MoveNext
' And average the two values.
varMedian = _
(varMedian + rstDomain.Fields(strField)) / 2
' Make sure you return a date, even when
' averaging two dates.
If intFieldType = dbDate And Not IsNull(varMedian) Then
varMedian = CDate(varMedian)
End If
Else
' Odd number or records.
' Move to the middle record and return its value.
rstDomain.Move ((intRecords \ 2))
varMedian = rstDomain.Fields(strField)
End If
Else
' No records; return Null.
varMedian = Null
End If
Case Else
' Non-numeric field; so raise an app error.
Err.Raise errAppTypeError
End Select
DMedian = varMedian
ExitHere:
'On Error Resume Next
rstDomain.Close
Set rstDomain = Nothing
db.Close
Set db = Nothing
Exit Function
HandleErr:
' Return an error value.
DMedian = CVErr(Err.Number)
Resume ExitHere
End Function
...

Excel VBA QueryTable Parameter Fails with Null Value

Is it possible to pass a NULL value to QueryTable.Parameters for use in a (My)SQL query?
From this other answer, we can see that it's possible to do this with ADODB.Command, but unfortunately, ADODB is not available in Excel for Mac, and the application I'm developing should work on both Windows & Mac.
The below is confirmed to error with Windows (and I'd assume Mac).
The following VBA code works fine if you set param_value to anything but Null, but as soon as you try with a Null, it fails terribly.
Option Explicit
Sub Test()
' SQL '
Dim sql As String
sql = "SELECT ? AS `something`"
Dim param_value As Variant
'param_value = "hello" ' this works
'param_value = Null ' this does NOT work
' QUERY & TABLE CONFIG '
Dim my_dsn As String
Dim sheet_name As String
Dim sheet_range As Range
Dim table_name As String
my_dsn = "ODBC;DSN=my_dsn;"
sheet_name = "Sheet1"
Set sheet_range = Range("$A$1")
table_name = "test_table"
' EXECUTE QUERY '
Dim qt As QueryTable
Set qt = ActiveWorkbook.Worksheets(sheet_name).ListObjects.Add( _
SourceType:=xlSrcExternal, _
Source:=my_dsn, _
Destination:=sheet_range _
).QueryTable
With qt
.ListObject.Name = table_name
.ListObject.DisplayName = table_name
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.CommandText = sql
End With
Dim param As Parameter
Set param = qt.Parameters.Add( _
"param for something", _
xlParamTypeUnknown _
)
param.SetParam xlConstant, param_value
qt.Refresh BackgroundQuery:=False
End Sub
When setting param_value to "hello", the successful result looks like this:
(This bottom part with command prompt screenshot is what was recorded by MySQL's logging).
This is the error when setting param_value to Null:
You can see from the MySQL log that the successful query first does a Prepare, followed by an Execute of the query.
Whereas the failing, Null query does the Prepare, but never makes it to the Execute.
Searching online for run-time error -2147417848 (80010108) is no help; people report getting that for everything from "freeze pane" issues to "userform" issues, and I don't see anything about this related to QueryTable.
Not only does the VBA code fail to work as expected, it also corrupts the workbook in some way:
(This occurs when attempting to save the file after the failed query; close without saving and you can re-open).
The fact that the MySQL log is showing the VBA connection failing to Quit, and that the Excel file gets corrupted, makes me think that not only is it not possible to use Null in QueryTable.Parameters, but that it is also a bug in the underlying software.
Am I missing something, or is it impossible to pass a Null Parameter to a QueryTable?
Update
In response to close votes: my point is that there should be a way to pass a parameter as NULL, just as is referenced here.
Update
Due to this issue with Null, as well as xlParamTypeDate not being converted from a decimal to 'yyyy-mm-dd', I ended up rolling my own parameterizing class module. It has been posted below as an answer to this question.
If anyone knows how to accomplish this with QueryTable.Parameters, then post and I'll select your answer. But following is a custom solution.
For all SqlTypes except char, the parameterization is custom, but char still uses QueryTable.Parameters due to the various escaping corner cases that can occur when trying to implement that.
Edit to above strikethrough: I have actually reverted to also manually handling char params with this custom parameterization. I forget the exact corner case encountered, but the definitive conclusion reached was that the VBA parameterization was failing for a singular case of a specific char param with a specific query string... I have absolutely no idea where the point of failure was as it was generated within the black-box of Microsoft's VBA method, but I validated as a factual certainty that the string param was simply not getting passed to the (My)SQL engine for this one seemingly random case. Suffice it to say that my experience has been that the QueryTable.Parameters method can simply not be trusted at all. My recommendation is to uncomment the line of GetValueAsSqlString = Replace$(Replace$(Replace$(CStr(value), "\", "\\"), "'", "\'"), """", "\""") and to remove the IF char THEN logic within SetQueryTableSqlAndParams. Since different engines have different literal characters, I leave this as an exercise for the reader to handle in their circumstance; for example, the above Replace$() code may (or may not) have the behavior you desire to see with a VBA string containing \n.
One inconsistency I noticed with QueryTable is that if you execute a non-parameterized query of SELECT "hello\r\nthere" AS s, the query will return with a newline (as expected), but if you use QueryTable.Parameters xlParamTypeChar with "hello\r\nthere", then it will return with raw backslashes. So you must use vbCrLf, etc. when parameterizing string literals.
SqlParams class module:
Option Explicit
' https://web.archive.org/web/20180304004843/http://analystcave.com:80/vba-enum-using-enumerations-in-vba/#Enumerating_a_VBA_Enum '
Public Enum SqlTypes
[_First]
bool
char
num_integer
num_fractional
dt_date
dt_time
dt_datetime
[_Last]
End Enum
Private substitute_string As String
Private Const priv_sql_type_index As Integer = 0
Private Const priv_sql_val_index As Integer = 1
Private params As New collection
Private Sub Class_Initialize()
substitute_string = "?"
End Sub
Public Property Get SubstituteString() As String
' This is the string to place in the query '
' i.e. "SELECT * FROM users WHERE id = ?" '
SubstituteString = substitute_string
End Property
Public Property Let SubstituteString(ByVal s As String)
substitute_string = s
End Property
Public Sub SetQueryTableSqlAndParams( _
ByVal qt As QueryTable, _
ByVal sql As String _
)
Dim str_split As Variant
str_split = Split(sql, substitute_string)
Call Assert( _
(GetArrayLength(str_split) - 1) = params.Count, _
"Found " & (GetArrayLength(str_split) - 1) & ", but expected to find " & params.Count & " of '" & substitute_string & "' in '" & sql & "'" _
)
qt.Parameters.Delete
sql = str_split(0)
Dim param_n As Integer
For param_n = 1 To params.Count
If (GetSqlType(param_n) = SqlTypes.char) And Not IsNull(GetValue(param_n)) Then
sql = sql & "?"
With qt.Parameters.Add( _
param_n, _
xlParamTypeChar _
)
.SetParam xlConstant, GetValue(param_n)
End With
Else
sql = sql & GetValueAsSqlString(param_n)
End If
sql = sql & str_split(param_n)
Next param_n
qt.CommandText = sql
End Sub
Public Property Get Count() As Integer
Count = params.Count
End Property
Public Sub Add( _
ByVal sql_type As SqlTypes, _
ByVal value As Variant _
)
Dim val_array(1)
val_array(priv_sql_type_index) = sql_type
Call SetThisToThat(val_array(priv_sql_val_index), value)
params.Add val_array
End Sub
Public Function GetSqlType(ByVal index_n As Integer) As SqlTypes
GetSqlType = params.Item(index_n)(priv_sql_type_index)
End Function
Public Function GetValue(ByVal index_n As Integer) As Variant
Call SetThisToThat( _
GetValue, _
params.Item(index_n)(priv_sql_val_index) _
)
End Function
Public Sub Update( _
ByVal index_n As Integer, _
ByVal sql_type As SqlTypes, _
ByVal value As Variant _
)
Call SetSqlType(index_n, sql_type)
Call SetValue(index_n, value)
End Sub
Public Sub SetSqlType( _
ByVal index_n As Integer, _
ByVal sql_type As SqlTypes _
)
params.Item(index_n)(priv_sql_type_index) = sql_type
End Sub
Public Sub SetValue( _
ByVal index_n As Integer, _
ByVal value As Variant _
)
Call SetThisToThat( _
params.Item(index_n)(priv_sql_val_index), _
value _
)
End Sub
Public Function GetValueAsSqlString(index_n As Integer) As String
Dim value As Variant
Call SetThisToThat(value, GetValue(index_n))
If IsNull(value) Then
GetValueAsSqlString = "NULL"
Else
Dim sql_type As SqlTypes
sql_type = GetSqlType(index_n)
Select Case sql_type
Case SqlTypes.num_integer
GetValueAsSqlString = CStr(value)
Call Assert( _
StringIsInteger(GetValueAsSqlString), _
"Expected integer, but found " & GetValueAsSqlString, _
"GetValueAsSqlString" _
)
Case SqlTypes.num_fractional
GetValueAsSqlString = CStr(value)
Call Assert( _
StringIsFractional(GetValueAsSqlString), _
"Expected fractional, but found " & GetValueAsSqlString, _
"GetValueAsSqlString" _
)
Case SqlTypes.bool
If (value = True) Or (value = 1) Then
GetValueAsSqlString = "1"
ElseIf (value = False) Or (value = 0) Then
GetValueAsSqlString = "0"
Else
err.Raise 5, "GetValueAsSqlString", _
"Expected bool of True/False or 1/0, but found " & value
End If
Case Else
' Everything below will be wrapped in quotes as a string for SQL '
Select Case sql_type
Case SqlTypes.char
err.Raise 5, "GetValueAsSqlString", _
"Use 'QueryTable.Parameters.Add' for chars"
' GetValueAsSqlString = Replace$(Replace$(Replace$(CStr(value), "\", "\\"), "'", "\'"), """", "\""") ''
Case SqlTypes.dt_date
If VarType(value) = vbString Then
GetValueAsSqlString = value
Else
GetValueAsSqlString = Format(value, "yyyy-MM-dd")
End If
Call Assert( _
StringIsSqlDate(GetValueAsSqlString), _
"Expected date as yyyy-mm-dd , but found " & GetValueAsSqlString, _
"GetValueAsSqlString" _
)
Case SqlTypes.dt_datetime
If VarType(value) = vbString Then
GetValueAsSqlString = value
Else
GetValueAsSqlString = Format(value, "yyyy-MM-dd hh:mm:ss")
End If
Call Assert( _
StringIsSqlDatetime(GetValueAsSqlString), _
"Expected datetime as yyyy-mm-dd hh:mm:ss, but found " & GetValueAsSqlString, _
"GetValueAsSqlString" _
)
Case SqlTypes.dt_time
If VarType(value) = vbString Then
GetValueAsSqlString = value
Else
GetValueAsSqlString = Format(value, "hh:mm:ss")
End If
Call Assert( _
StringIsSqlTime(GetValueAsSqlString), _
"Expected time as hh:mm:ss, but found " & GetValueAsSqlString, _
"GetValueAsSqlString" _
)
Case Else
err.Raise 5, "GetValueAsSqlString", _
"SqlType of " & GetSqlType(index_n) & " has not been configured for escaping"
End Select
GetValueAsSqlString = "'" & GetValueAsSqlString & "'"
End Select
End If
End Function
Dependency Module:
Function GetArrayLength(ByVal a As Variant) As Integer
' https://stackoverflow.com/a/30574874 '
GetArrayLength = UBound(a) - LBound(a) + 1
End Function
Sub Assert( _
ByVal b As Boolean, _
ByVal msg As String, _
Optional ByVal src As String = "Assert" _
)
If Not b Then
err.Raise 5, src, msg
End If
End Sub
Sub SetThisToThat(ByRef this As Variant, ByVal that As Variant)
' Used if "that" can be an object or a primitive '
If IsObject(that) Then
Set this = that
Else
this = that
End If
End Sub
Function StringIsDigits(ByVal s As String) As Boolean
StringIsDigits = Len(s) And (s Like String(Len(s), "#"))
End Function
Function StringIsInteger(ByVal s As String) As Boolean
If Left$(s, 1) = "-" Then
StringIsInteger = StringIsDigits(Mid$(s, 2))
Else
StringIsInteger = StringIsDigits(s)
End If
End Function
Function StringIsFractional( _
ByVal s As String, _
Optional ByVal require_decimal As Boolean = False _
) As Boolean
' require_decimal means that the string must contain a "." decimal point '
Dim n As Integer
n = InStr(s, ".")
If n Then
StringIsFractional = StringIsInteger(Left$(s, n - 1)) And StringIsDigits(Mid$(s, n + 1))
ElseIf require_decimal Then
StringIsFractional = False
Else
StringIsFractional = StringIsInteger(s)
End If
End Function
Function StringIsDate(ByVal s As String) As Boolean
StringIsDate = True
On Error GoTo no
IsObject (DateValue(s))
Exit Function
no:
StringIsDate = False
End Function
Function StringIsSqlDate(ByVal s As String) As Boolean
StringIsSqlDate = StringIsDate(s) And ( _
(s Like "####-##-##") _
Or (s Like "####-#-##") _
Or (s Like "####-##-#") _
Or (s Like "####-#-#") _
)
End Function
Function StringIsTime(ByVal s As String) As Boolean
StringIsTime = True
On Error GoTo no
IsObject (TimeValue(s))
Exit Function
no:
StringIsTime = False
End Function
Function StringIsSqlTime(ByVal s As String) As Boolean
StringIsSqlTime = StringIsTime(s) And ( _
(s Like "##:##:##") _
Or (s Like "#:##:##") _
)
End Function
Function StringIsDatetime(ByVal s As String) As Boolean
Dim n As Integer
n = InStr(s, " ")
If n Then
StringIsDatetime = StringIsDate(Left$(s, n - 1)) And StringIsTime(Mid$(s, n + 1))
Else
StringIsDatetime = False
End If
End Function
Function StringIsSqlDatetime(ByVal s As String) As Boolean
Dim n As Integer
n = InStr(s, " ")
If n Then
StringIsSqlDatetime = StringIsSqlDate(Left$(s, n - 1)) And StringIsSqlTime(Mid$(s, n + 1))
Else
StringIsSqlDatetime = False
End If
End Function
Example Usage:
Dim params As SqlParams
Set params = New SqlParams
params.Add SqlTypes.num_integer, 123
Dim sql As String
sql = "SELECT * FROM users WHERE id = " & params.SubstituteString
Dim odbc_str As String
odbc_str = "ODBC;DSN=my_dsn;"
Dim sheet As Worksheet
Set sheet = ThisWorkbook.Worksheets("Sheet1")
Dim table_name As String
table_name = "test_table"
Dim qt As QueryTable
Set qt = sheet.ListObjects.Add( _
SourceType:=xlSrcExternal, _
Source:=odbc_str, _
Destination:=Range("$A$1") _
).QueryTable
With qt
.ListObject.name = table_name
.ListObject.DisplayName = table_name
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
End With
Call params.SetQueryTableSqlAndParams(qt, sql)
qt.Refresh BackgroundQuery:=False

Why is datediff interval "weekday" returning interval based on weeks and not days minus weekends?

I'm using the expression builder to give me a calculated value. It looks like this:
=DateDiff("w",Date(),[Latest_Call_Date])
However, this is giving me results as if it were doing a calculation based on weeks instead of days minus weekends . When I try the same function, but with the interval set to days (d) instead of weekdays (w) I get the expected results (but of course, including Saturdays and Sundays, which I do not wish to include in the calculation). So, for example, for everyday this week I'm getting 1,2,3,4,5 day differences but they all return zero with weekdays. That formula is:
=DateDiff("d",Date(),[Latest_Call_Date])
Is there something else I have to do to get "weekdays" interval to work?
I'm using Access 2013, in an .accdb file format.
Weekdays excluding weekends (and holidays) are usually labelled workdays.
DateDiff("w", ..) returns the count of a weekday between two dates, while
DateDiff("ww", ..) returns the difference in calendar weeks between two dates.
Counting workdays takes a little more. This function will do:
Public Function DateDiffWorkdays( _
ByVal datDate1 As Date, _
ByVal datDate2 As Date, _
Optional ByVal booWorkOnHolidays As Boolean) _
As Long
' Calculates the count of workdays between datDate1 and datDate2.
' 2014-10-03. Cactus Data ApS, CPH
Dim aHolidays() As Date
Dim lngDiff As Long
Dim lngSign As Long
Dim lngHoliday As Long
lngSign = Sgn(DateDiff("d", datDate1, datDate2))
If lngSign <> 0 Then
If booWorkOnHolidays = True Then
' Holidays are workdays.
Else
' Retrieve array with holidays between datDate1 and datDate2.
aHolidays = GetHolidays(datDate1, datDate2)
End If
Do Until DateDiff("d", datDate1, datDate2) = 0
Select Case Weekday(datDate1)
Case vbSaturday, vbSunday
' Skip weekend.
Case Else
' Check for holidays to skip.
' Ignore error when using LBound and UBound on an unassigned array.
On Error Resume Next
For lngHoliday = LBound(aHolidays) To UBound(aHolidays)
If Err.Number > 0 Then
' No holidays between datDate1 and datDate2.
ElseIf DateDiff("d", datDate1, aHolidays(lngHoliday)) = 0 Then
' This datDate1 hits a holiday.
' Subtract one day before adding one after the loop.
lngDiff = lngDiff - lngSign
Exit For
End If
Next
On Error GoTo 0
lngDiff = lngDiff + lngSign
End Select
datDate1 = DateAdd("d", lngSign, datDate1)
Loop
End If
DateDiffWorkdays = lngDiff
End Function
And the Holidays, should you need it some day:
Public Function GetHolidays( _
ByVal datDate1 As Date, _
ByVal datDate2 As Date, _
Optional ByVal booDesc As Boolean) _
As Date()
' Finds the count of holidays between datDate1 and datDate2.
' The holidays are returned as an array of dates.
' DAO objects are declared static to speed up repeated calls with identical date parameters.
' 2014-10-03. Cactus Data ApS, CPH
' The table that holds the holidays.
Const cstrTable As String = "tblHoliday"
' The field of the table that holds the dates of the holidays.
Const cstrField As String = "HolidayDate"
' Constants for the arrays.
Const clngDimRecordCount As Long = 2
Const clngDimFieldOne As Long = 0
Static dbs As DAO.Database
Static rst As DAO.Recordset
Static datDate1Last As Date
Static datDate2Last As Date
Dim adatDays() As Date
Dim avarDays As Variant
Dim strSQL As String
Dim strDate1 As String
Dim strDate2 As String
Dim strOrder As String
Dim lngDays As Long
If DateDiff("d", datDate1, datDate1Last) <> 0 Or DateDiff("d", datDate2, datDate2Last) <> 0 Then
' datDate1 or datDate2 has changed since the last call.
strDate1 = Format(datDate1, "\#yyyy\/mm\/dd\#")
strDate2 = Format(datDate2, "\#yyyy\/mm\/dd\#")
strOrder = Format(booDesc, "\A\s\c;\D\e\s\c")
strSQL = "Select " & cstrField & " From " & cstrTable & " " & _
"Where " & cstrField & " Between " & strDate1 & " And " & strDate2 & " " & _
"Order By 1 " & strOrder
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
' Save the current set of date parameters.
datDate1Last = datDate1
datDate2Last = datDate2
End If
lngDays = rst.RecordCount
If lngDays = 0 Then
' Leave adatDays() as an unassigned array.
Else
ReDim adatDays(lngDays - 1)
' As repeated calls may happen, do a movefirst.
rst.MoveFirst
avarDays = rst.GetRows(lngDays)
' rst is now positioned at the last record.
For lngDays = LBound(avarDays, clngDimRecordCount) To UBound(avarDays, clngDimRecordCount)
adatDays(lngDays) = avarDays(clngDimFieldOne, lngDays)
Next
End If
' DAO objects are static.
' Set rst = Nothing
' Set dbs = Nothing
GetHolidays = adatDays()
End Function

Loop Doesn't Terminate in Access VBA

I have a function called "CurveInterpolateRecordset", which is as follows:
Function CurveInterpolateRecordset(rsCurve As Recordset, InterpDate As Date) As Double
Dim I As Long
Dim x1 As Date, x2 As Date, y1 As Double, y2 As Double, x As Date
CurveInterpolateRecordset = Rnd()
If rsCurve.RecordCount <> 0 Then
I = 1
rsCurve.MoveFirst
x1 = CDate(rsCurve.Fields("MaturityDate"))
y1 = CDbl(rsCurve.Fields("ZeroRate"))
If InterpDate = CDate(rsCurve.Fields("MaturityDate")) Then CurveInterpolateRecordset = CDbl(rsCurve.Fields("ZeroRate")): Exit Function
'Do While Not rsCurve.EOF
rsCurve.MoveNext
Do While (CDate(rsCurve.Fields("MaturityDate")) <= InterpDate)
If rsCurve.EOF Then CurveInterpolateRecordset = CDbl(rsCurve.Fields("ZeroRate")): Exit Function
If InterpDate = CDate(rsCurve.Fields("MaturityDate")) Then CurveInterpolateRecordset = CDbl(rsCurve.Fields("ZeroRate")): Exit Function
If InterpDate > CDate(rsCurve.Fields("MaturityDate")) Then
x1 = CDate(rsCurve.Fields("MaturityDate"))
y1 = CDbl(rsCurve.Fields("ZeroRate"))
End If
rsCurve.MoveNext
If rsCurve.EOF Then CurveInterpolateRecordset = y1: Exit Function
Loop
x2 = CDate(rsCurve.Fields("MaturityDate"))
y2 = CDbl(rsCurve.Fields("ZeroRate"))
CurveInterpolateRecordset = y1 + (y2 - y1) * CDate((InterpDate - x1) / (x2 - x1))
End If
Debug.Print I, InterpDate, x1, x2, y1, y2
End Function
This loop will interpolate a missing value for a specific date by interpolating using the values for the nearest dates.
I have a table of dates, some of which need interpolating, so I am using another function to iterate through the recordset and pass the function through each record's corresponding date in order to interpolate the value.
Sub SampleReadCurve()
Dim rs As Recordset
Dim iRow As Long, iField As Long
Dim strSQL As String
Dim CurveID As Long
Dim MarkRunID As Long
Dim ZeroCurveID As String
CurveID = 124
MarkRunID = 10167
ZeroCurveID = "'" & CurveID & "-" & MarkRunID & "'"
'strSQL = "SELECT * FROM dbo_VolatilityInput WHERE ZeroCurveID='124-10167'"
strSQL = "SELECT * FROM dbo_VolatilityInput WHERE ZeroCurveID=" & ZeroCurveID & " ORDER BY MaturityDate"
Set rs = CurrentDb.OpenRecordset(strSQL, Type:=dbOpenDynaset, Options:=dbSeeChanges)
If rs.RecordCount <> 0 Then
Do While Not rs.EOF
rs.MoveFirst
Debug.Print vbCrLf
Debug.Print "First", rs!ZeroCurveID, rs!MaturityDate, rs!ZeroRate, rs!DiscountFactor
rs.MoveLast
Debug.Print "Last", rs!ZeroCurveID, rs!MaturityDate, rs!ZeroRate, rs!DiscountFactor
Debug.Print "There are " & rs.RecordCount & " records and " _
& rs.Fields.Count & " fields."
Dim BucketTermAmt As Long
Dim BucketTermUnit As String
Dim BucketDate As Date
Dim MarkAsOfDate As Date
Dim InterpRate As Double
MarkAsOfDate = rs!MarkAsOfDate
BucketTermAmt = 3
BucketTermUnit = "m"
BucketDate = DateAdd(BucketTermUnit, BucketTermAmt, MarkAsOfDate)
InterpRate = CurveInterpolateRecordset(rs, BucketDate)
Debug.Print BucketDate, InterpRate
rs.MoveNext
Loop
End If
End Sub
For one individual record and date, the first function works fine. However, when I execute the second function, the loop keeps repeating infinitely and the program crashes. I don't understand why this happens because there is clearly an end condition in the second loop. The recordset is only 76 records so not extremely large.
Remove the block that starts with rs.MoveFirst and ends with rs.MoveLast from inside your while loop. They should be inside the if but before the while.

vb 6.0 can anyone help me with my code?

Im working with my project inventory system i want to display the filtered dates in my books table in the mysql in my listview1 using 2 DTPicker and make a report for it. Im having an error in my query in the classmodule idk if its only the query and im really confused im a begginer in vb 6.0...please in need your help guys.
Im using 2 tables namely books and supplier.
MY CODE IN THE 'CLASS MODULE':
Sub DisplayList(ListView1 As ListView, DateFrom As Date, DateTo As Date)
Dim lstItem As ListItem, a As Integer
Dim rs As New ADODB.Recordset
Dim sql As String
If rs.State = adStateOpen Then rs.Close
sql = " SELECT supplier.category,books.title,books.dataAcquired,books.amount,books.quantity,books.accesionno,books.conditions" & _
" From supplier INNER JOIN books" & _
" ON supplier.code=books.code" & _
" WHERE (((books.dataAcquired)>=#" & DateFrom & "#) and ((books.dataAcquired) <=#" & DateTo & "#))" & _
" GROUP BY supplier.category,books.title,books.dataAcquired,books.amount,books.quantity,books.accesionno,books.conditions" & _
" ORDER BY books.dataAcquired DESC;"
rs.Open sql, cnn
ListView1.ListItems.Clear
Do While Not rs.EOF
a = a + 1
Set lstItem = ListView1.ListItems.Add(, , a, 1, 1)
lstItem.SubItems(1) = rs(0).Value
lstItem.SubItems(2) = rs(1).Value
lstItem.SubItems(3) = rs(2).Value
lstItem.SubItems(4) = rs(3).Value
lstItem.SubItems(5) = rs(4).Value
lstItem.SubItems(6) = rs(5).Value
lstItem.SubItems(7) = rs(6).Value
rs.MoveNext
Loop
End Sub
MY CODE IN MY FORM:
Private Sub Show_Click()
clsData.DisplayList ListView1, DTPicker1.Value, DTPicker2.Value
lblCount.Caption = ListView1.ListItems.Count
End Sub
Private Sub Form_Load()
DTPicker1.Value = Date
DTPicker2.Value = Date
End Sub
Private Sub Form_Activate()
clsData.DisplayList ListView1, DTPicker1.Value, DTPicker2.Value
lblCount.Caption = ListView1.ListItems.Count
End Sub
Change # by '
format date how yyyy-MM-dd or yyyyMMdd
sql = " SELECT supplier.category,books.title,books.dataAcquired,books.amount,books.quantity,books.accesionno,books.conditions" & _
" From supplier INNER JOIN books" & _
" ON supplier.code=books.code" & _
" WHERE (((books.dataAcquired)>='" & format(DateFrom,"yyyy-MM-dd") & "') and ((books.dataAcquired) <='" & format(DateTo,"yyyy-MM-dd") & "'))" & _
" GROUP BY supplier.category,books.title,books.dataAcquired,books.amount,books.quantity,books.accesionno,books.conditions" & _
" ORDER BY books.dataAcquired DESC;"
change loop while added validations for recordset emptys, some how
if RecordsetIsClosed(rs) then exit sub
While Not RecordSetIsEmpty(rs)
a = a + 1
Set lstItem = ListView1.ListItems.Add(, , a, 1, 1)
lstItem.SubItems(1) = rs(0).Value
lstItem.SubItems(2) = rs(1).Value
lstItem.SubItems(3) = rs(2).Value
lstItem.SubItems(4) = rs(3).Value
lstItem.SubItems(5) = rs(4).Value
lstItem.SubItems(6) = rs(5).Value
lstItem.SubItems(7) = rs(6).Value
rs.MoveNext
wend
Public Function RecordSetIsEmpty(ByRef rs As ADODB.Recordset) As Boolean
' On Local Error GoTo RecordSetIsEmpty_Error
' RecordSetIsEmpty = True
' If rs Is Nothing Then
' RecordSetIsEmpty = True
' Exit Function
' End If
' If RecordsetIsClosed(rs) = True Then
' RecordSetIsEmpty = True
' Exit Function
' End If
RecordSetIsEmpty = (rs.BOF = True And rs.EOF = True)
' RecordSetIsEmpty_Done:
' Exit Function
' RecordSetIsEmpty_Error:
' Resume RecordSetIsEmpty_Done
End Function
Public Function RecordsetIsClosed(ByRef rs As ADODB.Recordset) As Boolean
On Local Error GoTo RecordsetIsClosed_Error
RecordsetIsClosed = True
If rs Is Nothing Then
RecordsetIsClosed = True
End If
If rs.State <> adStateClosed Then
RecordsetIsClosed = False
End If
RecordsetIsClosed_Done:
Exit Function
RecordsetIsClosed_Error:
Resume RecordsetIsClosed_Done
End Function
Dont forget to open the database connection
updated thanks Mark Bertenshaw
RecordSetIsEmpty is use for problems when do movenext.. well i remember
RecordsetIsClosed is use because in some cases and databases managers return not recordset or the recordset is not correct initialized
for example access is necessary use movefist before do movenext or read values