Excel uses cell values to query the MySQL database and return data to the appropriate cells

Source: Internet
Author: User

Dim MyConn as Object
Private Sub Connectdb ()
Set myconn = CreateObject ("ADODB. Connection ")-----> Create Connection object
Dim Str as String
str = "Driver={mysql ODBC 5.3 Unicode DRIVER};" & _----> Join statement ODBC Driver
"Server=localhost;" & _----> Host

"port=3306;" & _-----> Port number

"Database=test;" & _-----> Specify Database
"User=read;" & _------> Username
"Password=read;" & _------> Password
"Option=3"
MyConn.Open str----> Open connection


Dim SQL as String
Dim I as Integer

Dim field as String

-----------Cycle begins

For I=1 to 500

field = Worksheets ("Sheet4"). Cells (i, 5)---> Get query criteria from cells

sql = "Select Delivery_no from tb_table where tb_table.order_id = '" & Field & "'"-----> Stitching SQL


Sheets ("Sheet1"). Cells (i, 3). CopyFromRecordset myconn.execute (SQL)-----> Assign Query Results to Sheet1 3rd column

Next I

End of-------------cycle


Myconn.close-----> Close Connection

End Sub

Excel uses cell values to query the MySQL database and return data to the appropriate cells

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.