none
VB6 資料庫 的問題 RRS feed

  • 問題

  • 各位大哥好,我有一個資料庫,有三個欄位,分別卡號,工號,時間, 目前是用公號來查詢
    我有寫過一點vb.net然後再寫VB6
    請問各位大哥在Recordset這個物件是不是要一直開開關關的阿刷新Data Grid
    還是我的觀念錯了?
    另外在關閉的時候有時候ACCESS的檔案有時候還是沒有被釋放出來,是我用的R.close不對ㄇ?還是C.close

    Dim C As New ADODB.Connection
    Dim R As New ADODB.Recordset
        
    Dim StrCnn, StrSQL As String


    Private Sub Command1_Click()

    R.Close
    C.Close
    End
    End Sub

    Private Sub Command2_Click()
       
         Set C = CreateObject("ADODB.Connection")
         Set R = CreateObject("ADODB.Recordset")
         R.CursorLocation = 3
         StrCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Steve Lin\桌面\vb6access2000\cyt.mdb;Persist Security Info=False"
         C.Open StrCnn
         StrSQL = " SELECT * FROM cyttb "
         R.Open StrSQL, C, 1
         Set DataGrid1.DataSource = R
         DataGrid1.Refresh
        
         Set Text1.DataSource = R
         Text1.DataField = "卡號"
         Set Text2.DataSource = R
         Text2.DataField = "公號"
         Call showdata
         Command1.Enabled = True
         Command3.Enabled = True
         Command4.Enabled = True
         Command5.Enabled = True
         Command6.Enabled = True
        
         

          
    End Sub

    Private Sub Command3_Click()
    R.MoveFirst
    Call showdata
    End Sub

    Private Sub Command4_Click()
    R.MoveLast
    Call showdata
    End Sub
    Sub showdata()
    Label1 = R.AbsolutePosition & "/" & R.RecordCount
    End Sub

    Private Sub Command5_Click()

    R.MovePrevious
    If R.AbsolutePosition < 0 Then
    R.MoveNext

    End If
    Call showdata
    End Sub

    Private Sub Command6_Click()

    R.MoveNext
    If R.AbsolutePosition < 0 Then
    R.MovePrevious

    End If
    Call showdata
    End Sub

    Private Sub Command7_Click()
    R.Close
    R.Open "Select * From cyttb where[卡號]='" & Text3.Text & "'", C
    Set DataGrid1.DataSource = R
    DataGrid1.Refresh
    End Sub

    Private Sub DataGrid1_Click()
    Call showdata
    End Sub

     

    2009年5月26日 上午 07:31

解答

  • Recordset有變動,才需要做DataGrid.Refresh

    Recordset和Connection.Close之後,
    Set Redordset = Nothing
    Set Connection = Nothing
    Set DataGrid1.DataSource = ""

    試試囉...
    2009年5月26日 上午 08:19