Hi all,
I'm trying to populate a datagrid as part of a search function for my application. I've tried running my code, which gets executed with no errors but the contents ain't showing up in the datagrid. Please fix this for me :)

Private Sub Search_Click()
    Dim dbuser As String, password As String, dbname As String, hostname As String
    Dim sql As String
    Dim count As Integer
    count = 0
   
    Open App.Path & "\settings.txt" For Input As #1
        Line Input #1, dbuser
        Line Input #1, password
        Line Input #1, dbname
        Line Input #1, hostname
    Close #1
    
    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    conn.Open "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=" & hostname & ";" & "DATABASE=" & dbname & ";" & "UID=" & dbuser & ";" & "PWD=" & password & ";OPTION=3"
    rs.CursorLocation = adOpenKeyset
    
    sql = "SELECT * FROM tblorders"
        
    Set datagrid1.DataSource = rs
    rs.Open sql, conn
    
    Do Until rs.EOF
        count = count + 1
        rs.MoveNext
        datagrid1.Refresh
    Loop
    
    Text1.Text = "Finshed"
    Label4.Caption = count
    rs.Close
End Sub

Let me know if anyone needs anymore information :)

Cheers,

I am not sure... But why don't you try removing the line:

rs.Close

and check whether it is working or not... :)

Lines 22 and 23 need to be reversed. First open the recordset, then set the recordsource, and finally do the refresh, which reminds me. No need to do the refresh in the loop. After that then you might also want to follow AK's advice above...

Good Luck

As above, swop your code around, FIRST open RS and then SET GRID Property. If you are not using anything else from RS, you can close it now. Remember that to use the refresh property afterwards will also result in an error because you have closed RS.

Set datagrid1.DataSource = rs
    rs.Open sql, conn
'Should read ----
rs.Open sql, conn
Set datagrid1.DataSource = rs
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.