I am trying to set the row source of a series of combo boxs in vba, where the value chosen in the ‘parent’ of a particular combo become the value used in the WHERE clause for the row source sql statement.
I’ve done this before and had it working the way I wanted.
The difficulty I’m having this time is that if the ‘parent’ combo is blank I want to return ALL records for my row source, not none.
I’ve had a look at some queries with parameters and can achieve this, what I can’t do is recreate this in vba. I’m sure I’m getting confused with my strings and having a zero length string when I really want a NULL.
Additionally, the value picked from cboE is a string, whereas the value from cboM is a NUMBER
Here’s myCode, where I have 3 combo boxes cboE, cboM and cboC.
for cboE the row source is:
strESQL = “SELECT DISTINCT tblDataTest.FieldE FROM tblTest”
for cboM the row source is:
strMSQL = “SELECT DISTINCT tblDataTest.fieldM FROM tblDataTest”
strMSQL = strMSQL & ” WHERE (tblTest.fieldE = ‘” & strE & “‘ Or (‘” & strE & “‘ Is Null))”
strMSQL = strMSQL & ” ORDER BY tblTest.fieldM;”
for cboC the row source is:
strCSQL = “SELECT DISTINCT tblDataTest.fieldC FROM tblDataTest”
strCSQL = strCSQL & ” WHERE (tblDataTest.fieldE = ‘” & strELR & “‘ Or (‘” & strE & “‘ Is Null))”
strCSQL = strCSQL & ” AND ((tblDataTest.fieldM = ” & intMile & ” Or (” & intMile & ” Is Null))”
strCSQL = strCSQL & ” ORDER BY tblDataTest.fieldC;”
so before anything is picked from cboE the row source is
SELECT DISTINCT tblDataTest.fieldM FROM tblDataTest WHERE (tblDataTest.ELR = ” Or (” Is Null)) ORDER BY tblDataTest.fieldM;
which obviously is not going to return all my rows as I know ” is not the same as null, but how can I get it to be null
similarly (for cboC)
SELECT DISTINCT tblDataTest.fieldC FROM tblDataTest WHERE (tblDataTest.fieldE = ” Or (” Is Null)) And (tblDataTest.Mileage = 0 Or (0 Is Null)) ORDER BY tblDataTest.fieldC;
as 0 isn’t null its not gonna work
Any ideas?, its the OR parts I’m stuck on. The examples use variables but I also had no luck with referencing the cbo values directly.