There's an excellent FAQ here, faq181-1202, that describes how to leave the rowsource of a combo blank until the user has typed a number of characters. It's pretty slick, but I keep getting a SELECT syntax error that I don't understand.
My code in the combo's OnChange looks like this:
My ReloadDonorList, which is also in the Form's module looks like this:
and lastly, the BuildFullName function, which is in its own module, looks like this:
The error I receive, upon typing a letter, is this:
Sorry for the big chunks of code. Does anything stand out? All of the fields are simply strings, and if I replace the
in the SQL with, say,
and run it as its own query, it runs fine.
My code in the combo's OnChange looks like this:
Code:
Private Sub cboDonorList_Change()
Dim cbo As ComboBox
Dim sText As String
Set cbo = Me.cboDonorList
sText = cbo.Text
Select Case sText
Case " " ' Remove initial space
cbo = Null
Case Else ' Reload RowSource data.
Call ReloadDonorList(sText)
End Select
Set cbo = Nothing
End Sub
My ReloadDonorList, which is also in the Form's module looks like this:
Code:
Function ReloadDonorList(strDonor As String)
Dim strNewStub As String
strNewStub = Nz(Left(strDonor, conDonorMin), "")
' If first n chars are the same as previously, do nothing.
If strNewStub <> strDonorStub Then
If Len(strNewStub) < conDonorMin Then
'Remove the RowSource
Me.cboDonorList.RowSource = "SELECT [Donor].[ID], [Donor].[LastName] AS FullName WHERE (False);"
strDonorStub = ""
Else
'New RowSource
Me.cboDonorList.RowSource = "SELECT [Donor].[ID], BuildFullName([Donor].[LastName],[Donor].[FirstName],[Donor].[MiddleName],[Donor].[NickName]) AS FullName FROM Donor WHERE [Donor].[LastName] LIKE """ & strNewStub & "*"" ORDER BY [Donor].[LastName], [Donor].[FirstName], [Donor].[MiddleName], [Donor].[NickName];"
strDonorStub = strNewStub
End If
End If
End Function
Code:
Public Function BuildFullName(strLast As Variant, strFirst As Variant, strMiddle As Variant, strNick As Variant) As String
Dim strFullName As String
strFullName = ""
If Not IsNull(strLast) Then
strFullName = strLast
End If
If Not IsNull(strLast) And (Not IsNull(strFirst) Or Not IsNull(strMiddle) Or Not IsNull(strNick)) Then
strFullName = strFullName & ", "
End If
If Not IsNull(strFirst) Then
strFullName = strFullName & strFirst
End If
If Not IsNull(strMiddle) And (Not IsNull(strLast) Or Not IsNull(strFirst)) Then
strFullName = strFullName & " " & strMiddle
End If
If Not IsNull(strNick) And (Not IsNull(strLast) Or Not IsNull(strFirst)) Then
strFullName = strFullName & " (" & strNick & ")"
End If
BuildFullName = strFullName
End Function
Except that I'm almost positive things are ok.The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.
Sorry for the big chunks of code. Does anything stand out? All of the fields are simply strings, and if I replace the
Code:
LIKE """ & strNewStub & "*""
Code:
LIKE "fred*"