Access VBA Filter RecordCount not properly updating value -
i have form have built filter search on runs "after_update". when filter results in no records form fails , shows blank. around found several posts recommending add "recordcount" , use "if" statement trigger filter if not <1 or =0. issue value of recordcount seems showing # of records last ran filter selections, not current filter. have tried several methods "requery" , update value of recordcount after filter applied cant update intend work.
example:
filter 1: results in 14 records, debug.print recordcount displays 1
filter 2: results in 22 records, debug.print recordcount displays 14
filter 3: results in 0 records, form fails, debug.print recordcount displays 22
code:
private sub applyfilterbtn_click() on error goto err_applyfilterbtn_click dim stfilter string stfilter = "" if nz(me.filterowner, "") <> "" stfilter = stfilter & "[machineowner] = " & me.filterowner & " , " end if if nz(me.filtertype, "") <> "" stfilter = stfilter & "[machinetype] = " & me.filtertype & " , " end if if nz(me.filtersubtype, "") <> "" stfilter = stfilter & "[machinesubtype] = " & me.filtersubtype & " , " end if if nz(me.filtermake, "") <> "" stfilter = stfilter & "[make] '" & me.filtermake & "' , " end if if nz(me.filtermodel, "") <> "" stfilter = stfilter & "[model] '*" & me.filtermodel & "*' , " end if if nz(me.filtersn, "") <> "" stfilter = stfilter & "[sn] '" & me.filtersn & "' , " end if if nz(me.filterstatus, "") <> "" stfilter = stfilter & "[newstatus] = " & me.filterstatus & " , " end if if stfilter <> "" stfilter = left(stfilter, len(stfilter) - 5) 'remove , '<<<<<<<issue starts here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< me.recordset.clone me.recordsetclone.filter = stfilter me.recordsetclone.movelast ' me.recordsetclone.movenext 'tried - did not ' me.recordsetclone.requery 'tried - did not ' me.filter = stfilter ' tries - did not 'debugging see value of recordcount debug.print stfilter debug.print me.recordsetclone.recordcount '>>>>>> recordsetclone.recordcount value not refreshing above, shows called filter record count if me.recordsetclone.recordcount < 1 ' no records, don't filter 'if not (me.recordsetclone.bof , me.recordsetclone.eof) 'work around attempt 2 - failed 'if me.nomatch 'work around attempt 3 - failed removefilterbtn_click ' call sub clears filter msgbox "filter results in no records", vbokonly, "no results" else ' there records, turn on filter me.filter = stfilter me.filteron = true end if 'me.recordcount < 1 else me.filteron = false removefilterbtn_click ' clears filter end if 'stfilter <> "" exit_applyfilterbtn_click: exit sub err_applyfilterbtn_click: msgbox err.description resume exit_applyfilterbtn_click end sub private sub removefilterbtn_click() on error goto err_removefilterbtn_click 'sets filter field values blank me.filterowner = "" me.filtertype = "" me.filtermake = "" me.filtermodel = "" me.filtersn = "" me.filterstatus = "" me.filtersubtype = "" 'removes filter me.filter = "" me.filteron = false exit_removefilterbtn_click: exit sub err_removefilterbtn_click: msgbox err.description resume exit_removefilterbtn_click end sub
it simpler need right syntax:
dim rst dao.recordset dim rstfiltered dao.recordset set rst = me.recordsetclone rst.filter = stfilter set rstfiltered = rst.openrecordset() if rstfiltered.recordcount > 0 ' stuff. end if rstfiltered.close set rstfiltered = nothing set rst = nothing
Comments
Post a Comment