Checking the last record in a loop over a Recordset

wim1981

Registered User.
Local time
Today, 06:02
Joined
May 20, 2015
Messages
40
Hi,

when you loop over a recordset with Do While Not rs.EOF.

How can you check what is the last record and whats in it?

I need to know this because i have an invalid use of null when i loop over a recordset to get every year to calculate hours per year.

When the error occurs he points out the line where he wants to do the DSUM of the hours of each year. When the year is beond the last year thats in the table, he puts up the error message.

Here's my code:

Code:
Private Sub BtnNieuweOpleidingsuren_Click()

DoCmd.RunCommand acCmdSaveRecord
MsgBox "De gegevens zijn opgeslaan"

Dim yearr As Integer
Dim nextYear As Integer
Dim landmeterID As String
Dim SQL As String
Dim SQL1 As String
Dim rs As DAO.Recordset
Dim rs1 As DAO.Recordset
Dim i As Integer
Dim currentYear As Integer
Dim hours_followed As Integer
Dim exemption_this_year As Integer
Dim total_hours_this_year As Integer
Dim transaction As Integer
Dim total_hours_next_year As Integer
Dim exemption_next_year As Integer
Dim result As Integer
Dim Jaar As Integer
Dim verplichtJaar As Integer

LanIDTxt.SetFocus
landmeterID = LanIDTxt.Text


Jaar = year(Me.startdatumTxt)
SQL = "Select Id_jaar from dbo_Lan_verplichtejaaropleiding where Id_landmeter = '" & landmeterID & "' and Id_jaar >=" & Jaar & " order by Id_jaar asc"
Set rs = CurrentDb.OpenRecordset(SQL)

    Do While Not rs.EOF
    If (rs.EOF = True) Then
    
    Else
    
      yearr = rs!Id_jaar
      
    'Get the date from this year
    
    
    hours_followed = DSum("uren", "dbo_Lan_opleiding", "Id_landmeter ='" & landmeterID & "' and Year(datumopleiding) = " & yearr & "")
    exemption_this_year = DLookup("Urenvrijgesteld", "dbo_Lan_vrijstelling", "Id_landmeter = '" & landmeterID & "' and Jaar = " & yearr & "")
    total_hours_this_year = DLookup("Uren", "dbo_Lan_verplichttevolgen", "Jaar = " & yearr & "")
      
      'Calculate the amount of hours to transfer
      transaction = total_hours_this_year - exemption_this_year - hours_followed
      
   'Get the data from next year
     nextYear = yearr + 1
   
      total_hours_next_year = DLookup("Uren", "dbo_Lan_verplichttevolgen", "Jaar = " & nextYear & "")
      exemption_next_year = DLookup("Urenvrijgesteld", "dbo_Lan_vrijstelling", "Id_landmeter = '" & landmeterID & "' and Jaar = " & nextYear & "")
      If (transaction < 0) Then
        result = total_hours_next_year - exemption_next_year + transaction
      
      Else
      MsgBox "De Landmeter heeft te weinig uren gevolgd!"
      End If
      
      CurrentDb.Execute "update dbo_Lan_verplichtejaaropleiding set te_volgen_uren =" & result & " where Id_jaar =" & nextYear & " and Id_landmeter = '" & landmeterID & "'"
      
      
      Debug.Print yearr; hours_followed; exemption_this_year; total_hours_this_year; transaction
      
      End If
    rs.MoveNext
    Loop
    'End If
   rs.Close
   
   Forms![MainForm]![OpleidingOverzichtList].Requery
   'Forms![MainForm]![OpleidingOverzichtList].Form.Requery
   '[Forms]![MainForm]![OpleidingOverzichtList].Form.Requery
 
   

End Sub
 
Rs.movelast brings you to the last record.
 
Rs.movelast brings you to the last record.

So if i use rs.Movelast, i can check with the year?

Saying if (year = rs.Movelast) then
...
 
I changed the code little bit because first i think you asked to read out last record.

If i'm right you want to add some other parameters when it is the last record and otherwise the values of the recordset.

I see that you have problems with null values. Beter see function NZ. With this one you can check if the value is null and do something and if not do something else.

Code:
 Private Sub BtnNieuweOpleidingsuren_Click()

