如何以编程方式选择vb6 Datagrid

时间:2016-07-10 13:48:39

标签: ms-access datagrid vb6 ado

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

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

1 个答案:

答案 0 :(得分:1)

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

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

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

我很难猜到那是什么,因为你没有向我展示Form的代码 - 我假设记录集是全局的,带有表单的模块 - 但可能不是?

如果没有这些信息,我可以猜到某些东西,希望翻译可能有用。

替换

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的记录集变量,但您还没有与我分享,所以这可能会对您有用