简体   繁体   中英

Pass a value dynamically to SQL Server through VBA

Disclaimer: I am new to VBA.

I hope to pass the value in the SQL query (30881570) through a field on my Excel sheet. I have tried a few different things.

Private Sub cmdImport_Click()

    Call cmdClear_Click

    Dim conn As New ADODB.Connection, cmd As New ADODB.Command, rs As New ADODB.Recordset

    With conn
        .ConnectionString = _
            "Provider=SQLOLEDB; " & _
            "Data Source=PRGTAPPDBSWC019; " & _
            "Initial Catalog=DETEP;" & _
            "Integrated Security=SSPI;"
        .Open
    End With

    With cmd
        .ActiveConnection = conn
        .CommandText = "SELECT * FROM [dbo].[tbl_PMHeader] WHERE [PMHeader_PM_NUM] = '30881570'"
        .CommandType = adCmdText
    End With

    Set rs.Source = cmd
    rs.Open

    'Need this to populate header row, starting at specified Range
    For intColIndex = 0 To rs.Fields.Count - 1
        Range("B1").Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
    Next

    'This is where your data table will be copied to
    ActiveSheet.Range("B2").CopyFromRecordset rs

    Worksheets("Sheet1").Columns("B:BB").AutoFit

    Worksheets("Sheet1").Range("A25").Formula = "=COUNTA(B:B)-1"

End Sub

It looks like you're already passing that value as criteria for the query's WHERE statement.

If you're asking how to replace that with a value from a worksheet, here's one way:

.CommandText = "SELECT * FROM [dbo].[tbl_PMHeader] " & _
    "WHERE [PMHeader_PM_NUM] = '" & Sheets("mySheet").Range("A1") & "'"

...where your worksheet is named mySheet and the value is in cell A1 .

This is the simplest method, potentially fine for internal use by trusted parties, but if the value has any ' single-quotes in it, you will get an error.

Worst-case scenario, this method leaves you open to SQL Injection attacks. Depends on your needs (and whether this this is just a school assignment), you may be better of using a parameter query .


See Also:

Private Sub cmdImport_Click() Dim conn As New ADODB.Connection Dim cmd As New ADODB.Command Dim rs As New ADODB.Recordset Dim sqlStr As String With conn .ConnectionString = _ "Provider=SQLOLEDB; " & _ "Data Source=PRGTAPPDBSWC019; " & _ "Initial Catalog=DETEP;" & _ "Integrated Security=SSPI;" .Open End With orderno = Sheets("Sheet1").Range("A22") strSql = "SELECT * FROM [dbo].[tbl_PMHeader] " & _ "WHERE [PMHeader_PM_NUM] = " & orderno With cmd .ActiveConnection = conn .CommandText = strSql .CommandType = adCmdText End With 'Call cmdClear_Click Set rs.Source = cmd rs.Open 'Need this to populate header row, starting at specified Range For intColIndex = 0 To rs.Fields.Count - 1 Range("B1").Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name Next 'This is where your data table will be copied to ActiveSheet.Range("B2").CopyFromRecordset rs Worksheets("Sheet1").Columns("B:BB").AutoFit Worksheets("Sheet1").Range("A25").Formula = "=COUNTA(B:B)-1" End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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