DoCmd.RunCommand acCmdSaveRecord
MsgBox "De gegevens zijn opgeslaan"

Dim yearr As Integer
Dim nextYear As Integer
Dim landmeterID As String
Dim SQL As String
Dim SQL1 As String
Dim rs As DAO.Recordset
Dim rs1 As DAO.Recordset
Dim i As Integer
Dim currentYear As Integer
Dim hours_followed As Integer
Dim exemption_this_year As Integer
Dim total_hours_this_year As Integer
Dim transaction As Integer
Dim total_hours_next_year As Integer
Dim exemption_next_year As Integer
Dim result As Integer
Dim Jaar As Integer
Dim verplichtJaar As Integer

LanIDTxt.SetFocus
landmeterID = LanIDTxt.Text


Jaar = year(Me.startdatumTxt)
SQL = "Select Id_jaar from dbo_Lan_verplichtejaaropleiding where Id_landmeter = '" & landmeterID & "' and Id_jaar >=" & Jaar & " order by Id_jaar asc"
Set rs = CurrentDb.OpenRecordset(SQL)

    Do[COLOR=red] until rs.EOF[/COLOR]
    If (rs.EOF = True) Then
    
    Else
    
      yearr = rs!Id_jaar
      
    'Get the date from this year
    
    
    hours_followed = DSum("uren", "dbo_Lan_opleiding", "Id_landmeter ='" & landmeterID & "' and Year(datumopleiding) = " & yearr & "")
    exemption_this_year = DLookup("Urenvrijgesteld", "dbo_Lan_vrijstelling", "Id_landmeter = '" & landmeterID & "' and Jaar = " & yearr & "")
    total_hours_this_year = DLookup("Uren", "dbo_Lan_verplichttevolgen", "Jaar = " & yearr & "")
      
      'Calculate the amount of hours to transfer
      transaction = total_hours_this_year - exemption_this_year - hours_followed
      
   'Get the data from next year
     nextYear = yearr + 1
   
      total_hours_next_year = DLookup("Uren", "dbo_Lan_verplichttevolgen", "Jaar = " & nextYear & "")
      exemption_next_year = DLookup("Urenvrijgesteld", "dbo_Lan_vrijstelling", "Id_landmeter = '" & landmeterID & "' and Jaar = " & nextYear & "")
      If (transaction < 0) Then
        result = total_hours_next_year - exemption_next_year + transaction
      
      Else
      MsgBox "De Landmeter heeft te weinig uren gevolgd!"
      End If
      
      CurrentDb.Execute "update dbo_Lan_verplichtejaaropleiding set te_volgen_uren =" & result & " where Id_jaar =" & nextYear & " and Id_landmeter = '" & landmeterID & "'"
      
      
      Debug.Print yearr; hours_followed; exemption_this_year; total_hours_this_year; transaction
      
      End If
    rs.MoveNext
    Loop
    'End If
   rs.Close
   
   Forms![MainForm]![OpleidingOverzichtList].Requery
   'Forms![MainForm]![OpleidingOverzichtList].Form.Requery
   '[Forms]![MainForm]![OpleidingOverzichtList].Form.Requery
 
   

End Sub
 
I changed the code little bit because first i think you asked to read out last record.

If i'm right you want to add some other parameters when it is the last record and otherwise the values of the recordset.

I see that you have problems with null values. Beter see function NZ. With this one you can check if the value is null and do something and if not do something else.

Code:
 Private Sub BtnNieuweOpleidingsuren_Click()

DoCmd.RunCommand acCmdSaveRecord
MsgBox "De gegevens zijn opgeslaan"

Dim yearr As Integer
Dim nextYear As Integer
Dim landmeterID As String
Dim SQL As String
Dim SQL1 As String
Dim rs As DAO.Recordset
Dim rs1 As DAO.Recordset
Dim i As Integer
Dim currentYear As Integer
Dim hours_followed As Integer
Dim exemption_this_year As Integer
Dim total_hours_this_year As Integer
Dim transaction As Integer
Dim total_hours_next_year As Integer
Dim exemption_next_year As Integer
Dim result As Integer
Dim Jaar As Integer
Dim verplichtJaar As Integer

