VB6如何在listview中编辑记录

时间:2015-09-03 06:34:43

标签: listview vb6

好的,所以我非常绝望,否则我不会提起这件事。请帮忙。 我的项目(库存控制)使用列表视图来显示Microsoft访问数据库中的记录。我已经想出了大多数控件来帮助我:创建新记录,删除记录等等。 但是我无法编辑/更新已有的记录。 当我尝试编辑/更新现有记录时,会创建一条新记录并完全替换表中的第一条记录,因此我最终会得到2条几乎相似的记录,并且表中的第一条记录被替换。

    Private Sub editrec()
    'This is the code that SHOULD update and overwrite the exsiting record but it doesnt work
    rsStocks!Itname = txts_2.Text
    rsStocks!ItPrice = txts_3.Text
    rsStocks!InStock = txts_4.Text
    rsStocks!OrderLvl = txts_5.Text
    rsStocks!Supplier = txts_6.Text
    rsStocks!ID = dlg_stk.Text
    rsStocks!Dull = "q"

    rsStocks.Update
    end sub

这就是editrec私有子被调用的方式。

Private Sub dlg_ok_Click()


If (txts_2.Text = "") Or (txts_3.Text = "") Or (txts_4.Text = "") Or (txts_5.Text = "") Or (txts_6.Text = "") Then
 aflag = True
 MsgBox ("please fill the appropriate fields!"), vbCritical
 End If


If (aflag = True) Or (bflag = True) Then
  'nothing is supposed to happen here mate
aflag = False
bflag = False
cflag = False
MsgBox ("Error occured"), vbCritical, "Error"
Exit Sub

Else
'editrec gets called here 
editrec
aflag = False
bflag = False
cflag = False

txts_2.Text = ""
txts_3.Text = ""
txts_4.Text = ""
txts_5.Text = ""
txts_6.Text = ""

MsgBox ("Please hit UPDATE to refresh list and unblock list"), vbCritical, "REFRESH"
End If
Exit Sub

End Sub 

连接:

Public conn_mod As ADODB.Connection
Public rsUsers As ADODB.Recordset
Public rsSupp As ADODB.Recordset
Public rsStocks As ADODB.Recordset

Public Sub Connect()
    Set conn_mod = New ADODB.Connection
    pathx = App.Path & "\Databases\main.mdb"
    conn_mod.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & pathx
conn_mod.Open
End Sub

    Public Sub openUsers()
        Set rsUsers = New ADODB.Recordset
         rsUsers.Open "select * from Users", conn_mod, adOpenStatic, adLockOptimistic
    End Sub

     Public Sub openSupp()
          Set rsSupp = New ADODB.Recordset
        rsSupp.Open "select * from Supp", conn_mod, adOpenStatic, adLockOptimistic
    End Sub

    Public Sub openStocks()
        Set rsStocks = New ADODB.Recordset
        rsStocks.Open "select * from Stocks", conn_mod, adOpenDynamic, adLockOptimistic
    End Sub

更多信息可以帮助您: .adodb连接工作正常

请帮助,iv一直试图计算如何工作超过5天:/我只是想编辑/更新并覆盖现有记录。

2 个答案:

答案 0 :(得分:0)

几乎可以肯定,您使用的是只读游标。我无法确定,因为您没有包含创建和打开rsStocks变量的代码。如果您不熟悉CursorType属性,则需要调查它。默认光标类型仅为forward,不可写。可写光标类型是键集和动态。

答案 1 :(得分:0)

@jac @BobRodes 谢谢你回答这个问题。我终于开始工作了。 你是对的,记录没有被正确选择进行编辑。 BobRodes感谢您告诉我有关动态部分的信息,它有所帮助。