ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: fecmen

[求助]ADO写入数据到ACCESS(圆满解决)-已添加VB版的Listview隔行显示不同颜色功能

[复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2008-8-30 11:12 | 显示全部楼层
Sub 修改科目(ByVal dex As Integer)
    Dim SQL As String
    Dim CNN As New ADODB.Connection
    CNN.Open "provider=Microsoft.jet.OLEDB.4.0;data source=" _
            & ThisWorkbook.Path & "\凭证数据库.mdb"
    SQL = "Update 科目表 Set 类型='" & ComboBox1.Text & "',"
    SQL = SQL & " 科目编码='" & TextBox3.Text & "',"
    SQL = SQL & " 总账科目='" & TextBox4.Text & "',"
    SQL = SQL & " 明细科目='" & TextBox5.Text & "',"
    SQL = SQL & " 方向='" & TextBox6.Text & "',"
    SQL = SQL & " 期初金额=" & Val(TextBox7.Text) & ""
    SQL = SQL & " Where 科目编码='" & Label8.Caption & "'"
    CNN.Execute SQL                 '或写成   CNN.Execute SQL, 1, 1
    CNN.Close
    Set CNN = Nothing
    With ListView1.ListItems(dex)       《 -----为何以下代码向listview中更新数据不成功呢?
       .Text = ComboBox1.Text
       .SubItems(1) = Me.TextBox3.Text
       .SubItems(2) = Me.TextBox4.Text
       .SubItems(3) = Me.TextBox5.Text
       .SubItems(4) = Me.TextBox6.Text
       .SubItems(5) = Me.TextBox7.Text
    End With
End Sub

TA的精华主题

TA的得分主题

发表于 2008-8-30 11:26 | 显示全部楼层

Sub 修改科目(ByVal dex As Integer)
    Dim SQL As String
    Dim CNN As New ADODB.Connection
    CNN.Open "provider=Microsoft.jet.OLEDB.4.0;data source=" _
            & ThisWorkbook.Path & "\凭证数据库.mdb"
    SQL = "Update 科目表 Set 类型='" & ComboBox1.Text & "',"
    SQL = SQL & " 科目编码='" & TextBox3.Text & "',"
    SQL = SQL & " 总账科目='" & TextBox4.Text & "',"
    SQL = SQL & " 明细科目='" & TextBox5.Text & "',"
    SQL = SQL & " 方向='" & TextBox6.Text & "',"
    SQL = SQL & " 期初金额=" & CCur(TextBox7.Text) & ""
    SQL = SQL & " Where 科目编码='" & Label8.Caption & "'"
    CNN.Execute SQL
    CNN.Close
    Set CNN = Nothing
   
End Sub

Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem)
  On Error Resume Next
  publicdex = Item.Index
  ComboBox1 = ListView1.SelectedItem.Text
  TextBox3 = ListView1.SelectedItem.SubItems(1)
  TextBox4 = ListView1.SelectedItem.SubItems(2)
  TextBox5 = ListView1.SelectedItem.SubItems(3)
  TextBox6 = ListView1.SelectedItem.SubItems(4)
  TextBox7 = ListView1.SelectedItem.SubItems(5)
  TextBox8 = ListView1.SelectedItem.SubItems(6)
  Label8.Caption = ListView1.SelectedItem.SubItems(1)
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2008-8-30 11:32 | 显示全部楼层
QUOTE:
以下是引用lanyuu在2008-8-30 11:26:03的发言:

Sub 修改科目(ByVal dex As Integer)
    Dim SQL As String
    Dim CNN As New ADODB.Connection
    CNN.Open "provider=Microsoft.jet.OLEDB.4.0;data source=" _
            & ThisWorkbook.Path & "\凭证数据库.mdb"
    SQL = "Update 科目表 Set 类型='" & ComboBox1.Text & "',"
    SQL = SQL & " 科目编码='" & TextBox3.Text & "',"
    SQL = SQL & " 总账科目='" & TextBox4.Text & "',"
    SQL = SQL & " 明细科目='" & TextBox5.Text & "',"
    SQL = SQL & " 方向='" & TextBox6.Text & "',"
    SQL = SQL & " 期初金额=" & CCur(TextBox7.Text) & ""
    SQL = SQL & " Where 科目编码='" & Label8.Caption & "'"
    CNN.Execute SQL
    CNN.Close
    Set CNN = Nothing
   
