如何以编程方式在 vb6 Datagrid 中 select 行

How to programmatically select rows in vb6 Datagrid

我有一个正在创建的 VB6 项目,我有一种方法可以从访问数据库中搜索和编辑学生。我需要编写程序代码,以便它可以 select 搜索到的学生并对其进行修改。我看到了这个网页,但它没有 select 学生,用户在进行编辑之前必须 select 它,https://support.microsoft.com/en-us/kb/195472。我如何对其进行编程,以便它可以 select 该特定行,以便用户可以编辑。 使用网站的代码:

Option Explicit
Dim connSearch As New ADODB.Connection
Dim rec As New ADODB.Recordset

Private Sub cmdSearch_Click()
connSearch.Close
connSearch.Open connstr
rec.CursorLocation = adUseClient

  If cmbSearch.Text = "Last Name" Then
    rec.Open "Select * From Table1 where [Last Name] like '" & txtSearch.Text & "'", connSearch, adOpenDynamic, adLockOptimistic
    frmStudents.cmdShowall.Enabled = True
    If rec.EOF Then
        MsgBox "No Student Found.", vbInformation, "Error"

    Else
        Set frmStudents.StudentTable.DataSource = rec
        MsgBox "Student found Successfully", vbInformation, "Success"
        ' Remove previously saved bookmark from collection
     If (frmStudents.StudentTable.SelBookmarks.Count <> 0) Then
        frmStudents.StudentTable.SelBookmarks.Remove 0
     End If
        ' Append your bookmark to the collection of selected rows
     frmStudents.StudentTable.SelBookmarks.Add rec.Bookmark
        frmSearch.Hide
    End If
  End If
End Sub

感谢您的帮助。 :)

编辑:将代码从评论移至此处

Private Sub Form_Load() 
   connSearch.Open connstr 'open the connection 
   frmStudents.Adodc1.ConnectionString = conn.connstr 
   Set frmStudents.StudentTable.DataSource = frmStudents.Adodc1 
End Sub

您必须使用记录集来填充 frmStudents.Adodc1 数据源,但出于某种原因您不想显示该代码。

然后在您尝试打开新记录集的代码中搜索学生并分配书签。那是行不通的。

如果您想显示所有学生 - 如示例所示 - 您需要单独保留数据源并在数据网格使用的同一记录集上进行查找。

我很难猜出那是什么,因为你没有向我展示表单的代码 - 我假设记录集在表单模块中是全局的 - 但也许不是?

没有这些信息,我只能猜测一些东西,希望翻译能奏效。

替换这个

rec.Open "Select * From Table1 where [Last Name] like '" & txtSearch.Text & "'", connSearch, adOpenDynamic, adLockOptimistic
frmStudents.cmdShowall.Enabled = True
If rec.EOF Then
    MsgBox "No Student Found.", vbInformation, "Error"

Else
    Set frmStudents.StudentTable.DataSource = rec
    MsgBox "Student found Successfully", vbInformation, "Success"
    ' Remove previously saved bookmark from collection
 If (frmStudents.StudentTable.SelBookmarks.Count <> 0) Then
    frmStudents.StudentTable.SelBookmarks.Remove 0
 End If
    ' Append your bookmark to the collection of selected rows
 frmStudents.StudentTable.SelBookmarks.Add rec.Bookmark
    frmSearch.Hide

有了这个

Dim varBookmark as Variant

With frmStudents.StudentTable 

    varBookMark = .Bookmark

   ' Remove previously saved bookmark from collection
   If (.SelBookmarks.Count <> 0) Then
        .SelBookmarks.Remove 0
   End If
   .Recordset.Find  "[Last Name] like '" & txtSearch.Text & "'"
   ' If Find method fails, notify user 
   ' If the search fails, the Recordset will point to either EOF or BOF. 
   If .Recordset.EOF or .Recordset.BOF Then
      Msgbox "No Student Found"
      ' Reset back to last selection
      .Recordset.Bookmark = varBookmark
   Else
      Msgbox "Student Found"
     .SelBookmarks.Add .Recordset.Bookmark
  Endif

End With

理想情况下,您只需使用分配给 frmStudents.Adodc1 而不是 frmStudents.Adodc1.Recordset 的记录集变量,但您尚未与我共享该变量,因此也许这对您有用