不要用这种绑定的,也不要用这个Grid和这个控件。改用其他的。 比如用msflexgrid来显示就漂亮的多,用ado.recordset来处理记录。 用SQL语句中的Insert,update,delete来更新记录。连接数据库模块代码,这是连接odbc的。 Public gServer As String Public gDSN As String Public Const uId = "sa" Public Const PWD = ""Public strConnect As String Public cnn As ADODB.Connection''''''''''' Public Sub ConnectDB() On Error GoTo Err_Link
Set cnn = New ADODB.Connection cnn.CursorLocation = adUseClient
If cnn.State <> 1 Or cnn.ConnectionString <> strConn Then If cnn.State = 1 Then cnn.Close End If
cnn.CursorLocation = adUseClient cnn.Open strConn End If
If cnn.State = 1 Then OpenDB = True Else OpenDB = False End If
Exit Function ErrHandler: msgbox Err.Description & "Error:OpenDB function" & vbCrLf OpenDB = False Err.Clear Exit Function End Function 窗体中的代码示范:On Error GoTo errTrap cnn.BeginTrans '开始处理事务 If StatusBar1.Panels(2).Text = "新增" And txtboxs(iGoodID).Text = "" Then strSQL = "INSERT INTO Goods values ('" & Trim(cboType.Text) & "', " _ & "'" & Trim(txtboxs(iName).Text) & "'," _ & "'" & Trim(txtboxs(iStyle).Text) & "'," _ & "'" & Trim(txtboxs(iDot).Text) & "'," _ & "'" & Trim(txtboxs(iWt).Text) & "'," _ & dfVntToDbl(txtboxs(iPrice).Text) & "," _ & "'" & Trim(txtboxs(iRe).Text) & "', " _ & dfVntToDbl(txtboxs(iPoint).Text) & ")" End If If StatusBar1.Panels(2).Text = "编辑" And txtboxs(iGoodID).Text <> "" Then
strSQL = "SELECT * FROM Goods WHERE GoodID=" & CLng(txtboxs(iGoodID).Text) Set Rs = cnn.Execute(strSQL)
If Not Rs.EOF Then lngGoodID = CLng(txtboxs(iGoodID).Text)
strSQL = "Update Goods set GoodName='" & Trim(txtboxs(iName).Text) & "'," _ & "Type='" & Trim(cboType.Text) & "', " _ & "Price=" & dfVntToDbl(txtboxs(iPrice).Text) & "," _ & "Style='" & Trim(txtboxs(iStyle).Text) & "'," _ & "Dot='" & Trim(txtboxs(iDot).Text) & "'," _ & "Weight='" & Trim(txtboxs(iWt).Text) & "'," _ & "Re='" & Trim(txtboxs(iRe).Text) & "', " _ & "Point=" & dfVntToDbl(txtboxs(iPoint).Text) & " " _ & "WHERE GoodID=" & lngGoodID Else If MsgBox("该记录已经被别的用户删除,是否做为新记录保存?", vbYesNo + vbQuestion, "提示信息") = vbCancel Then strSEM = showGoodInfo("") Exit Sub Else strSQL = "INSERT INTO Goods values ('" & Trim(cboType.Text) & "', " _ & "'" & Trim(txtboxs(iName).Text) & "'," _ & "'" & Trim(txtboxs(iStyle).Text) & "'," _ & "'" & Trim(txtboxs(iDot).Text) & "'," _ & "'" & Trim(txtboxs(iWt).Text) & "'," _ & dfVntToDbl(txtboxs(iPrice).Text) & "," _ & "'" & Trim(txtboxs(iRe).Text) & "', " _ & dfVntToDbl(txtboxs(iPoint).Text) & ")" End If End If End If If MsgBox("您确定要删除该信息吗?", vbYesNo + vbQuestion, "确认操作") = vbNo Then Exit Sub else strSQL = "DELETE FROM Goods where GoodID=" & CLng(txtboxs(iGoodID).Text) end if
cnn.Execute strSQL '执行事务
cnn.CommitTrans '确定执行事务
errTrap: cnn.RollbackTrans '加载网格显示的过程,这个是用的vsFlexGrid,但基本上与msFlexGrid是相同的。 Private Sub loadDbGrid() On Error GoTo errHandle Dim strSQL As String Dim i As Integer, intRow As Integer Dim RsTmp As New Recordset
intRow = 0
strSQL = "SELECT DataID, DataName, SqlType, SqlString " _ & "From TDataClear ORDER BY DataID "
Exit Sub errHandle: msgbox "加载数据网格时出现意外:" & vbCrLf & Err.Description End Sub希望这些能对你有所帮助。多去找些简单的例子来看看。绑定来修改这种方式是不可取的。
时间不多了,这是时候不能考虑换控件了。而且如果换了的话很多代码都有影响,改动比较大。昨天手动写代码用ado进行添加记录操作,但是datagrid绑定了的Recordset在鼠标指向空记录行录入数据字段的时候自动执行添加,在手动添加之后,datagrid绑定的添加操作无法取消掉。不断的提示添加操作失败。现在我想问的是这种绑定了的datagrid在自动监控添加记录的时候能不能屏蔽掉这样的功能?或者将自动添加取消? 在程序中加入了'定义recordest Dim WithEvents adoPrimaryRS As Recordset........ adoPrimaryRS.cancelupdate 执行到这句的时候报错 可能我对这个问题的描述不是很清楚,希望大家能看的明白。再次谢谢你们了
用一sstab 其中:按钮有增加、删除、刷新、查询一个是网格用来显示数据
一个是用来增加数据很多例子!自己网上找找
比如用msflexgrid来显示就漂亮的多,用ado.recordset来处理记录。
用SQL语句中的Insert,update,delete来更新记录。连接数据库模块代码,这是连接odbc的。
Public gServer As String
Public gDSN As String
Public Const uId = "sa"
Public Const PWD = ""Public strConnect As String
Public cnn As ADODB.Connection'''''''''''
Public Sub ConnectDB()
On Error GoTo Err_Link
Set cnn = New ADODB.Connection
cnn.CursorLocation = adUseClient
strConnect = "PROVIDER=MSDataShape.1;Data Source=" & gDSN _
& ";Data Provider=MSDASQL;user id=" & uId & ";pwd=" & PWD
cnn.Open strConnect
Dim Rs As ADODB.Recordset
Set Rs = cnn.Execute("select @@servername")
If Not Rs.EOF Then
gServer = Rs(0)
End If
Rs.Close
Set Rs = Nothing
Exit SubErr_Link:
MsgBox "Connect DB Error: " & Err.Description
End Sub'--------OPEN DATABASE-------------这是连接SQL数据库的
Public Function OpenDB() As Boolean
On Error GoTo ErrHandler
strConn = "Provider=SQLOLEDB.1; " _
& "Persist Security Info=False;User ID=sa; " _
& "Initial Catalog=" & gDSN & ";Data Source=" & gServer
Set cnn = New ADODB.Connection
cnn.CommandTimeout = 60000
If cnn.State <> 1 Or cnn.ConnectionString <> strConn Then
If cnn.State = 1 Then
cnn.Close
End If
cnn.CursorLocation = adUseClient
cnn.Open strConn
End If
If cnn.State = 1 Then
OpenDB = True
Else
OpenDB = False
End If
Exit Function
ErrHandler:
msgbox Err.Description & "Error:OpenDB function" & vbCrLf
OpenDB = False
Err.Clear
Exit Function
End Function
窗体中的代码示范:On Error GoTo errTrap
cnn.BeginTrans '开始处理事务
If StatusBar1.Panels(2).Text = "新增" And txtboxs(iGoodID).Text = "" Then
strSQL = "INSERT INTO Goods values ('" & Trim(cboType.Text) & "', " _
& "'" & Trim(txtboxs(iName).Text) & "'," _
& "'" & Trim(txtboxs(iStyle).Text) & "'," _
& "'" & Trim(txtboxs(iDot).Text) & "'," _
& "'" & Trim(txtboxs(iWt).Text) & "'," _
& dfVntToDbl(txtboxs(iPrice).Text) & "," _
& "'" & Trim(txtboxs(iRe).Text) & "', " _
& dfVntToDbl(txtboxs(iPoint).Text) & ")"
End If If StatusBar1.Panels(2).Text = "编辑" And txtboxs(iGoodID).Text <> "" Then
strSQL = "SELECT * FROM Goods WHERE GoodID=" & CLng(txtboxs(iGoodID).Text)
Set Rs = cnn.Execute(strSQL)
If Not Rs.EOF Then
lngGoodID = CLng(txtboxs(iGoodID).Text)
strSQL = "Update Goods set GoodName='" & Trim(txtboxs(iName).Text) & "'," _
& "Type='" & Trim(cboType.Text) & "', " _
& "Price=" & dfVntToDbl(txtboxs(iPrice).Text) & "," _
& "Style='" & Trim(txtboxs(iStyle).Text) & "'," _
& "Dot='" & Trim(txtboxs(iDot).Text) & "'," _
& "Weight='" & Trim(txtboxs(iWt).Text) & "'," _
& "Re='" & Trim(txtboxs(iRe).Text) & "', " _
& "Point=" & dfVntToDbl(txtboxs(iPoint).Text) & " " _
& "WHERE GoodID=" & lngGoodID
Else
If MsgBox("该记录已经被别的用户删除,是否做为新记录保存?", vbYesNo + vbQuestion, "提示信息") = vbCancel Then
strSEM = showGoodInfo("")
Exit Sub
Else
strSQL = "INSERT INTO Goods values ('" & Trim(cboType.Text) & "', " _
& "'" & Trim(txtboxs(iName).Text) & "'," _
& "'" & Trim(txtboxs(iStyle).Text) & "'," _
& "'" & Trim(txtboxs(iDot).Text) & "'," _
& "'" & Trim(txtboxs(iWt).Text) & "'," _
& dfVntToDbl(txtboxs(iPrice).Text) & "," _
& "'" & Trim(txtboxs(iRe).Text) & "', " _
& dfVntToDbl(txtboxs(iPoint).Text) & ")"
End If
End If
End If If MsgBox("您确定要删除该信息吗?", vbYesNo + vbQuestion, "确认操作") = vbNo Then
Exit Sub
else
strSQL = "DELETE FROM Goods where GoodID=" & CLng(txtboxs(iGoodID).Text)
end if
cnn.Execute strSQL '执行事务
cnn.CommitTrans '确定执行事务
errTrap:
cnn.RollbackTrans
'加载网格显示的过程,这个是用的vsFlexGrid,但基本上与msFlexGrid是相同的。
Private Sub loadDbGrid()
On Error GoTo errHandle
Dim strSQL As String
Dim i As Integer, intRow As Integer
Dim RsTmp As New Recordset
intRow = 0
strSQL = "SELECT DataID, DataName, SqlType, SqlString " _
& "From TDataClear ORDER BY DataID "
Set RsTmp = cnn.Execute(strSQL)
With vfgDbList
.Cols = RsTmp.Fields.Count + 1
.Rows = 1
.ColDataType(0) = flexDTBoolean
.Clear
Do While Not RsTmp.EOF
If intRow > 0 Then .AddItem ""
.TextMatrix(intRow, 1) = Trim(RsTmp("SqlType")): intColType = 1
.TextMatrix(intRow, 2) = Trim(RsTmp("SqlString")): intColSQL = 2
.TextMatrix(intRow, 3) = Trim(RsTmp("DataID")): intColDataID = 3
.TextMatrix(intRow, 4) = Trim(RsTmp("DataName"))
RsTmp.MoveNext
intRow = intRow + 1
Loop
.ColHidden(1) = True
.ColHidden(2) = True
.ColHidden(3) = True
End With
Exit Sub
errHandle:
msgbox "加载数据网格时出现意外:" & vbCrLf & Err.Description
End Sub希望这些能对你有所帮助。多去找些简单的例子来看看。绑定来修改这种方式是不可取的。
在程序中加入了'定义recordest
Dim WithEvents adoPrimaryRS As Recordset........
adoPrimaryRS.cancelupdate
执行到这句的时候报错
可能我对这个问题的描述不是很清楚,希望大家能看的明白。再次谢谢你们了
写代码的愚蠢的干活!!
adoPrimaryRS.CancelBatch
然后重新查询adoPrimaryRs重新填入grdDataGrid
不报错了,也cancel掉了添加记录集的操作
另外还有一个问题,另外开了贴的
帮忙去看看啊,谢谢大家了http://community.csdn.net/Expert/topic/4254/4254211.xml?temp=.9560205