VBA 通过 VBA 和 ADO 调用函数

VBA calling function via VBA and ADO

我在通过 VBA 调用 Oracle 服务器上的函数时遇到问题。 当我尝试调用不带参数的函数时。没关系。 但是,当我用参数调用函数时。我收到错误消息([Microsoft][Oracle 的 ODBC 驱动程序]参数类型无效) 有什么想法吗? 这是 vba 代码和 plsql(我制作了用于测试的基本函数)

Vba

Private Sub test()
Dim Oracon As ADODB.Connection
Dim cmd As New ADODB.Command
Dim param1 As New ADODB.Parameter
Dim objErr As ADODB.Error


Set Oracon = CreateObject("ADODB.Connection")
mujuser = "xxxx"
mujPWD = "xxxxx"

  strConn = "UID=" & mujuser & ";PWD=" & mujPWD & ";driver={Microsoft ODBC for Oracle};" & _
              "SERVER=xx.xxx;"
Oracon.ConnectionString = strConn
Oracon.Open
cmd.ActiveConnection = Oracon
 cmd.CommandText = "el_test"
   cmd.CommandType = adCmdStoredProc

Set param1 = cmd.CreateParameter("P1", adLongVarChar, adParamInput, 256)
cmd.Parameters.Append param1
cmd.Parameters(0).Value = "ahoj1"

cmd.Execute

End Sub

和函数

CREATE OR REPLACE FUNCTION EL_TEST 
(
  P1 IN VARCHAR2 
) RETURN VARCHAR2 AS 
BEGIN
  RETURN 'Ahoj';
END EL_TEST;

谢谢。

我承认,我从未尝试过通过代码执行这样的函数,但令我惊讶的是它在没有参数的情况下也能正常工作,因为您设置函数的方式。我认为您希望获得价值的方式是:

select el_test('ahoj1') from dual;

如果您将此更改从 函数 更改为 过程 ,我认为它会按您预期的方式工作:

CREATE OR REPLACE procedure EL_TEST 
( P1 IN VARCHAR2,
  p2 out varchar2) is
BEGIN
  p2 := 'Ahoj';
END EL_TEST;

然后您的 VBA 将如下所示:

Private Sub test()
  Dim Oracon As ADODB.Connection
  Dim cmd As New ADODB.Command
  Dim param1 As New ADODB.Parameter
  Dim objErr As ADODB.Error

  Set Oracon = CreateObject("ADODB.Connection")
  mujuser = "xxxx"
  mujPWD = "xxxxxx"

  strConn = "UID=" & mujuser & ";PWD=" & mujPWD & _
        ";driver={Microsoft ODBC for Oracle};SERVER=xxxx-xx"

  Oracon.ConnectionString = strConn
  Oracon.Open

  cmd.ActiveConnection = Oracon
  cmd.CommandText = "el_test"
  cmd.CommandType = adCmdStoredProc
  cmd.NamedParameters = True

  cmd.Parameters.Append cmd.CreateParameter("P1", adVarChar, adParamInput, 256, "ahoj1")
  cmd.Parameters.Append cmd.CreateParameter("P2", adVarChar, adParamOutput, 256)

  cmd.Execute

  Dim result As String
  result = cmd.Parameters(1).Value

End Sub

使用 OLEDB

获取函数 return 值

我到处搜索这个问题,但最终还是自己解决了这个问题。
我的解决方案是在 VBScript 中,但我在下面的(未经测试的)代码中表示了它。

诀窍是 第一个参数是 return 值 .

Private Sub test()
Dim Oracon As ADODB.Connection
Dim cmd As New ADODB.Command
Dim param1 As New ADODB.Parameter
Dim param0 As New ADODB.Parameter
Dim objErr As ADODB.Error

Set Oracon = CreateObject("ADODB.Connection")
mujuser = "xxxx"
mujPWD = "xxxxx"

  strConn = "UID=" & mujuser & ";PWD=" & mujPWD & ";driver={Microsoft ODBC for Oracle};" & _
              "SERVER=xx.xxx;"
Oracon.ConnectionString = strConn
Oracon.Open
cmd.ActiveConnection = Oracon
 cmd.CommandText = "el_test"
   cmd.CommandType = adCmdStoredProc

Set param0 = cmd.CreateParameter("P0", adLongVarChar, adParamReturnValue, 256)
Set param1 = cmd.CreateParameter("P1", adLongVarChar, adParamInput, 256)
cmd.Parameters.Append param0
cmd.Parameters.Append param1

cmd.Execute

Dim result As String
result = param0.Value  ' Use the variable you set. Same as cmd.Parameters(1).Value '

End Sub