Home > Software engineering >  VB export excel object close do not allow the operation?
VB export excel object close do not allow the operation?

Time:09-30

Option Explicit
Dim the rs As New ADODB. You
Dim cn As New ADODB. Connection
Dim I, j, k As Integer
Dim xlapp As the Variant
Dim xlbook As the Variant
Dim xlsheet As the Variant

Private Sub Command1_Click ()
Adodc1. You. AddNew
End Sub

Private Sub Command2_Click ()
Adodc1. You. Update
Adodc1. Refresh
End Sub

Private Sub Command3_Click ()
The Set cn=Nothing
Set the rs=Nothing
Cn. Open "dojo.provide=Microsoft. Jet. The OLEDB. 4.0; Data Source="& amp; App. The Path & amp; "\ ceshi. MDB; Persist
"The Security Info=False
Rs. CursorLocation=adUseClient
Rs. Open "ceshi", cn, adOpenDynamic adLockOptimistic,
The Set DataGrid1.
the DataSource=rsIf the rs. RecordCount & gt; 0 Then
Command4. Enabled=True
End the If
End Sub

Private Sub Command4_Click ()
Adodc1. The ConnectionString="Provider=Microsoft. Jet. The OLEDB. 4.0; Data Source="+ App. Path +" \ ceshi MDB. Persist Security Info=False "
Adodc1.Com mandType=adCmdText
Adodc1. RecordSource="select * from ceshi"
- If the rs. RecordCount & lt;=0 Then
MsgBox "no output data, please select data!"
The Exit Sub
The Else
The Set xlapp=CreateObject (" excle. Application ")
The Set xlbook=xlapp. Workbooks. Add
The Set xlsheet=xlbook. Worksheets (1)
Xlapp. Visible=True
End the If
On the Error Resume Next
If Err. Number & lt;> 0 Then
The Set xlapp=CreateObject (" excle. Application ")
The Set xlbook=xlapp. Workbooks. Add
The Set xlsheet=xlbook. Activesheet
For k=1 To DataGrid1. Columns (k - 1). The Caption
Next k
For I=1 To rs. RecordCount + 1
For j=0 To DataGrid1 Columns. The Count
Xlsheet. Cells (I + 1, j + 1)=rs (j)
Next j
Rs. MoveNext
Next I
End the If
End Sub

Private Sub Form_Load ()
Adodc1. The ConnectionString="Provider=Microsoft. Jet. The OLEDB. 4.0; Data Source="+ App. Path +" \ ceshi MDB. Persist Security Info=False "
Adodc1.Com mandType=adCmdText
Adodc1. RecordSource="select * from ceshi"
The Set Text1. The DataSource=Adodc1
Text1. DataField="name"
End Sub



This is all my code, ACCESS database name only, I want to use EXCEL export, hint at ->
Thank you for your help!

CodePudding user response:

Click the Command3 ah,
  • Related