Empty Datagrid problem in VB6
Recently, i encountered a problem; when I bind a recordset to datagr开发者_JAVA百科id ,and run the application the datagrid is not populated even though recordset has data
I use the following code
Option Explicit
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim recordset As New ADODB.recordset
Private Sub InitializeConnection()
Dim str As String
str = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" + App.Path + "\phonebook.mdb;" & _
"Persist Security Info=False"
conn.CursorLocation = adUseClient
conn.ConnectionString = str
conn.Open (conn.ConnectionString)
End Sub
Private Sub AbandonConnection()
If conn.State <> 0 Then
conn.Close
End If
End Sub
Private Sub Persons_Read()
Dim qry_all As String
' qry_all = "select * from person,web,phone Where web.personid = person.id And phone.personid = person.id"
qry_all = "SELECT * FROM person"
Call InitializeConnection
cmd.CommandText = qry_all
cmd.CommandType = adCmdText
Set cmd.ActiveConnection = conn
If conn.State = 1 Then
Set recordset = cmd.Execute()
End If
Call BindDatagrid
Call AbandonConnection
End Sub
Private Function Person_Add()
End Function
Private Function Person_Delete()
End Function
Private Function Person_Update()
End Function
Private Sub BindDatagrid()
Set dg_Persons.DataSource = recordset
dg_Persons.Refresh
End Sub
Private Sub cmd_Add_Click()
Person_Add
End Sub
Private Sub cmd_Delete_Click()
Person_Delete
End Sub
Private Sub cmd_Update_Click()
Person_Update
End Sub
Private Sub Form_Load()
Call Persons_Read
End Sub
Private Sub mnu_About_Click()
frm_About.Show
End Sub
Thanks in advance
For me, I was able to rectify the problem by adding this line to my code after setting the connection timeout
conn.CursorLocation = adUseClient
This solved my problem
It was solved by not closing connection right after the query !
精彩评论