Add the search content to the new Excel file in VB.

Source: Internet
Author: User

Private sub command4_click ()
Dim RS as new ADODB. recordset
Dim SQL as string
Dim connstr as string
Connstr = "provider = Microsoft. Jet. oledb.4.0;" & "Data Source =" & App. Path & "/data. mdb ;"
'*************************************** **************************************** ****************
Dim application as object
Dim workbook as object
Dim sheet as object
'*************************************** **************************************** ****************

Select case label12.caption
Case "all"
SQL = "select * from Members"
Conn. Open (connstr)
Rs. Open SQL, Conn, 1, 1

If Rs. recordcount = 0 then
Msgbox "no record"
Rs. Close
Conn. Close
Exit sub
Else

With commondialog1
. Dialogtitle = "enter an Excel name"
. Filter = "Microsoft Office Excel Workbook (*. xls) | *. xls"
. Showsave
End
If commondialog1.filename = "" Then exit sub
Set Application = Createobject ("Excel. application") 'create an Excel Object

Workbooks. Add
Activeworkbook. saveas (commondialog1.filename)
Set workbook = application. workbooks. Open (commondialog1.filename)

Set sheet = Workbook. Sheets. Add () 'to create a new form

For t = 1 to Rs. recordcount

Sheet. cells (T, 1). value = Rs. Fields (1). value' write data to excel
Sheet. cells (T, 2). value = Rs. Fields (2). value' write data to excel
Sheet. cells (T, 3). value = Rs. Fields (3). value' write data to excel
Sheet. cells (T, 4). value = Rs. Fields (4). value' write data to excel
Sheet. cells (T, 5). value = Rs. Fields (5). Value 'write data to excel
Sheet. cells (T, 6). value = Rs. Fields (6). value' write data to excel
Sheet. cells (T, 7). value = Rs. Fields (7). value' write data to excel
Rs. movenext
Next

Activeworkbook. Save save
Msgbox "exported"
'Application. Visible = true' EXCEL to make it visible

Workbook. Close
Set workbook = nothing
Set sheet = nothing
Set Application = nothing
Rs. Close
Conn. Close
End if
End sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.