sql - Validating records in one table based on data in another table -


this question offshoot of this question here. using loop loop through recordset based on master table, able records returned inline sql statement show. however, when put validation code in loop, valid employees being reported invalid , vice versa.

public function validemployee(empid string)  dim dbs dao.database dim rs dao.recordset dim sqlstring string set dbs = currentdb  sqlstring = "select [employeeid] [master] [employeestatus] = 'terminated'"  set rs = dbs.openrecordset(sqlstring) rs.movelast rs.movefirst // obtain accurate count of records in recordset  if not (rs.bof , rs.eof) // verify recordset not empty     until rs.eof         if instr(1, rs.fields("employeeid"), empid, vbtextcompare) = 0             validemployee = "valid employee"         else             validemployee = "employee" & empid & "is invalid"             exit         end if     loop     rs.movenext end if 

some of steps have tried include:

  • checking leading or trailing spaces in field names
  • validating values of empid , rs.fields("employeeid") via debug.print
  • checking syntax errors both in sql , in vba
  • quotation / escaping of string literals in sql statement

i feel problem in way wrote instr() comparison. comparison using single record without loop works fine.

you don't need loop on of records find match: database can handle that.

public function validemployee(empid string)      dim rs dao.recordset     dim sqlstring string      'add single-quotes around empid if it's not numeric field     sqlstring = "select count([employeeid]) num [master] " & _                 " [employeestatus] = 'terminated' , " & _                 " [employeeid] = " & empid      set rs = currentdb.openrecordset(sqlstring)     validemployee = rs.fields("num").value>0 end if 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -