Results 1 to 9 of 9

Thread: DataGrid Help

  1. #1
    Join Date
    Dec 2003
    Location
    Philippines
    Posts
    7

    DataGrid Help

    Hello.

    Im quite new in programming with DataGrid in VB6. I have a program which imports a text file (comma-delimited value format) to a table in access 2002. i have a prob with binding the table to a DataGrid. It display the records unpermanently.


    Dim adoRs As ADODB.Recordset

    Set adoRs = New ADODB.Recordset
    With adoRs
    Set .ActiveConnection = adoCn
    .CursorLocation = adUseServer
    .CursorType = adOpenStatic
    .LockType = adLockReadOnly
    .Open strSQL, , , , adCmdText
    End With

    'bind recordset to the datagrid for display
    With frmProcessAppForms.grdViewData
    Set .DataSource = adoRs
    .HoldFields
    .ScrollBars = dbgBoth
    .Refresh
    End With

    Is my DataGrid code right? Please Help.
    Thanks.

    Hotaru

  2. #2
    Join Date
    Jan 2004
    Location
    Sri Lanka
    Posts
    4
    technically there's nothing wrong in that code. But some questions do arise.
    Is the ADO connection already active at this stage of the code ?
    Also where is the Recordset's data retreval SQL ?

    You cant just open an emplty recordset and expect to see data in the grid.

    If the ADO Connection is active the folowing code should do the trick as long as it is in the same form as the Datagrid.

    Dim adoRs As ADODB.Recordset

    Set adoRs = New ADODB.Recordset
    adoRs.open "SELECT * FROM some-table-in-your-database",adoCn, adOpenStatic, adLockReadOnly

    'bind recordset to the datagrid for display
    grdViewData.DataSource = adoRs

  3. #3
    Join Date
    Dec 2003
    Location
    Philippines
    Posts
    7
    Hm. Check this whole code:

    Sub ViewData()

    On Error GoTo ErrHandler

    Dim adoCn As ADODB.Connection
    Dim adoRs As ADODB.Recordset
    Dim adoFld As ADODB.Field
    Dim strCn As String
    Dim strSQL As String

    lblStatus.Caption = "Status: Viewing Data... "
    txtViewData.Text = ""

    'need to use microsoft access driver
    'txtJetDriver.Text = {Micrsoft Access Driver (*.mdb)}
    'txtDatabase.Text = Databasename.mdb

    strCn = "Driver=" & txtJetDriver.Text & ";" & _
    "DBQ=" & App.Path & "\" & txtDatabase.Text & ";" & _
    "DefaultDir=" & App.Path & ";"

    Set adoCn = New ADODB.Connection
    adoCn.Open strCn '<< connection opened

    'SQL statement
    strSQL = "SELECT * FROM [" & txtTable.Text & "]"

    Set adoRs = New ADODB.Recordset
    adoRs.Open strSQL, adoCn, adOpenStatic, adLockReadOnly, adCmdText

    'edit: bind recordset to the datagrid for display
    Set grdViewData.DataSource = adoRs

    While Not adoRs.EOF
    For Each adoFld In adoRs.Fields
    txtViewData.Text = txtViewData.Text & adoFld.Value & " | "
    Next
    txtViewData.Text = txtViewData.Text & vbCrLf
    adoRs.MoveNext
    Wend

    GoTo ExitSub

    ErrHandler:
    lblStatus.Caption = "Status: Viewing Data - Error."
    MsgBox "Error: " & Err.Number & vbCrLf & Err.Description

    ExitSub:
    If Not (adoRs Is Nothing) And (Not adoRs.State = 0) Then
    lblStatus.Caption = "Status: Complete ... " & adoRs.RecordCount & " Records"
    adoRs.Close
    End If
    Set adoRs = Nothing
    adoCn.Close
    Set adoCn = Nothing

    End Sub

    I think there's nothing wrong with that code except to the DataGrid wherein if i tried to bind the table, about 2 secs it displays the table and it scrolls down automatically O___o and then the table will disappear. I think i should use other data-binding objects.
    Last edited by Hotaru_chan; 01-04-04 at 23:05.

  4. #4
    Join Date
    Jan 2004
    Location
    Sri Lanka
    Posts
    4
    all code there os ok excep this bit :

    While Not adoRs.EOF
    For Each adoFld In adoRs.Fields
    txtViewData.Text = txtViewData.Text & adoFld.Value & " | "
    Next
    txtViewData.Text = txtViewData.Text & vbCrLf
    adoRs.MoveNext
    Wend

    What are you trying to do in that while loop ?

  5. #5
    Join Date
    Dec 2003
    Location
    Philippines
    Posts
    7
    Oh. I have a Textbox that displays the imported text. Just to test if the text importing works.

  6. #6
    Join Date
    Dec 2003
    Location
    Philippines
    Posts
    7
    Here's the zipped proj.
    Last edited by Hotaru_chan; 01-05-04 at 05:27.

  7. #7
    Join Date
    Jan 2004
    Location
    Sri Lanka
    Posts
    4
    OK I have the project. you can take it off the forum now.

  8. #8
    Join Date
    Jan 2004
    Location
    Sri Lanka
    Posts
    4
    ok! found your problem.
    First of all if you're binding recodsets to a grid programmatically NEVER give the grid a layout at design time.
    Do that programmatically as well.

    Also because the grid is bound to the recordset, the moment you close the recodset the the grid loses it's data.

    I have attached the fixed project.
    Attached Files Attached Files

  9. #9
    Join Date
    Dec 2003
    Location
    Philippines
    Posts
    7
    Sou da ne... well, thanks for that.
    Im gonna fix my comp first before i DL that ^^

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •