通过 .NET 在 Access 中调用 sub 后接收值

编程入门 行业动态 更新时间:2024-10-15 02:26:45
本文介绍了通过 .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 后接收值

本文发布于:2023-11-25 02:20:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627931.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   Access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!