LanIDTxt.SetFocus
landmeterID = LanIDTxt.Text


Jaar = year(Me.startdatumTxt)
SQL = "Select Id_jaar from dbo_Lan_verplichtejaaropleiding where Id_landmeter = '" & landmeterID & "' and Id_jaar >=" & Jaar & " order by Id_jaar asc"
Set rs = CurrentDb.OpenRecordset(SQL)

    Do[COLOR=red] until rs.EOF[/COLOR]
    If (rs.EOF = True) Then
    
    Else
    
      yearr = rs!Id_jaar
      
    'Get the date from this year
    
    
    hours_followed = DSum("uren", "dbo_Lan_opleiding", "Id_landmeter ='" & landmeterID & "' and Year(datumopleiding) = " & yearr & "")
    exemption_this_year = DLookup("Urenvrijgesteld", "dbo_Lan_vrijstelling", "Id_landmeter = '" & landmeterID & "' and Jaar = " & yearr & "")
    total_hours_this_year = DLookup("Uren", "dbo_Lan_verplichttevolgen", "Jaar = " & yearr & "")
      
      'Calculate the amount of hours to transfer
      transaction = total_hours_this_year - exemption_this_year - hours_followed
      
   'Get the data from next year
     nextYear = yearr + 1
   
      total_hours_next_year = DLookup("Uren", "dbo_Lan_verplichttevolgen", "Jaar = " & nextYear & "")
      exemption_next_year = DLookup("Urenvrijgesteld", "dbo_Lan_vrijstelling", "Id_landmeter = '" & landmeterID & "' and Jaar = " & nextYear & "")
      If (transaction < 0) Then
        result = total_hours_next_year - exemption_next_year + transaction
      
      Else
      MsgBox "De Landmeter heeft te weinig uren gevolgd!"
      End If
      
      CurrentDb.Execute "update dbo_Lan_verplichtejaaropleiding set te_volgen_uren =" & result & " where Id_jaar =" & nextYear & " and Id_landmeter = '" & landmeterID & "'"
      
      
      Debug.Print yearr; hours_followed; exemption_this_year; total_hours_this_year; transaction
      
      End If
    rs.MoveNext
    Loop
    'End If
   rs.Close
   
   Forms![MainForm]![OpleidingOverzichtList].Requery
   'Forms![MainForm]![OpleidingOverzichtList].Form.Requery
   '[Forms]![MainForm]![OpleidingOverzichtList].Form.Requery
 
   

End Sub[/QUOTE]
Yes indeed i have problems with null values. 
But how do i implement the NZ function?
 
try this

Do Until rs.EOF
If (Nz(rs!Id_jaar, 0) = 0) Then
'do something
Else

yearr = rs!Id_jaar
 
try this

Do Until rs.EOF
If (Nz(rs!Id_jaar, 0) = 0) Then
'do something
Else

yearr = rs!Id_jaar

That doesn't solve the problem :(.

When he throws the error he points to this line:

hours_followed = DSum("uren", "dbo_Lan_opleiding", "Id_landmeter ='" & landmeterID & "' and Year(datumopleiding) = " & yearr & "")

When you loop through the years and in the table there are years 2015, 2016 and 2017. Then when i do yearr + 1 he comes to 2018. But 2018 isn't present in the table, so he throws the null error.
 
What error it gave?

What is the value of it when you go over it with the mouse i debug mode?

Can you upload a stripped down version of your DB so can see at it?
 
What error it gave?

What is the value of it when you go over it with the mouse i debug mode?

Can you upload a stripped down version of your DB so can see at it?

its a runtime error "94" Invalid use of null

When i have inserted the hours for 2015 and there are no hours for 2016, then he get's the error on year 2016. So value in debug mode is 2016

No i can't upload the db :(
 
Jaar = year(Me.startdatumTxt)
SQL = "Select Id_jaar from dbo_Lan_verplichtejaaropleiding where Id_landmeter = '" & landmeterID & "' and Id_jaar >=" & Jaar & " order by Id_jaar asc"
Set rs = CurrentDb.OpenRecordset(SQL)

Can you change your SQL so it not get year 2016 because there are no hours for it you say? Now if you put 2015 in the txtbox it will take all the years from 2015 and further

