dbTalk Databases Forums  

Updating records on a table from code

comp.database.ms-access comp.database.ms-access


Discuss Updating records on a table from code in the comp.database.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Rob
 
Posts: n/a

Default Updating records on a table from code - 09-04-2004 , 04:10 PM






This should be an easy one to figure out but I'm not getting it. I've
got a form reading data from a table (tblTempTest5Level). I've got a
button on the form that when clicked it would update the value of a
field (Choice) in the table. I've got it working but it only changes
the value in the first record in the table, I want it to change the
value of the record that's currently displayed in the form. Here's the
code I've got in the OnClick event:

Private Sub CmdA_Click()
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb()
Set rec = db.OpenRecordset("tblTempTest5Level")

rec.Edit
rec!choice.Value = "A"
rec.Update

Refresh

End Sub

Any help would be appreciated

Rob

Reply With Quote
  #2  
Old   
Geoff Goddard
 
Posts: n/a

Default Re: Updating records on a table from code - 09-10-2004 , 03:39 AM






google (AT) pinnaclepromos (DOT) com (Rob) wrote in message news:<85d3bc52.0409041310.3918fa9 (AT) posting (DOT) google.com>...
Quote:
This should be an easy one to figure out but I'm not getting it. I've
got a form reading data from a table (tblTempTest5Level). I've got a
button on the form that when clicked it would update the value of a
field (Choice) in the table. I've got it working but it only changes
the value in the first record in the table, I want it to change the
value of the record that's currently displayed in the form. Here's the
code I've got in the OnClick event:

Private Sub CmdA_Click()
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb()
Set rec = db.OpenRecordset("tblTempTest5Level")

rec.Edit
rec!choice.Value = "A"
rec.Update

Refresh

End Sub

Any help would be appreciated

Rob
I think that you code will only deal with the first record because you
haven't set up a loop

What you probably need is this.

If rec.RecordCount
rec.MoveFirst
Do Until rec.EOF
rec.Edit
rec!choice.Value = "A"
rec.Update
rec.MoveNext
Loop
End If

The 'If' Statement checks to see if there is anything to do. You'd get
an error message if the table was empty otherwise.

You then go to the first record and update it before moving on to the
next one. The process keeps looping until you reach the end of file
(EOF)

Hope this works.

Geoff Goddard


Reply With Quote
  #3  
Old   
Geoff Goddard
 
Posts: n/a

Default Re: Updating records on a table from code - 09-11-2004 , 02:51 AM



geoff (AT) sunny-side (DOT) ws (Geoff Goddard) wrote in message news:<d9d681ee.0409100039.14391640 (AT) posting (DOT) google.com>...
Quote:
google (AT) pinnaclepromos (DOT) com (Rob) wrote in message news:<85d3bc52.0409041310.3918fa9 (AT) posting (DOT) google.com>...
This should be an easy one to figure out but I'm not getting it. I've
got a form reading data from a table (tblTempTest5Level). I've got a
button on the form that when clicked it would update the value of a
field (Choice) in the table. I've got it working but it only changes
the value in the first record in the table, I want it to change the
value of the record that's currently displayed in the form. Here's the
code I've got in the OnClick event:

Private Sub CmdA_Click()
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb()
Set rec = db.OpenRecordset("tblTempTest5Level")

rec.Edit
rec!choice.Value = "A"
rec.Update

Refresh

End Sub

Any help would be appreciated

Rob

I think that you code will only deal with the first record because you
haven't set up a loop

What you probably need is this.

If rec.RecordCount
rec.MoveFirst
Do Until rec.EOF
rec.Edit
rec!choice.Value = "A"
rec.Update
rec.MoveNext
Loop
End If

The 'If' Statement checks to see if there is anything to do. You'd get
an error message if the table was empty otherwise.

You then go to the first record and update it before moving on to the
next one. The process keeps looping until you reach the end of file
(EOF)

Hope this works.

Geoff Goddard
Rob,

Sorry completely missread your message - my solution would update all
records. What you need to do is identify the record you are trying to
update. If your recordset has an ID field (autonumber)it's not
difficult.

Place that field on your form then go this way

Set rec = db.OpenRecordset("SELECT * FROM tblTempTest5Level WHERE ID =
" & Me.ID)

rec.Edit
rec!choice.Value = "A"
rec.Update


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.