|
本帖最后由 rflhjl 于 2013-11-8 16:52 编辑
六郎 发表于 2012-3-4 20:57
添加数据很正常,不存问题,可能你的excel软件有问题。这个不用教,一看便会。
你好,下面这段代码中的TextBox是6个,增加了就不能修改了。
Private Sub 修改记录_Click()
Dim rst As New ADODB.Recordset
Dim SQL$, temp$, i&, j&, s$, ok As Boolean
If TextBox1.Text = "" Or TextBox21.Text = "" Then Exit Sub '姓名为空则退出
For i = 0 To rs.Fields.Count - 1 '逐个字段
s = Me.Controls("TextBox" & i + 1).Text
If Len(s) = 0 Then
If i > 1 And i < rs.Fields.Count - 1 Then
s = "0"
ok = True
End If
End If
If Not IsNumeric(s) Then s = "'" & s & "'"
temp = temp & rs.Fields(i).Name & " = " & s & "," '更新记录字符串
Next i
On Error Resume Next
SQL = "update [sheet1$] set " & Left(temp, Len(temp) - 1) & " where " & rs.Fields(0).Name & "='" & ary(0) & "' and " & rs.Fields(i - 1).Name & "='" & ary(1) & "'" '姓名和工作部门都相同的视为同一个人
rst.Open SQL, cnn, adOpenKeyset, adLockOptimistic '执行更新
Set rst = Nothing
If ok Then Sheet1.UsedRange.Replace 0, "", xlWhole '清除sheet1工作表中的"0"
SQL = "select * from [sheet1$] "
If 模糊查询.Text = "" Then Call 显示数据(SQL) Else 模糊查询.Text = "" '刷新ListView1数据
Call 清空文本框
MsgBox "已在sheet1中将该记录修改!", vbInformation, "修改记录"
End Sub
或者:
Private Sub 修改记录_Click()
Dim rst As New ADODB.Recordset
Dim SQL$, temp$, i&, j&, s$, ok As Boolean
For i = 1 To rs.Fields.Count Step rs.Fields.Count - 1 '这里设置姓名和工作部门字段不能为空
If Me.Controls("TextBox" & i).Text = "" Then
MsgBox rs.Fields(i - 1).Name & "不能为空!", vbCritical
Me.Controls("TextBox" & i).SetFocus
Exit Sub
End If
Next
For i = 0 To rs.Fields.Count - 1 '逐个字段
s = Me.Controls("TextBox" & i + 1).Text
If Len(s) = 0 Then
If i > 1 And i < rs.Fields.Count - 1 Then
s = "0"
ok = True
End If
End If
If Not IsNumeric(s) Then s = "'" & s & "'"
temp = temp & rs.Fields(i).Name & " = " & s & "," '更新记录字符串
Next i
On Error Resume Next
SQL = "update [数据库$] set " & Left(temp, Len(temp) - 1) & " where " & rs.Fields(0).Name & "='" & ary(0) & "' and " & rs.Fields(i - 1).Name & "='" & ary(1) & "'" '姓名和工作部门都相同的视为同一个人
rst.Open SQL, cnn, adOpenKeyset, adLockOptimistic '执行更新
Set rst = Nothing
If ok Then Sheet2.UsedRange.Replace 0, "", xlWhole '清除数据库工作表中的"0"
SQL = "select * from [数据库$] "
If 模糊查询.Text = "" Then Call 显示数据(SQL) Else 模糊查询.Text = "" '刷新ListView1数据
Call 清空文本框
MsgBox "已在数据库中将该记录修改!", vbInformation, "修改记录"
End Sub
都不行,我是借用不懂,能帮帮吗,关键是对代码好我不明白?
|
|