End Sub

Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem)
  On Error Resume Next
  publicdex = Item.Index
  ComboBox1 = ListView1.SelectedItem.Text
  TextBox3 = ListView1.SelectedItem.SubItems(1)
  TextBox4 = ListView1.SelectedItem.SubItems(2)
  TextBox5 = ListView1.SelectedItem.SubItems(3)
  TextBox6 = ListView1.SelectedItem.SubItems(4)
  TextBox7 = ListView1.SelectedItem.SubItems(5)
  TextBox8 = ListView1.SelectedItem.SubItems(6)
  Label8.Caption = ListView1.SelectedItem.SubItems(1)
End Sub

谢谢啦!

SQL = SQL & " 期初金额=" & CCur(TextBox7.Text) & ""    

也可这样  SQL = SQL & " 期初金额=" & Val(Replace(TextBox7.Text, ",", "")) & ""

再次表示感谢!

[此贴子已经被作者于2008-9-3 15:16:40编辑过]

TA的精华主题

TA的得分主题

 楼主| 发表于 2008-8-30 11:40 | 显示全部楼层

   With ListView1.ListItems(dex)       《 -----为何以下代码向listview中更新数据不成功呢?
       .Text = ComboBox1.Text
       .SubItems(1) = Me.TextBox3.Text
       .SubItems(2) = Me.TextBox4.Text
       .SubItems(3) = Me.TextBox5.Text
       .SubItems(4) = Me.TextBox6.Text
       .SubItems(5) = Me.TextBox7.Text
    End With

上面代码是否有问题呢?

TA的精华主题

TA的得分主题

 楼主| 发表于 2008-8-30 16:49 | 显示全部楼层
QUOTE:
以下是引用office2008在2008-8-30 10:46:31的发言:

呵呵,这不是Bug

 CNN.Execute SQL

 改成

 CNN.Execute SQL, 1, 1   '立即更新

请问office2008在上面的SQL后面加上1,1是什么意思呢?

TA的精华主题

TA的得分主题

发表于 2008-8-30 17:10 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

打开 和 锁定的方式

ado帮助中有

TA的精华主题

TA的得分主题

 楼主| 发表于 2008-8-30 17:29 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2008-8-30 21:20 | 显示全部楼层

把下面的代码全部替换你的代码,基本所有问题都已解决

Dim CNN As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim NE As String, Exec As String, publicdex As Integer

Private Sub UserForm_Initialize()
  'On Error Resume Next
  ComboBox1.List = Array("资产", "负债", "权益", "成本", "损益")
  With ListView1
      .View = lvwReport
      .Gridlines = True
      .FullRowSelect = True
      .ColumnHeaders.Add , , "类型", 28
      .ColumnHeaders.Add , , "科目编码", 55
      .ColumnHeaders.Add , , "总账科目", 90
      .ColumnHeaders.Add , , "明细科目", 95
      .ColumnHeaders.Add , , "方向", 28, lvwColumnCenter
      .ColumnHeaders.Add , , "期初金额", 70, lvwColumnRight
      .ColumnHeaders.Add , , "现金编码", 45, lvwColumnCenter
  End With
  ListView1.LabelEdit = 0    '内容不可修改
  ComboBox1 = "资产"
  'OptionButton1.Value = True
  TextBox3.SetFocus
End Sub

Private Sub ComboBox1_Change()   '自动搜索数据
  Call 查询科目
  Call 清空文本
  TextBox3.SetFocus
End Sub

