I've not done any vba for years and totally forgotten how to do this - it's like learning vba all over again!
I have a form which has an "on open" event to populate data from an ado recordset.
What I am trying to do is provide some text boxes at the top to allow the user to enter some criteria and change the recordset they are viewing.
Here's my "on open" code;
Private Sub Form_Open(Cancel As Integer)
qString = "SELECT tblPerson.personID, [personLName]+', '+[personFName] AS fullName, tblFamily.familyAFCID, tblAddress.addressLine1, " & _
" tblFamily.personAFCID, refSource.sourceDescription AS personSource " & _
" FROM refSource RIGHT JOIN (((tblPerson LEFT JOIN tblFamily ON tblPerson.personID = tblFamily.personID) " & _
" LEFT JOIN lnkAddressPerson ON tblPerson.personID = lnkAddressPerson.personID) " & _
" LEFT JOIN tblAddress ON lnkAddressPerson.addressID = tblAddress.addressID) ON refSource.sourceID = tblPerson.personSource " & _
" WHERE lnkAddressPerson.addresslinkend is null "
qStringAppend = " order by personLName, personFName;"
bindFormData qString, qStringAppend, "frmPerson"
End Sub
And bindFormData sub is;
Public Sub bindFormData(ByVal qString As String, ByVal qStringAppend As String, ByVal formName As String)
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = CurrentProject.AccessConnection
Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = cn
.Source = qString + qStringAppend
.LockType = adLockBatchOptimistic
.CursorType = adOpenKeyset
.CursorLocation = adUseClient
.Open
End With
Set Forms(formName).Recordset = rs
Set rs = Nothing
Set cn = Nothing
End Sub
Here's what I am trying to do on the command button;
Private Sub cmdApplyFilter_Click()
Dim qStringAppend As String, qString As String
qStringAppend = ""
If IsNull(Me.txtNameFilter) = False Then
qStringAppend = qStringAppend + " and (lcase(personFname) like '*" & LCase(Me.txtNameFilter) & "*' or lcase(personLName) like '*" & LCase(Me.txtNameFilter) & "*' )"
End If
qString = "SELECT tblPerson.personID, [personLName]+', '+[personFName] AS fullName, tblFamily.familyAFCID, tblAddress.addressLine1, " & _
" tblFamily.personAFCID, refSource.sourceDescription AS personSource " & _
" FROM refSource RIGHT JOIN (((tblPerson LEFT JOIN tblFamily ON tblPerson.personID = tblFamily.personID) " & _
" LEFT JOIN lnkAddressPerson ON tblPerson.personID = lnkAddressPerson.personID) " & _
" LEFT JOIN tblAddress ON lnkAddressPerson.addressID = tblAddress.addressID) ON refSource.sourceID = tblPerson.personSource " & _
" WHERE lnkAddressPerson.addresslinkend is null "
qStringAppend = qStringAppend + " order by personLName, personFName;"
bindFormData qString, qStringAppend, "frmPerson"
Me.Repaint
End Sub
My form populates correctly on the open event, but returns zero records on the command button click. I have debug.print the sql on the command button click and verified that it returns records in a query.
Any help appreciated.