ms access - SQL statement for combobox row source -


i'm trying define sql statement use row source combobox on msaccess form. sql should select records table tbli particular table field matches variant parameter vars set user; however, if vars null or not present in table tbls, sql should select records in tbli.

i can code first parts of (vars matches or null):

select tbli.id, tbli.s tbli ((tbli.s = vars) or (vars null)) order tbli.id; 

where i'm struggling incorporating final element (vars not present in tbls). can code test absence of vars in tbls:

is null(dlookup("[tbls.id]","tbls","[tbls.id]= " & vars)) 

but can't work out how incorporate in sql statement. should work?

select tbli.id, tbli.s tbli tbli.s = vars or vars null or dlookup("[tbls.id]","tbls","[tbls.id]= " & vars) null order tbli.id; 

when run query returns every record in tbls no matter value of vars.

table structure:

tbli contains 2 fields, autonumber id , long s

tbls contains 1 field, autonumber id

my own approach problem this:

private sub setcombosource(vid variant)     dim ssql string     ssql = "select tbli.id, tbli.s " & _         "from tbli "     if isnull(vid) = false         if isnumeric(vid) = true             if dcount("id", "tbls", "id = " clng(vid)) > 0                 ssql = ssql & "where tbli.s = " & clng(vid)             end if         end if     end if     ssql = ssql & " order tbli.id"     me.cbocombobox.rowsource = ssql end sub 

btw, recommend give tables , fields more descriptive names , use aliasing in sql, table names. think it's best avoid using variant variables. use longs , take value less 1 mean user didn't select anything, or selected all, or whatever meaning want derive it. in other words, id's number greater 0 , id of less 1 in variable means id empty. use signal create new record, or return records, or whatever meaning want derive in given context.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

mysql - Pass value between different pages (form) with PHP -