Dim Cnn1 As New ADODB.Connection Dim rst As New ADODB.Recordset Dim cnnstr As String Dim SqlStr As StringPrivate Sub Command1_Click() If DataGrid1.Columns(DataGrid1.Col).Caption <> "姓名" Then MsgBox "请选择一个姓名", vbInformation + vbOKOnly, "提示" Else Cnn1.Execute "Delete * From 学生 Where 姓名='" & DataGrid1.Text & "'" If Cnn1.State = adStateOpen Then Cnn1.Close If rst.State = 1 Then rst.Close Cnn1.Open cnnstr With rst .CursorType = adOpenKeyset .LockType = adLockOptimistic .Open SqlStr, Cnn1, , , adCmdText End With Set DataGrid1.DataSource = rst DataGrid1.Caption = "共" & rst.RecordCount & "条记录" End If End Sub Private Sub Form_Load() cnnstr = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & App.Path & "\db1.mdb" SqlStr = "Select * From 学生" Cnn1.CursorLocation = adUseClient Cnn1.Open cnnstr With rst .CursorType = adOpenKeyset .LockType = adLockOptimistic .Open SqlStr, cnnstr, , , adCmdText End With Set DataGrid1.DataSource = rst DataGrid1.Caption = "共" & rst.RecordCount & "条记录" End Sub
你在你的数据库中设定一个自动编号的ID,把它设为主键后,然后在VB程序中dim c as integer后在datagrid_click里加c=rs!id不就把id给选出来了,应该不难你试试
Dim rst As New ADODB.Recordset
Dim cnnstr As String
Dim SqlStr As StringPrivate Sub Command1_Click()
If DataGrid1.Columns(DataGrid1.Col).Caption <> "姓名" Then
MsgBox "请选择一个姓名", vbInformation + vbOKOnly, "提示"
Else
Cnn1.Execute "Delete * From 学生 Where 姓名='" & DataGrid1.Text & "'"
If Cnn1.State = adStateOpen Then Cnn1.Close
If rst.State = 1 Then rst.Close
Cnn1.Open cnnstr
With rst
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open SqlStr, Cnn1, , , adCmdText
End With
Set DataGrid1.DataSource = rst
DataGrid1.Caption = "共" & rst.RecordCount & "条记录"
End If
End Sub
Private Sub Form_Load()
cnnstr = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & App.Path & "\db1.mdb"
SqlStr = "Select * From 学生"
Cnn1.CursorLocation = adUseClient
Cnn1.Open cnnstr
With rst
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open SqlStr, cnnstr, , , adCmdText
End With
Set DataGrid1.DataSource = rst
DataGrid1.Caption = "共" & rst.RecordCount & "条记录"
End Sub
'读出此行记录数据
第一列:From1.Grid.Columns(0).Text
第二列:From1.Grid.Columns(1).Text
第三列:From1.Grid.Columns(2).Text
......