简体   繁体   English

How can I use Excel to interact with MySQL and show result of an SQL query in a cell, after I enter SQL table id in another cell?

[英]How can I use Excel to interact with MySQL and show result of an SQL query in a cell, after I enter SQL table id in another cell?

I am trying to figure out how to use Excel as an interactive front end to MySQL.我试图弄清楚如何使用 Excel 作为 MySQL 的交互式前端。 It will be my first VBA experience with a database.这将是我第一次使用数据库的 VBA 体验。

My scenario is I want to enter an order number into one cell, and upon completing the input, I want an SQL query to be ran, like SELECT field1, field2 FROM table WHERE order_number =?我的场景是我想在一个单元格中输入一个订单号,完成输入后,我想运行一个 SQL 查询,例如SELECT field1, field2 FROM table WHERE order_number =? , and then display the return result of field1 in a cell. ,然后在单元格中显示field1的返回结果。 I may use field2 in other cells.我可以在其他单元格中使用field2

在此处输入图像描述

I see there is some code here that may be useful, but I don't know where to enter that code, and how to make that code work after I enter an order number into the cell.我看到这里有一些代码可能有用,但我不知道在哪里输入该代码,以及在单元格中输入订单号后如何使该代码工作。 I have already made an ODBC Driver connection to where I am able to connect to a database using Excel Database functions.我已经建立了一个 ODBC 驱动程序连接到我可以使用 Excel 数据库功能连接到数据库的位置。 I don't yet know how to use VBA do make a database connection or run interactive queries.我还不知道如何使用 VBA 进行数据库连接或运行交互式查询。

Can you help get me to the point where I can enter an order number in one cell, and see field1 show up in another cell, where field1 will be a value from an SQL query, like the above?你能帮我在一个单元格中输入订单号,然后看到field1出现在另一个单元格中,其中field1将是来自 SQL 查询的值,如上所示?

Put code on worksheet where you enter the order number.将代码放在输入订单号的工作表上。 This uses a DSN created using ODBC Data Source Administrator.这使用使用 ODBC 数据源管理器创建的 DSN。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim ar As Variant
    If Target.Address = "$B$2" Then
        ar = GetOrder(Target.Value)
        Range("B4") = ar(0)
        Range("B5") = ar(1)
    End If

End Sub

Function GetOrder(OrderNo As Long) As Variant

    Const CONN = "DSN=***;UID=***;PWD=***;"
 
    Const SQL = " SELECT Field1,Field2" & _
                " FROM table1 " & _
                " WHERE OrderNo = ?"
  
    Dim dbConn As ADODB.Connection, dbCmd As ADODB.Command
    Dim rs As ADODB.Recordset
    Dim param As ADODB.Parameter, n As Long

    Set dbConn = New ADODB.Connection
    dbConn.Open CONN

    Set dbCmd = New ADODB.Command
    With dbCmd
        .ActiveConnection = dbConn
        .CommandType = adCmdText
        .CommandText = SQL
        Set param = .CreateParameter("P1", adInteger, adParamInput, 0)
        .Parameters.Append param
    End With
    
    Set rs = dbCmd.Execute(n, OrderNo)
    If Not rs.EOF Then
       GetOrder = Array(rs(0).Value, rs(1).Value)
    Else
       GetOrder = Array("#N/A", "#N/A")
       MsgBox "Could not find " & OrderNo, vbExclamation, "Error"
    End If
   
    dbConn.Close

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM