2016-07-10 135 views
1

我有一個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 
+0

這並不看起來就像這個例子。不應該將datagrid數據源設置爲整個記錄集 - 而不僅僅是您正在執行的匹配姓氏?你的表格加載代碼填充Datagrid在哪裏? – dbmitch

+0

這看起來像是模塊的一部分,而不是表單 - 它是否位於同一個表單中?或者你只是向我們展示一些提取的代碼? – dbmitch

+0

我有兩種形式,一種用於搜索學生,另一種用於數據網格和控件。我在問題中提供的代碼是來自搜索表單的摘錄。下一個註釋顯示了檢索數據網格和數據庫的表單加載代碼。 –

回答

1

您必須使用記錄,以填補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記錄變量,但是你有沒有共享與我這麼說,也許這將適用於你

+1

這正是我需要感謝,但我需要驗證,如果姓氏不存在,它顯示msgbox說「用戶未找到」。現在,如果姓氏不存在,則表示** BOF或EOF爲True,或者當前記錄已被刪除。請求的操作需要當前記錄。** –

+0

嘗試上面的編輯,檢查NoMatch – dbmitch

+0

它不起作用,因爲** NoMatch **在記錄集後不存在 –

相關問題