通过 .NET 在 Access 中调用 sub 后接收值 [英] Receiving values back after calling a sub in Access through .NET

查看:46
本文介绍了通过 .NET 在 Access 中调用 sub 后接收值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个必须在 Access mdb 数据库文件中调用 subs 的 C# 应用程序.

I am creating an C# application that must call subs in an Access mdb database file.

我在 mdb 文件中创建了一个测试子,我可以从 C# 调用它并传递参数.一切正常,但我想将结果传递回 C#.我知道我不能用函数来做到这一点,所以是否可以通过引用传递变量,然后 vba 可以更改变量并返回结果?我尝试了以下方法,但不起作用.有谁知道这是否可能?

I've created a test sub in the mdb file and I can call it from C# and also pass it parameters. That all works fine, but I want to pass a result back to C#. I know I can't do this with a function, so is it possible to pass the variables by reference, then vba can change the variables and I get my result back? I tried the following, but it doesn't work. Anybody know if this is possible?

谢谢

VBA 测试子:

Sub test(Byref p1 As String)
   p1="bar"
   MsgBox p1
End Sub

从 C# 调用它:

Access.Application oAccess = new Access.Application();
oAccess.Visible = true;
oAccess.OpenCurrentDatabase("d:\\tmp\\test1.mdb", false, "");
string t;
t = "foo"
oAccess.Run("test", t);
//t still equals "foo" at this point, it should be equal to "bar"

推荐答案

对于标量(简单的数字、字符串或布尔值),系统会生成一个当前值的副本并将此副本传递给被调用的程序.从被调用过程返回时,系统丢弃副本.所以,即使被调用的过程改变了一个ByVal 参数,该更改无法传播回来电者.

For a scalar (a simple number, string, or Boolean) the system makes a copy of the current value and passes this copy to the called procedure. Upon return from the called procedure the system discards the copy. So, even if the called procedure changes the value of a ByVal argument, there is no way for that change to propagate back to the caller.

查看this 以了解有关传递标量值的说明.如页面所述,被调用者可以修改对象的属性,从而调用者可以看到修改后的值(在使用对象的情况下).

Take a look at this for explanation on passing scalar values. As explained on the page, callee can modify the property of the object, thereby caller can see the modified value (in case of using an object).

另一种选择是使用函数的返回值.

Another alternative, is to use a return value from the function.

Function test(p1 As String) as String
   test = "Bar"
End Function

c#

t = "foo"
t = oAccess.Run("test", t);

这篇关于通过 .NET 在 Access 中调用 sub 后接收值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