我有一個VB6項目,我正在創建,我有一種方法可以從訪問數據庫中搜索和編輯學生。我需要對程序進行編碼,以便選擇被搜索和修改的學生。我看到了這個網頁,但它沒有選擇學生,用戶必須在進行編輯之前選擇它,https://support.microsoft.com/en-us/kb/195472。我如何編程,以便它可以選擇特定的行,以便用戶可以編輯。 使用網址代碼:如何以編程方式選擇vb6中的行Datagrid
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
這並不看起來就像這個例子。不應該將datagrid數據源設置爲整個記錄集 - 而不僅僅是您正在執行的匹配姓氏?你的表格加載代碼填充Datagrid在哪裏? – dbmitch
這看起來像是模塊的一部分,而不是表單 - 它是否位於同一個表單中?或者你只是向我們展示一些提取的代碼? – dbmitch
我有兩種形式,一種用於搜索學生,另一種用於數據網格和控件。我在問題中提供的代碼是來自搜索表單的摘錄。下一個註釋顯示了檢索數據網格和數據庫的表單加載代碼。 –