Private Sub CommandButton1_Click()   '新增数据
  Dim s$, i%
  For i = 1 To ListView1.ListItems.Count
   s = s & "  " & ListView1.ListItems(i).SubItems(1)
  Next
  If ComboBox1 = "" Or TextBox3 = "" Or TextBox4 = "" Or TextBox6 = "" Then
     MsgBox "数据不完整,请核对后录入!", vbCritical, "凭证处理系统": Exit Sub
  ElseIf InStr(s, TextBox3.Text) Then
  MsgBox "科目已存在", vbCritical, "凭证处理系统": Call 清空文本: Exit Sub
  Else
     If MsgBox("是否确认新增科目的数据?", vbYesNo + vbQuestion, "凭证处理系统") = vbNo Then
        Call 清空文本: Exit Sub
     End If
     Call 输入科目
     Call 查询科目
     Call 清空文本
     MsgBox "会计科目新增完毕!", vbInformation, "凭证处理系统"
  End If
  TextBox3.SetFocus
End Sub

Private Sub CommandButton2_Click()   '修改数据
  Application.ScreenUpdating = False
  If ComboBox1 = "" Or TextBox3 = "" Or TextBox4 = "" Or TextBox6 = "" Then
     MsgBox "数据不完整,请核对后修改!", vbCritical, "凭证处理系统": Exit Sub
  Else
     If MsgBox("是否确认修改科目的数据?", vbYesNo + vbQuestion, "凭证处理系统") = vbYes Then
       Call 修改科目
       Call 查询科目
       Call 清空文本
     Else
      Call 清空文本: Exit Sub        '
     End If
     MsgBox "会计科目修改完毕!", vbInformation, "凭证处理系统"
  End If
  Application.ScreenUpdating = True
  TextBox3.SetFocus
End Sub

Private Sub CommandButton3_Click()   '删除数据
  If ComboBox1 = "" Or TextBox3 = "" Or TextBox4 = "" Or TextBox6 = "" Then
     MsgBox "数据不完整,请核对后删除!", vbCritical, "凭证处理系统": Exit Sub
  Else
     If MsgBox("是否确认删除科目的数据?", vbYesNo + vbQuestion, "凭证处理系统") = vbNo Then
        Call 清空文本: Exit Sub
     End If
     Call 删除科目
     Call 查询科目
     Call 清空文本
     MsgBox "会计科目删除完毕!", vbInformation, "凭证处理系统"
  End If
  TextBox3.SetFocus
End Sub

Private Sub CommandButton4_Click()
  UserForm7.Show
End Sub

Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem)
  'On Error Resume Next
  publicdex = Item.Index
  ComboBox1 = ListView1.SelectedItem.Text
  TextBox3 = ListView1.SelectedItem.SubItems(1)
  TextBox4 = ListView1.SelectedItem.SubItems(2)
  TextBox5 = ListView1.SelectedItem.SubItems(3)
  TextBox6 = ListView1.SelectedItem.SubItems(4)
  TextBox7 = ListView1.SelectedItem.SubItems(5)
  'TextBox8 = ListView1.SelectedItem.SubItems(6)
  Label8.Caption = ListView1.SelectedItem.SubItems(1)
End Sub

Sub 输入科目()
  'On Error Resume Next
  CNN.Open "provider=Microsoft.jet.OLEDB.4.0;data source=" _
            & ThisWorkbook.Path & "\凭证数据库.mdb"
  SQL = "Select * From 科目表"
  rst.Open SQL, CNN, adOpenKeyset, adLockOptimistic
  rst.AddNew
  rst.Fields("类型") = ComboBox1.Text
  rst.Fields("科目编码") = IIf(TextBox3.Text = "", Null, TextBox3.Text)
  rst.Fields("总账科目") = IIf(TextBox4.Text = "", Null, TextBox4.Text)
  rst.Fields("明细科目") = IIf(TextBox5.Text = "", Null, TextBox5.Text)
  rst.Fields("方向") = IIf(TextBox6.Text = "", Null, TextBox6.Text)
  rst.Fields("期初金额") = IIf(TextBox7.Text = "", Null, TextBox7.Text)
  rst.Update
  rst.Close
  Set rst = Nothing
End Sub

