DAO code error

Gregof1976

Registered User.
Local time
Today, 00:00
Joined
Mar 28, 2011
Messages
44
Hi All.

What I did wrong wit enclosed code? No, action after push the button..


Code:
Private Sub klik_Click()
Dim dbsRejestr As DAO.Database
Dim rstColor As DAO.Recordset
    Set dbsRejestr = CurrentDb
    Set rstColor = dbsRejestr.OpenRecordset("TblCar")
    
rstColor.MoveFirst

Do Until rstColor.EOF
    If rstColor!Color = "Red" Then
        rstColor.Edit
        rstColor!Color = "Orange2"
        rstColor.Update
    
    End If
    
    rstColor.MoveNext
    
    Loop
End Sub
 
You haven't mentioned what the error message is? Seeing your code without knowing what the error message is is pointless to us.

Also, did you know that you can use an UPDATE query for this?
 
Yes, I'm aware that I can use UPDATE query. This exercises can me answer on some possible solution that in the future I would like apply in my database. I'v never use before DAO, but I think it can improve my dB.
I woluld like update boolean field in the main table by this code depend on specific data.

As regard code the problem is that no result after push the button. Everything is like I have an example, but in result there is no action and even errorr code... in the other words errors is lack of any result ;-)
 
But how are you sure that this way of updating a table will solve your future problems? An UPDATE query is (most of the time) sufficient and even better for multiple updates.

Use this:
Code:
Private Sub klik_Click()
    Dim dbsRejestr As DAO.Database
    Dim rstColor As DAO.Recordset
    
    Set dbsRejestr = CurrentDb
    Set rstColor = dbsRejestr.OpenRecordset("TblCar")
        
    rstColor.MoveFirst
    
    Do While Not rstColor.EOF
        If rstColor![Color] = "Red" Then
            MsgBox "Is Red"
            rstColor.Edit
            rstColor![Color] = "Orange2"
            rstColor.Update
        End If
        rstColor.MoveNext
    Loop
End Sub
and tell me if you see the message box.
 
I'm not sure, but I can suppose..

Right now, dB work in such way that end user have only acces to Form ( Queries; Table; Reports are hiden) and all kind of reports are avaliable only from Excel level (user call to dB Acess via MS Query).

I would like add for final user some possibilities like 'press the button' and end-of-the month some table filed will be adjusted automaticly.

That's my whole philosophy...

As regard code no change ...
 
So there was no message box?

Then the code is fine. It's just not finding red. Create a query and type "Red" under the Criteria for Color. See if it comes back with some records.

Also, Colour is not a good name for a field because that's a reserved keyword for Access.
 
You are right I have mismatch in "Red'; code works perfect;

Thanks once more for your help!
 

Users who are viewing this thread

Back
Top Bottom