Software: Access 2000
Operating System: Windows XP Professional SP3
Problem: Error 2001 based on filter being "ModelYear <= 2006".
Solution: Table's datatype was text when SQL expected number.
I'm in the middle of debugging an issue with some dynamic SQL code and I think I may have missed some important rule. When I try to use it, it pops up with a general syntax error (Runtime Error 2001). My error arises when I have the function Me.frmSubForm.F orm.Filter = SQLFilter even though SQLFilter = "ModelYear <= 2006". Here's the relevant function information:
Main function:
Sub-functions from the lib Module:
GreaterThanAtta chAnd():
LessThanAttachA nd():
StrReplace():
Operating System: Windows XP Professional SP3
Problem: Error 2001 based on filter being "ModelYear <= 2006".
Solution: Table's datatype was text when SQL expected number.
I'm in the middle of debugging an issue with some dynamic SQL code and I think I may have missed some important rule. When I try to use it, it pops up with a general syntax error (Runtime Error 2001). My error arises when I have the function Me.frmSubForm.F orm.Filter = SQLFilter even though SQLFilter = "ModelYear <= 2006". Here's the relevant function information:
Main function:
Code:
'Outputs string of SQL statement
Private Function SQLFilter() As String
Dim strSQLContent As String, strDelim As String
'detecting whether to use AND or OR
'based on button 'ANY' or 'ALL'
strDelim = IIf(Me.btn2SearchSettingsAnyAll.Value = 1, " OR ", " AND ")
'transforming form inputs into SQL code with subfunctions
strSQLContent = strSQLContent & lib.GreaterThanAttachAnd("ModelYear", Me.txtMinModelYear.Value, strDelim)
strSQLContent = strSQLContent & lib.LessThanAttachAnd("ModelYear", Me.txtMaxModelYear.Value, strDelim)
'Clean up the concatenation of all of the fields into a SQL statement fragment
SQLFilter = lib.StrReplace(strSQLContent, strDelim, "", 1)
GreaterThanAtta chAnd():
Code:
'This code is meant to a string as part of a dynamic SQL statement.
'strField = field (i.e. 'strJusticeLeague')
'strValue = control (i.e. 'Me.strBatman')
'strDelim = 'AND' or 'OR'.
Public Function GreaterThanAttachAnd(strField As Variant, strValue As Variant, strDelim As Variant) As String
If strValue = "''" Or strValue = "" Or IsNull(strValue) Then
Exit Function
Else
strValue = Trim(strValue)
If strValue Like "##/##/####" Then 'filters dates
strValue = "#" & strValue & "#"
Else: If IsNumeric(strValue) = False Then strValue = "'" & strValue & "'"
End If
GreaterThanAttachAnd = strDelim & strField & " >= " & strValue
End If
End Function
Code:
'This code is meant to a string as part of a dynamic SQL statement.
'strField = field (i.e. 'strJusticeLeague')
'strValue = control (i.e. 'Me.strBatman')
'strDelim = 'AND' or 'OR'.
Public Function LessThanAttachAnd(strField As Variant, strValue As Variant, strDelim As Variant) As String
If strValue = "''" Or strValue = "" Or IsNull(strValue) Then
Exit Function
Else
strValue = Trim(strValue)
If strValue Like "##/##/####" Then
strValue = "#" & strValue & "#"
Else: If IsNumeric(strValue) = False Then strValue = "'" & strValue & "'"
End If
LessThanAttachAnd = strDelim & strField & " <= " & strValue
End If
End Function
Code:
Public Function StrReplace(strHaystack As String, strNeedle As String, strReplaceWith As String, intRepeat As Integer) As String
Dim init As Long, i As Integer
'Failsafe for illegal inputs, then declarations after "Else "
If Len(strHaystack) < 1 Or Len(strNeedle) < 1 Or intRepeat > 0 = False Then
Exit Function
Else
init = InStr(strHaystack, strNeedle)
i = IIf(IsNull(intRepeat), 99, intRepeat)
End If
Do While init > 0 And IIf(IsNull(intRepeat), 1 = 1, i > 0)
strHaystack = Left(strHaystack, init - 1) & strReplaceWith & Right(strHaystack, Len(strHaystack) - Len(strNeedle) - init + 1)
'Maintenance for While-loop
i = IIf(intRepeat > 0, i - 1, i)
init = InStr(strHaystack, strNeedle)
Loop
StrReplace = strHaystack
End Function
Comment