【问题标题】:Searching particular data from SQL in VBA在 VBA 中从 SQL 中搜索特定数据
【发布时间】:2021-07-20 13:15:41
【问题描述】:

我想从 SQL 中搜索特定数据,然后将其与 Excel 中的单元格匹配。

Excel 中的单元格是 PART NUMBER,我想显示来自 SQL 的 Link 和 Critical 列中具有 PART NUMBER 的值。此外,如果 Excel 中的 PART NUMBER 与 SQL 中的 PART NUMBER 不匹配,则将其保留为 NULL。

这是 Excel:

这是我的 SQL

这是我当前失败的代码

Sub Run_Report()
Dim SQL As String
Dim Server_Name As String
Dim DatabaseName As String
Dim lrow As Long
Dim lcol As Long
Dim var As Variant
Dim partnum As Variant
Dim part As String
Dim i As Integer
Dim result As Long
var = "%" & Range("D3").Value & "%"

lrow = Cells(Rows.Count, 1).End(xlUp).row
partnum = Cells(i, 1).Value

Server_Name = "FSNGPZ\SQLEXPRESS"
DatabaseName = "QualityEngineeringDB"
part = "Select SQTLogbook.PART# from QualityEngineeringDB.dbo.SQTLogbook"
Call Connect_TTo_SQLServer(Server_Name, DatabaseName, SQL)
Application.CalculateFullRebuild

For i = 9 To lrow
    If Cells(i, 1) = part Then
Server_Name = "FSNGPZ\SQLEXPRESS"
DatabaseName = "QualityEngineeringDB"
SQL = "Select SQTLogbook.Link, SQTLogbook.Critical From QualityEngineeringDB.dbo.SQTLogbook where QualityEngineeringDB.dbo.SQTLogbook.PART# = '" & partnum & "';"
Call Connect_TTo_SQLServer(Server_Name, DatabaseName, SQL)
Application.CalculateFullRebuild
Set result = sqlProcess(SQL)
Columns(i, 4).Value = result(0)
End If
Next i
End Sub

【问题讨论】:

  • 失败时说什么?
  • 您的代码包含对您帖子中未包含的其他方法的调用(并且这些方法似乎负责实际的连接和查询),并且您没有解释运行时出现的问题代码,所以不清楚我们如何帮助解决这个问题。

标签: sql excel vba loops


【解决方案1】:

与服务器建立一次连接并使用参数化查询。

Option Explicit

Sub Run_Report()

    Const SERVER_NAME = "FSNGPZ\SQLEXPRESS"
    Const DB_NAME = "QualityEngineeringDB"

    Const SQL = " SELECT Link, Critical" & _
                " FROM SQTLogbook" & _
                " WHERE PART# = ?"

    Application.CalculateFullRebuild
    
    Dim conn As ADODB.Connection, cmd As ADODB.Command, rs As ADODB.Recordset
    Dim ws As Worksheet, lastrow As Long, i As Long, icount As Long
    
    Set conn = ConnectToServer(SERVER_NAME, DB_NAME)
    
    Set cmd = New ADODB.Command
    With cmd
        .ActiveConnection = conn
        .CommandType = adCmdText
        .CommandText = SQL
        .Parameters.Append .CreateParameter("P1", adVarChar, adParamInput, 50)
    End With
    
    Set ws = ActiveSheet
    lastrow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    For i = 9 To lastrow
        cmd.Parameters(0).Value = Trim(ws.Cells(i, "A")) ' part num
        Set rs = cmd.Execute()
        If rs.EOF Then
            ws.Cells(i, "D") = "NULL"
            ws.Cells(i, "E") = "NULL"
        Else
            ws.Cells(i, "D") = rs(0)
            ws.Cells(i, "E") = rs(1)
            icount = icount + 1
        End If
    Next i
    conn.Close
    MsgBox icount & " rows matched", vbInformation
   
End Sub

Function ConnectToServer(SERVER As String, DB As String) As ADODB.Connection
    Dim strConn As String
    strConn = "Provider=SQLOLEDB" & _
              ";Server=" & SERVER & _
              ";Database=" & DB & _
              ";Trusted_Connection=yes;"

    Set ConnectToServer = New ADODB.Connection
    ConnectToServer.Open strConn   
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多