Excel data as a data grid in vb6

Greetings to all who I am new to!

So, I was developing simple software with VB 6.0 (yes, I know a pretty old one). I had to do to manipulate an Excel Ms worksheet in a VB application. The actual task is to display the data or tables (all that we call it) presented in the Excel worksheet as a data grid.

I am using ADODB. Here is the code:

  Dim file_name As String
  Dim cn As New ADODB.Connection
  Dim rs As New ADODB.Recordset
 cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
 file_name & ";Extended Properties=Excel 12.0;"

 If cn.State = adStateOpen Then
     MsgBox "Excel File is Connected. ", , "Message"
     rs.CursorLocation = adUseClient
     rs.Open "SELECT * FROM [Sheet1$]", cn, 3, 3, 1 - adCmdText
     Set DataGrid1.DataSource = rs
     DataGrid1.Refresh
     DataGrid1.ReBind

  Else: MsgBox "Error: Excel File is not Connected. ", , "Message"
 End If
 rs.Close
 cn.Close

But I ran into a problem! Now excel data is displayed in the Data Grid Control. But immediately, the data is erased after the rs.close line is executed. I give a snapshot.

Please, help. Thank...

Here is the image: enter image description here

+4
source share
1 answer

... rs.close cn.close

0

Source: https://habr.com/ru/post/1584613/


All Articles