Could you use a second table (YearTable) to hold year and count information:
YearCode YearCount
2009 1
2010 2
Now you could SELECT MAX(YearCount) AS YearVal FROM YearTable WHERE YearCode = 2010 Also SELECT MAX(YearCount) AS YearVal FROM YearTable WHERE YearCode = 2011 would return
YearVal = DbNull. If YearVal = DbNull then you could add a row: 2011 0
I think this would be the easiest solution.
HTH
Teme64
Veteran Poster
1,040 posts since Aug 2008
Reputation Points: 218
Solved Threads: 206
Skill Endorsements: 5
Agreed with Teme64. IMO is the best solution.
But if you can not change the database design for any reson, if I remember well, in Access you can force to return 0 when not found using the immediate if function (IIF):
("Select iif(max(val(mid(cCode,5,len(cCode)))=Null, 0, max(val(mid(cCode,5,len(cCode))))) as maxV from Contact where val(left(cCode,4)) = " & Val(cboYear.SelectedItem), con)
Hope this helps
lolafuertes
Practically a Posting Shark
895 posts since Oct 2008
Reputation Points: 164
Solved Threads: 189
Skill Endorsements: 5
Datareader has IsDBNull method, use that:
If reader.IsDBNull("maxV") Then
' Value is null, handle it
Else
' Value is not null, use the value
End If
HTH
Teme64
Veteran Poster
1,040 posts since Aug 2008
Reputation Points: 218
Solved Threads: 206
Skill Endorsements: 5
You must invert the IF in order to verify that the returned value is not null before using it like in the following code
If [B]Not[/B] reader.IsDBNull(0) Then
txt.Text = Val(cboYear.SelectedItem) & (reader.GetValue(0) + 1)
Else
txt.Text = Val(cboYear.SelectedItem) & 1
End If
I used to referene the returned fields by their position because is a little bit faster than resolving them by name.
Hope this helps
lolafuertes
Practically a Posting Shark
895 posts since Oct 2008
Reputation Points: 164
Solved Threads: 189
Skill Endorsements: 5
Question Answered as of 1 Year Ago by
Teme64
and
lolafuertes