rs.Edit而不是rs.AddNew [英] rs.Edit instead of rs.AddNew

查看:156
本文介绍了rs.Edit而不是rs.AddNew的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,当单击表单上的按钮时,会在列表中为每个ID添加新记录

框。这很好用。我现在的问题是

我希望能够编辑ID在

列表框中的人的所有记录。我对代码做了一些小修改(主要用

rs.Edit替换rs.AddNew),它似乎只更新了第一条记录,然后

覆盖了该记录下一个,等等,直到它在

列表框中用完了ID。换句话说,它是逐步通过列表框中的ID,

而不是记录。这有诀窍吗?我花了很多时间

进行微小的更改仍然遇到同样的问题。


代码如下(我已经减少了我要更新的字段数量保持

消息的大小)。


Dim intIndex As Integer


对于intIndex = 0 To Me.lboBulkList.ListCount


Me.lbo.BulkList.Selected(intIndex)= True


下一页intIndex

Dim db作为数据库


Dim rs作为记录集


Dim prm作为参数


Dim qdf作为QueryDef


设置db = CurrentDb()

设置qdf = db.QueryDefs(" qryBulkEdit")


每个prm in qdf.Parameters


prm.Value = Eval(prm.Name)


下一个prm


设置rs = qdf.OpenRecordset(dbOpenDynaset)


Dim frm As Form


Dim ctl As Control


Dim varItm As Variant


设置frm = Fo rms!frmExcursions


设置ctl = frm! lboBulkList


每个变量在ctl.ItemsSelected


rs.Edit


rs(0 )=我。 lboBulkList.ItemData(varItm)


rs!Date = frm!txtDate


rs!Faculty = frm!cboFaculty


rs!课程= frm!课程


rs!费用= frm!ExCost


rs.Update

下一个varItm


rs.Close:设置rs = Nothing

dixie

I have some code that adds new records into a table for each ID in a list
box when a button on a form is clicked. This works fine. My problem now is
that I wish to be able to edit all the records for people whose ID is in the
list box. I made minor changes to the code (mainly replacing rs.AddNew with
rs.Edit)and it appears to be updating only the first record and then
overwriting that record with the next, etc until it runs out of ID''s in the
list box. In other words, it is stepping through the ID''s in the list box,
but not the records. Is there a trick to this? I have spent many hours
doing minor changes and still have the same problem.

The code follows (I have reduced the number of fields I am updating to keep
the size of the message down).

Dim intIndex As Integer

For intIndex = 0 To Me.lboBulkList.ListCount

Me.lbo.BulkList.Selected(intIndex) = True

Next intIndex

Dim db As Database

Dim rs As Recordset

Dim prm As Parameter

Dim qdf As QueryDef

Set db = CurrentDb()

Set qdf = db.QueryDefs("qryBulkEdit")

For Each prm In qdf.Parameters

prm.Value = Eval(prm.Name)

Next prm

Set rs = qdf.OpenRecordset(dbOpenDynaset)

Dim frm As Form

Dim ctl As Control

Dim varItm As Variant

Set frm = Forms!frmExcursions

Set ctl = frm! lboBulkList

For Each varItm In ctl.ItemsSelected

rs.Edit

rs(0) = Me. lboBulkList.ItemData(varItm)

rs!Date = frm!txtDate

rs!Faculty = frm!cboFaculty

rs!Course = frm!Course

rs!Cost = frm!ExCost

rs.Update

Next varItm

rs.Close: Set rs = Nothing
dixie

推荐答案

你是对的。编辑功能将编辑当前记录。您将需要从第一条记录开始并执行MoveNext,FindFirst或其他

过程,该过程将移动到您要编辑的记录,然后进行编辑。


-

Wayne Morgan

MS Access MVP

" dixie" <二**** @ dogmail.com>在消息中写道

新闻:VZ ***************** @ nnrp1.ozemail.com.au ...
You are correct. The Edit function will edit the current record. You will
need to start at the first record and do a MoveNext, FindFirst, or other
procedure that will move to the record you want to edit then do the edit.

--
Wayne Morgan
MS Access MVP
"dixie" <di****@dogmail.com> wrote in message
news:VZ*****************@nnrp1.ozemail.com.au...
我有一些代码,当单击表单上的按钮时,会在列表
框中为每个ID向表中添加新记录。这很好用。我现在的问题是
,我希望能够为列表框中ID为
的人编辑所有记录。我对代码做了一些小改动(主要是用rs.Edit替换rs.AddNew
),它似乎只更新了第一条记录,然后用下一条记录覆盖了那条记录等,直到它耗尽为止ID列表中的
换句话说,它是单步执行列表
框中的ID',而不是记录。这有诀窍吗?我花了很多时间做了一些小改动,但仍然遇到了同样的问题。

