Z
Z
zhaar2019-04-22 14:23:37
SQL
zhaar, 2019-04-22 14:23:37

How to pass the contents of an Excel cell to a stored procedure (SQL query)?

Actually the task is quite "simple", but since. I'm not familiar with vb scripts, so I'm asking for help.
Excel workbook has 2 sheets. The parameters are specified on the first sheet, the result of the stored procedure execution from the SQL server is displayed on the second sheet.
How to make it so that when a button is pressed on an Excel sheet (cells, etc.), it executes a request to execute a stored procedure (namely, XP, a direct sql data query is impossible), the script looks at what is indicated on the first sheet in cell A1 and passes the specified value to procedure call?
The text may contain spaces, so the entered value must be passed in quotes param1='value 1'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
idShura, 2019-04-23
@idShura

sql server is not specified, so on the example of Oracle. You can get the connection string here connectionstrings.com

Private Sub CommandButton1_Click()

    'Строка подключения
    cConn = "Driver={Oracle in OraClient11g_home1};Dbq=<SID>;Uid=<User>;Pwd=<Password>;"

    Dim Cn As ADODB.Connection
    Dim Cm As ADODB.Command

    Set Cn = New ADODB.Connection
    Cn.Open cConn
    
    Set Cm = New ADODB.Command
    
    Cm.ActiveConnection = Cn
    Cm.CommandText = " begin MyStorageProc('" + Range("A1") + "') end;"
    Cm.CommandType = adCmdText
    Cm.Execute

End Sub

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question