Sub 查询科目()
  On Error Resume Next
  CNN.Open "provider=Microsoft.jet.OLEDB.4.0;data source=" _
            & ThisWorkbook.Path & "\凭证数据库.mdb"
  SQL = "Select * From 科目表 Where 类型='" & ComboBox1.Text & "' Order by 科目编码"
  rst.Open SQL, CNN, adOpenKeyset, adLockReadOnly
  ListView1.ListItems.Clear
  For i = 1 To rst.RecordCount
      ListView1.ListItems.Add , , rst.Fields("类型")
      ListView1.ListItems(i).SubItems(1) = rst.Fields("科目编码")
      ListView1.ListItems(i).SubItems(2) = rst.Fields("总账科目")
      ListView1.ListItems(i).SubItems(3) = IIf(IsNull(rst.Fields("明细科目")), "", rst.Fields("明细科目"))
      ListView1.ListItems(i).SubItems(4) = rst.Fields("方向")
      ListView1.ListItems(i).SubItems(5) = Format(rst.Fields("期初金额"), "#,##0.00")
      rst.MoveNext
  Next i
  rst.Close
  Set rst = Nothing
  CNN.Close: Set CNN = Nothing
End Sub

Sub 修改科目()
    Dim SQL As String
    Dim CNN As New ADODB.Connection
    CNN.Open "provider=Microsoft.jet.OLEDB.4.0;data source=" _
            & ThisWorkbook.Path & "\凭证数据库.mdb"
    SQL = "Update 科目表 Set 类型='" & Me.ComboBox1.Text & "',"
    SQL = SQL & " 科目编码='" & Me.TextBox3.Text & "',"
    SQL = SQL & " 总账科目='" & Me.TextBox4.Text & "',"
    SQL = SQL & " 明细科目='" & Me.TextBox5.Text & "',"
    SQL = SQL & " 方向='" & Me.TextBox6.Text & "',"
    SQL = SQL & " 期初金额=" & Val(TextBox7.Text) & ""
    SQL = SQL & " Where 科目编码='" & Me.Label8.Caption & "'"
    CNN.Execute SQL
    CNN.Close
    Set CNN = Nothing
    With ListView1.ListItems(publicdex)
       .Text = ComboBox1.Text
       .SubItems(1) = Me.TextBox3.Text
       .SubItems(2) = Me.TextBox4.Text
       .SubItems(3) = Me.TextBox5.Text
       .SubItems(4) = Me.TextBox6.Text
       .SubItems(5) = Me.TextBox7.Text
    End With
End Sub

Sub 删除科目()
  'On Error Resume Next
  CNN.Open "provider=Microsoft.jet.OLEDB.4.0;data source=" _
            & ThisWorkbook.Path & "\凭证数据库.mdb"
  SQL = "Delete From 科目表 Where 科目编码 = '" & TextBox3.Text & "'"
  CNN.Execute (SQL)
End Sub

Sub 清空文本()
  'On Error Resume Next
  TextBox3 = "": TextBox4 = "": TextBox5 = ""
  TextBox6 = "": TextBox7 = "":: TextBox8 = "": 'Label8.Caption = ""
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2008-8-30 23:27 | 显示全部楼层
谢谢office2008!辛苦啦,我好好看看。

TA的精华主题

TA的得分主题

 楼主| 发表于 2008-8-31 13:14 | 显示全部楼层

 For i = 1 To rst.RecordCount
      ListView1.ListItems.Add , , rst.Fields("类型")
      ListView1.ListItems(i).SubItems(1) = rst.Fields("科目编码")
      ListView1.ListItems(i).SubItems(2) = rst.Fields("总账科目")
      ListView1.ListItems(i).SubItems(3) = IIf(IsNull(rst.Fields("明细科目")), "", rst.Fields("明细科目"))
      ListView1.ListItems(i).SubItems(4) = rst.Fields("方向")
      ListView1.ListItems(i).SubItems(5) = Format(rst.Fields("期初金额"), "#,##0.00")
      rst.MoveNext
  Next i
  rst.Close
  Set rst = Nothing
  CNN.Close: Set CNN = Nothing  -------》看了半天原来之前缺少此句呀,导致打开的连接不能编辑的错误!!

谢谢office2008!敬仰之情犹如滔滔江水!我的ADO技术在你帮助下长进不少!

X6D2FvXG.rar (36.76 KB, 下载次数: 480)       更新后的文件
[此贴子已经被作者于2008-8-31 13:18:03编辑过]
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-22 20:39 , Processed in 0.039025 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表