代码如下(我已将我更新的字段数减少到
保持大小消息下来。

Dim intIndex As Integer

对于intIndex = 0到Me.lboBulkList.ListCount

Me.lbo.BulkList.Selected (intIndex)=正确

下一个intIndex

Dim db作为数据库

Dim rs As Recordset

Dim prm As参数

Dim qdf作为QueryDef

设置db = CurrentDb()

设置qdf = db.QueryDefs(" qryBulkEdit")

每个prm in qdf.Parameters

prm.Value = Eval(prm.Name)

下一个prm

设置rs = qdf.OpenRecordset(dbOpenDynaset)

Dim frm As Form

Dim ctl As Control

Dim varItm As Variant

>设置frm = Forms!frmExcursions

设置ctl = frm! lboBulkList

每个变量在ctl.ItemsSelected

rs.Edit

rs(0)=我。 lboBulkList.ItemData(varItm)

rs!Date = frm!txtDate

rs!Faculty = frm!cboFaculty

rs!Course = frm!课程

rs!费用= frm!ExCost

rs.Update

下一个varItm

rs.Close:Set rs = Nothing

dixie
I have some code that adds new records into a table for each ID in a list
box when a button on a form is clicked. This works fine. My problem now is that I wish to be able to edit all the records for people whose ID is in the list box. I made minor changes to the code (mainly replacing rs.AddNew with rs.Edit)and it appears to be updating only the first record and then
overwriting that record with the next, etc until it runs out of ID''s in the list box. In other words, it is stepping through the ID''s in the list box, but not the records. Is there a trick to this? I have spent many hours
doing minor changes and still have the same problem.

The code follows (I have reduced the number of fields I am updating to keep the size of the message down).

Dim intIndex As Integer

For intIndex = 0 To Me.lboBulkList.ListCount

Me.lbo.BulkList.Selected(intIndex) = True

Next intIndex

Dim db As Database

Dim rs As Recordset

Dim prm As Parameter

Dim qdf As QueryDef

Set db = CurrentDb()

Set qdf = db.QueryDefs("qryBulkEdit")

For Each prm In qdf.Parameters

prm.Value = Eval(prm.Name)

Next prm

Set rs = qdf.OpenRecordset(dbOpenDynaset)

Dim frm As Form

Dim ctl As Control

Dim varItm As Variant

Set frm = Forms!frmExcursions

Set ctl = frm! lboBulkList

For Each varItm In ctl.ItemsSelected

rs.Edit

rs(0) = Me. lboBulkList.ItemData(varItm)

rs!Date = frm!txtDate

rs!Faculty = frm!cboFaculty

rs!Course = frm!Course

rs!Cost = frm!ExCost

rs.Update

Next varItm

rs.Close: Set rs = Nothing
dixie



Dixie,

你正在循环你的选择的项目,但你没有循环浏览

你的记录集(虽然循环记录集只能工作

如果记录与列表框的_exact_顺序相同)。 rs(0)是返回记录集中的第一条记录。当您循环浏览项目时,您需要编辑相同的记录。


相反,您要选择每个记录对每个选定的记录进行协调。 >
列表框项目,然后更新,或者,拉出所有记录,找到

与循环中的列表框项匹配,然后更新。


希望能让你入门。

Pat


" dixie" <二**** @ dogmail.com>在消息中写道

新闻:VZ ***************** @ nnrp1.ozemail.com.au ...
Dixie,
You are looping through your selected items, but you are not looping through
your recordset (although looping through the recordset is only going to work
if the records are in the _exact_ same order as your listbox). rs(0) is the
first record in the returned recordset. As you loop through the items, you
are editing the same record.

Instead, you want to select each record that cooresponds to each selected
listbox item and then update, or, pull all your records, find the one that
matches the listbox item in the loop and then update.

Hope that gets you started.
Pat

"dixie" <di****@dogmail.com> wrote in message
news:VZ*****************@nnrp1.ozemail.com.au...
我有一些代码,当单击表单上的按钮时,会在列表
框中为每个ID向表中添加新记录。这很好用。我现在的问题是
,我希望能够为列表框中ID为
的人编辑所有记录。我对代码做了一些小改动(主要是用rs.Edit替换rs.AddNew
),它似乎只更新了第一条记录,然后用下一条记录覆盖了那条记录等,直到它耗尽为止ID列表中的
换句话说,它是单步执行列表
框中的ID',而不是记录。这有诀窍吗?我花了很多时间做了一些小改动,但仍然遇到了同样的问题。

代码如下(我已将我更新的字段数减少到
保持大小消息下来。

Dim intIndex As Integer

对于intIndex = 0到Me.lboBulkList.ListCount

Me.lbo.BulkList.Selected (intIndex)=正确

下一个intIndex

Dim db作为数据库

Dim rs As Recordset

Dim prm As参数

Dim qdf作为QueryDef

设置db = CurrentDb()

设置qdf = db.QueryDefs(" qryBulkEdit")

每个prm in qdf.Parameters

prm.Value = Eval(prm.Name)

下一个prm

设置rs = qdf.OpenRecordset(dbOpenDynaset)

Dim frm As Form

Dim ctl As Control

Dim varItm As Variant

>设置frm = Forms!frmExcursions

设置ctl = frm! lboBulkList

每个变量在ctl.ItemsSelected

rs.Edit

rs(0)=我。 lboBulkList.ItemData(varItm)

rs!Date = frm!txtDate

rs!Faculty = frm!cboFaculty

rs!Course = frm!课程

rs!费用= frm!ExCost

rs.Update

下一个varItm

rs.Close:Set rs = Nothing

dixie
I have some code that adds new records into a table for each ID in a list
box when a button on a form is clicked. This works fine. My problem now is that I wish to be able to edit all the records for people whose ID is in the list box. I made minor changes to the code (mainly replacing rs.AddNew with rs.Edit)and it appears to be updating only the first record and then
overwriting that record with the next, etc until it runs out of ID''s in the list box. In other words, it is stepping through the ID''s in the list box, but not the records. Is there a trick to this? I have spent many hours
doing minor changes and still have the same problem.

The code follows (I have reduced the number of fields I am updating to keep the size of the message down).

Dim intIndex As Integer

For intIndex = 0 To Me.lboBulkList.ListCount

Me.lbo.BulkList.Selected(intIndex) = True

Next intIndex

Dim db As Database

Dim rs As Recordset

Dim prm As Parameter

Dim qdf As QueryDef

Set db = CurrentDb()

Set qdf = db.QueryDefs("qryBulkEdit")

For Each prm In qdf.Parameters

prm.Value = Eval(prm.Name)

Next prm

Set rs = qdf.OpenRecordset(dbOpenDynaset)

Dim frm As Form

Dim ctl As Control

Dim varItm As Variant

Set frm = Forms!frmExcursions

Set ctl = frm! lboBulkList

For Each varItm In ctl.ItemsSelected

rs.Edit

rs(0) = Me. lboBulkList.ItemData(varItm)

rs!Date = frm!txtDate

rs!Faculty = frm!cboFaculty

rs!Course = frm!Course

rs!Cost = frm!ExCost

rs.Update

Next varItm

rs.Close: Set rs = Nothing
dixie



OK Pat,我想的也是。你已经确认了我认为发生的事情。我的问题是我不知道如何结合以前的类型

代码根据列表框中的ID更新然后这种类型的

代码


带rs


如果rs.RecordCount> 0然后

.MoveFirst



。编辑

rs!费用= 0

。更新

.MoveNext

循环直到.EOF

结束如果

。关闭:设置rs =没什么


结束


我知道我需要逐步完成记录集中的记录并编辑

每个字段都根据我在列表框中的ID,但我似乎无法将这两个想法结合起来得到我想要的解决方案 - 这对每个人来说都是
$ b选择$ b来根据表格中的各种控件编辑他们的特定记录。


dixie


-----原始消息-----

来自:Pat < no ***** @ ihatespam.bum>

新闻组:comp.databases.ms-access

发送时间:2004年4月22日,星期四,上午11:38

主题:Re:rs.Edit而不是rs.AddNew

OK Pat, I thought as much. You have confirmed what I believed was
happening. My problem is that I don''t know how to combine the previous type
of code that updates according to an ID in a list box and then this type of
code

With rs

If rs.RecordCount > 0 Then
.MoveFirst
Do
.Edit
rs!Cost = 0
.Update
.MoveNext
Loop Until .EOF
End If
.Close: Set rs = Nothing

End With

I know that I need to step through the records in the recordset and edit
each field according to my ID in the list box, but I can''t seem to combine
the two ideas to get to the solution I want - which is for each person
selected to have their specific record edited according to a what is in
various controls on the form.

dixie

----- Original Message -----
From: "Pat" <no*****@ihatespam.bum>
Newsgroups: comp.databases.ms-access
Sent: Thursday, April 22, 2004 11:38 AM
Subject: Re: rs.Edit instead of rs.AddNew

Dixie,
您正在循环选择的项目,但是你没有通过你的记录集循环
(尽管如果记录与列表框的_exact_顺序相同,循环遍历记录集只会产生
)。 rs(0)是
返回记录集中的第一条记录。当您循环浏览项目时,
您正在编辑相同的记录。

相反,您想要选择每个记录,这些记录与每个选定的
列表框项目相关联,然后更新,或者,拉出你的所有记录,找到与循环中的列表框项匹配的那个,然后更新。

希望能让你开始。
Pat

dixie <二**** @ dogmail.com>在消息中写道
新闻:VZ ***************** @ nnrp1.ozemail.com.au ...
Dixie,
You are looping through your selected items, but you are not looping through your recordset (although looping through the recordset is only going to work if the records are in the _exact_ same order as your listbox). rs(0) is the first record in the returned recordset. As you loop through the items, you are editing the same record.

Instead, you want to select each record that cooresponds to each selected
listbox item and then update, or, pull all your records, find the one that
matches the listbox item in the loop and then update.

Hope that gets you started.
Pat

"dixie" <di****@dogmail.com> wrote in message
news:VZ*****************@nnrp1.ozemail.com.au...
我有一些代码当单击表单上的按钮时,会在
列表框中为每个ID向表中添加新记录。这很好用。我的问题
现在
I have some code that adds new records into a table for each ID in a list box when a button on a form is clicked. This works fine. My problem now
我希望能够编辑ID在
中的人的所有记录列表框。我对代码做了一些小改动(主要用
list box. I made minor changes to the code (mainly replacing rs.AddNew
rs.Edit替换rs.AddNew


with


)它似乎只更新了第一条记录然后
用下一个等覆盖该记录,直到它用完

rs.Edit)and it appears to be updating only the first record and then
overwriting that record with the next, etc until it runs out of ID''s in

中的ID'

列表框。换句话说,它是单步执行列表中的ID
list box. In other words, it is stepping through the ID''s in the list


框,

而不是记录。这有诀窍吗?我花了很多时间做了一些小改动,但仍然遇到了同样的问题。

代码如下(我已经减少了我要更新的字段数
but not the records. Is there a trick to this? I have spent many hours
doing minor changes and still have the same problem.

The code follows (I have reduced the number of fields I am updating to


保持

消息的大小。

Dim intIndex As Integer

对于intIndex = 0 To Me.lboBulkList.ListCount
Me.lbo.BulkList.Selected(intIndex)= True

下一个intIndex

Dim db作为数据库

Dim rs As记录集

Dim prm作为参数

Dim qdf作为QueryDef

设置db = CurrentDb()

设置qdf = db.QueryDefs(" qryBulkEdit")

每个prm in qdf.Parameters

prm.Value = Eval(prm.Name)

下一个prm

设置rs = qdf.OpenRecordset(dbOpenDynaset)

Dim frm As Form

Dim ctl As Control
Dim varItm As Variant

设置frm = Forms!frmExcursions

设置ctl = frm! lboBulkList

每个变量在ctl.ItemsSelected

rs.Edit

rs(0)=我。 lboBulkList.ItemData(varItm)

rs!Date = frm!txtDate

rs!Faculty = frm!cboFaculty

rs!Course = frm!课程

rs!费用= frm!ExCost

rs.Update

下一个varItm

rs.Close:Set rs = Nothing

dixie
the size of the message down).

Dim intIndex As Integer

For intIndex = 0 To Me.lboBulkList.ListCount

Me.lbo.BulkList.Selected(intIndex) = True

Next intIndex

Dim db As Database

Dim rs As Recordset

Dim prm As Parameter

Dim qdf As QueryDef

Set db = CurrentDb()

Set qdf = db.QueryDefs("qryBulkEdit")

For Each prm In qdf.Parameters

prm.Value = Eval(prm.Name)

Next prm

Set rs = qdf.OpenRecordset(dbOpenDynaset)

Dim frm As Form

Dim ctl As Control

Dim varItm As Variant

Set frm = Forms!frmExcursions

Set ctl = frm! lboBulkList

For Each varItm In ctl.ItemsSelected

rs.Edit

rs(0) = Me. lboBulkList.ItemData(varItm)

rs!Date = frm!txtDate

rs!Faculty = frm!cboFaculty

rs!Course = frm!Course

rs!Cost = frm!ExCost

rs.Update

Next varItm

rs.Close: Set rs = Nothing
dixie





" Pat" <无***** @ ihatespam.bum>在消息中写道

新闻:上午**************** @ fe1.texas.rr.com ... Dixie,
你正在循环通过你选择的项目,但是你没有通过你的记录集循环
(尽管如果记录与列表框的_exact_顺序相同,循环记录集只会产生
)。 rs(0)是
返回记录集中的第一条记录。当您循环浏览项目时,
您正在编辑相同的记录。

相反,您想要选择每个记录,这些记录与每个选定的
列表框项目相关联,然后更新,或者,拉出你的所有记录,找到与循环中的列表框项匹配的那个,然后更新。

希望能让你开始。
Pat

dixie <二**** @ dogmail.com>在消息中写道
新闻:VZ ***************** @ nnrp1.ozemail.com.au ...



"Pat" <no*****@ihatespam.bum> wrote in message
news:Am****************@fe1.texas.rr.com... Dixie,
You are looping through your selected items, but you are not looping through your recordset (although looping through the recordset is only going to work if the records are in the _exact_ same order as your listbox). rs(0) is the first record in the returned recordset. As you loop through the items, you are editing the same record.

Instead, you want to select each record that cooresponds to each selected
listbox item and then update, or, pull all your records, find the one that
matches the listbox item in the loop and then update.

Hope that gets you started.
Pat

"dixie" <di****@dogmail.com> wrote in message
news:VZ*****************@nnrp1.ozemail.com.au...

我有一些代码当单击表单上的按钮时,会在
I have some code that adds new records into a table for each ID in a


列表框中为每个ID向表中添加新记录。这很好用。我的问题

list box when a button on a form is clicked. This works fine. My problem



现在是


now is

我希望能够编辑ID为

<的人的所有记录br />


the

列表框。我对代码做了一些小改动(主要用
list box. I made minor changes to the code (mainly replacing rs.AddNew
rs.Edit替换rs.AddNew


with


)它似乎只更新了第一条记录然后
用下一个等覆盖该记录,直到它用完

rs.Edit)and it appears to be updating only the first record and then
overwriting that record with the next, etc until it runs out of ID''s in

中的ID'

列表框。换句话说,它是单步执行列表中的ID
list box. In other words, it is stepping through the ID''s in the list


框,

而不是记录。这有诀窍吗?我花了很多时间做了一些小改动,但仍然遇到了同样的问题。

代码如下(我已经减少了我要更新的字段数
but not the records. Is there a trick to this? I have spent many hours
doing minor changes and still have the same problem.

The code follows (I have reduced the number of fields I am updating to


保持

消息的大小。

Dim intIndex As Integer

对于intIndex = 0 To Me.lboBulkList.ListCount
Me.lbo.BulkList.Selected(intIndex)= True

下一个intIndex

Dim db作为数据库

Dim rs As记录集

Dim prm作为参数

Dim qdf作为QueryDef

设置db = CurrentDb()

设置qdf = db.QueryDefs(" qryBulkEdit")

每个prm in qdf.Parameters

prm.Value = Eval(prm.Name)

下一个prm

设置rs = qdf.OpenRecordset(dbOpenDynaset)

Dim frm As Form

Dim ctl As Control
Dim varItm As Variant

设置frm = Forms!frmExcursions

设置ctl = frm! lboBulkList

每个变量在ctl.ItemsSelected

rs.Edit

rs(0)=我。 lboBulkList.ItemData(varItm)

rs!Date = frm!txtDate

rs!Faculty = frm!cboFaculty

rs!Course = frm!课程

rs!费用= frm!ExCost

rs.Update

下一个varItm

rs.Close:Set rs = Nothing

dixie
the size of the message down).

Dim intIndex As Integer

For intIndex = 0 To Me.lboBulkList.ListCount

Me.lbo.BulkList.Selected(intIndex) = True

Next intIndex

Dim db As Database

Dim rs As Recordset

Dim prm As Parameter

Dim qdf As QueryDef

Set db = CurrentDb()

Set qdf = db.QueryDefs("qryBulkEdit")

For Each prm In qdf.Parameters

prm.Value = Eval(prm.Name)

Next prm

Set rs = qdf.OpenRecordset(dbOpenDynaset)

Dim frm As Form

Dim ctl As Control

Dim varItm As Variant

Set frm = Forms!frmExcursions

Set ctl = frm! lboBulkList

For Each varItm In ctl.ItemsSelected

rs.Edit

rs(0) = Me. lboBulkList.ItemData(varItm)

rs!Date = frm!txtDate

rs!Faculty = frm!cboFaculty

rs!Course = frm!Course

rs!Cost = frm!ExCost

rs.Update

Next varItm

rs.Close: Set rs = Nothing
dixie




这篇关于rs.Edit而不是rs.AddNew的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