Just thinking ...
 
Jaar = year(Me.startdatumTxt)
SQL = "Select Id_jaar from dbo_Lan_verplichtejaaropleiding where Id_landmeter = '" & landmeterID & "' and Id_jaar >=" & Jaar & " order by Id_jaar asc"
Set rs = CurrentDb.OpenRecordset(SQL)

Can you change your SQL so it not get year 2016 because there are no hours for it you say? Now if you put 2015 in the txtbox it will take all the years from 2015 and further

Just thinking ...

The year from where he starts the calculation is good. It is the year afther the last year thats in the table he wants to calculate that gets the null error
 
just said you get the error at

hours_followed = DSum("uren", "dbo_Lan_opleiding", "Id_landmeter ='" & landmeterID & "' and Year(datumopleiding) = " & yearr & "")

so if there is no data it can be 0 because they didn't follow some yet? Right?

Try this one, if it returns Null it will take the 0 value instead

hours_followed = NZ(DSum("uren", "dbo_Lan_opleiding", "Id_landmeter ='" & landmeterID & "' and Year(datumopleiding) = " & yearr & ""),0)

If that not works you first have to check if the query of the DSUM you use is not null, if null use 0 else use the dsum function
 
just said you get the error at

hours_followed = DSum("uren", "dbo_Lan_opleiding", "Id_landmeter ='" & landmeterID & "' and Year(datumopleiding) = " & yearr & "")

so if there is no data it can be 0 because they didn't follow some yet? Right?

Try this one, if it returns Null it will take the 0 value instead

hours_followed = NZ(DSum("uren", "dbo_Lan_opleiding", "Id_landmeter ='" & landmeterID & "' and Year(datumopleiding) = " & yearr & ""),0)

If that not works you first have to check if the query of the DSUM you use is not null, if null use 0 else use the dsum function

Aha i can check if hours_followed is Null of not? :)
 
Aha i can check if hours_followed is Null of not? :)

Ive seen in the database that the hours column doesn't accept nulls. I could change that so it allows nulls. But maybe it is not best practice?
 
don't think so because the error is invalid use of Null so the error is in the DSUM function.

you check it before you calculate ,

try

hours_followed_Valid= DCOUNT("uren", "dbo_Lan_opleiding", "Id_landmeter ='" & landmeterID & "' and Year(datumopleiding) = " & yearr & "")

if (hours_followed_valid >0) then

...

end if
 
don't think so because the error is invalid use of Null so the error is in the DSUM function.

you check it before you calculate ,

try

hours_followed_Valid= DCOUNT("uren", "dbo_Lan_opleiding", "Id_landmeter ='" & landmeterID & "' and Year(datumopleiding) = " & yearr & "")

if (hours_followed_valid >0) then

...

end if
It works for the error, but then the calculation gets messed up.
 
Are the values who worked before also messed up? What's the difference with before?

Put
Code:
debug.print hours_followed_valid
after the hours_followed_valid = Dcount line.

This will display in de DIRECT display of VBA wich values he gets when running thru your recordset so you can see if it somethings get 0 and somethimes other values, if only 0 then there is something else wrong

It's difficult to help without seeing the DB, so can only suggest some things to try find the problem
 
Are the values who worked before also messed up? What's the difference with before?

Put
Code:
debug.print hours_followed_valid
after the hours_followed_valid = Dcount line.

This will display in de DIRECT display of VBA wich values he gets when running thru your recordset so you can see if it somethings get 0 and somethimes other values, if only 0 then there is something else wrong

It's difficult to help without seeing the DB, so can only suggest some things to try find the problem

The differents is that now he puts the result in all the years after the year you inserted the hours
so, if you insert hours for 2015 and the result for 2016 is 28. He defaultly sets the result for 2017 also at 28. It should be as before just 28 at the year 2016.
 
The differents is that now he puts the result in all the years after the year you inserted the hours
so, if you insert hours for 2015 and the result for 2016 is 28. He defaultly sets the result for 2017 also at 28. It should be as before just 28 at the year 2016.

i tried the debug print and its get me
2
0
0
so 2 for 2015 and for further years 0
 

Users who are viewing this thread

Back
Top Bottom