ms access - How to write VBA with Do While Loop? -


i have table 3 fields: donor_contact_id, recipient_contact_id, order_number. want sort donor_contact_id in ascending order did query q_recipient_sort. want use temporary variables check see if records have same donor_contact_id , display message if (most of records have same donor_contact_id). program supposed to, @ end gets error says "no current record". here code:

option compare database option explicit   function usingtemps()  dim dbs dao.database dim rst dao.recordset dim strtemp1 long dim strtemp2 long  docmd.setwarnings false docmd.openquery ("q_recipient_sort") docmd.opentable ("t_recipient_sort") docmd.setwarnings true set dbs = currentdb  set rst = dbs.openrecordset("t_recipient_sort", dbopentable)  rst.movefirst strtemp1 = rst!donor_contact_id rst.movenext strtemp2 = rst!donor_contact_id  while not (rst!donor_contact_id = rst.eof)  if strtemp1 = strtemp2 msgbox ("equal")  else msgbox ("not equal")  end if  strtemp1 = strtemp2 rst.movenext strtemp2 = rst!donor_contact_id  loop  set dbs = nothing  end function 

i think problem following lines:

rst.movenext  strtemp2 = rst!donor_contact_id 

i think trying move next record when there no more records left. wrong logic. i've been staring @ while , changes haven't worked. need set of eyes take @ it.

any appreciated!

consider happens when recordset loop on last row, , ...

rst.movenext strtemp2 = rst!donor_contact_id 

movenext positions recordset @ eof --- no record "current". so, in next line, code attempts store value current row's donor_contact_id strtemp2. however, since you're @ eof, no record "current", access complains "no current record".

i think version avoid error. test logic make sure need.

rst.movefirst strtemp1 = rst!donor_contact_id rst.movenext 'strtemp2 = rst!donor_contact_id  'do while not rst!donor_contact_id = rst.eof while not rst.eof     strtemp2 = rst!donor_contact_id     if strtemp1 = strtemp2         msgbox "equal"     else         msgbox "not equal"     end if     strtemp1 = strtemp2     rst.movenext     'strtemp2 = rst!donor_contact_id loop 

Comments