1

我没有太多使用 VB,但据我所知,范围与 C# 中的工作方式相同。问题是,我在 MS Access 中使用 VB,所以我不确定规则是否有点不同(尽管我认为它们不是)。以下代码显示了分配给仅在函数参数中声明的变量的值。特别是查看 PurchaseOrderID,我不确定它是如何保留其分配的值以便在执行它的函数中使用的。

Function Create(SupplierID As Long, EmployeeID As Long, OrderID As Long, PurchaseOrderID As Long) As Boolean
    Dim rsw As New RecordsetWrapper
    If rsw.OpenRecordset("Purchase Orders") Then
        With rsw.Recordset
            .AddNew
            ![Supplier ID] = SupplierID
            If EmployeeID > 0 Then
                ![Created By] = EmployeeID
                ![Creation Date] = Now()
                ![Submitted By] = EmployeeID
                ![Submitted Date] = Now()
                ![Status ID] = Submitted_PurchaseOrder
            End If

            If OrderID > 0 Then
                ![Notes] = InsertString(PurchaseGeneratedBasedOnOrder, CStr(OrderID))
            End If
            If rsw.Update Then
                .Bookmark = .LastModified
                PurchaseOrderID = ![Purchase Order ID]
                Create = True
            End If
        End With
    End If
End Function


Function CreateLineItem(PurchaseOrderID As Long, ProductID As Long, UnitCost As Long, Quantity As Long) As Boolean
    Dim rsw As New RecordsetWrapper
    If rsw.OpenRecordset("Purchase Order Details") Then
        With rsw.Recordset
            .AddNew
            ![Purchase Order ID] = PurchaseOrderID
            ![Product ID] = ProductID
            ![Quantity] = Quantity
            ![Unit Cost] = UnitCost
            CreateLineItem = rsw.Update
        End With
    End If
End Function

有人可以给我一些见解吗?

4

3 回答 3

2

在 VBA 中,如何调用过程可能很重要。

“即使被调用的过程已将其参数声明为 ByRef,您也可以通过将每个参数括在括号中来强制它们为 ByVal。”

-- http://www.cpearson.com/excel/byrefbyval.aspx

所有 Office 应用程序中的 VBA 都是如此。例如,假设您有两个过程:

Sub SubByVal(ByVal Total As Integer)
    Total = 50
End Sub

Sub SubByRef(ByRef Total As Integer)
    Total = 50
End Sub

然后你运行了一些测试:

Dim Total As Integer
Total = 100

这三个版本按预期工作,总计等于 100

Call SubByVal(Total)
SubByVal (Total)
SubByVal Total

这两个按预期工作,总计等于 50

Call SubByRef(Total)
SubByRef Total

然而,在这个版本中,尽管调用了 ByRef,Total 还是等于 100,因为它被括号强制为 ByVal。

SubByRef (Total)
于 2012-11-13T00:37:19.690 回答
2

也许这最好由类型来处理。像这样的东西:

Type PurchaseSet
   PurchaseOrderID As Long
   OrderCreated as boolean
End Type


Function Create(SupplierID As Long, EmployeeID As Long, OrderID As Long ) As PurchaseSet
    Dim rsw As New RecordsetWrapper
    Dim ps as PurchaseSet 
   ps.OrderCreated = false

    If rsw.OpenRecordset("Purchase Orders") Then
        With rsw.Recordset
            .AddNew
            ![Supplier ID] = SupplierID
            If EmployeeID > 0 Then
                ![Created By] = EmployeeID
                ![Creation Date] = Now()
                ![Submitted By] = EmployeeID
                ![Submitted Date] = Now()
                ![Status ID] = Submitted_PurchaseOrder
            End If

            If OrderID > 0 Then
                ![Notes] = InsertString(PurchaseGeneratedBasedOnOrder, CStr(OrderID))
            End If
            If rsw.Update Then
                .Bookmark = .LastModified
                ps.PurchaseOrderID = ![Purchase Order ID]
                ps.OrderCreated = True

            End If
        End With
    End If

    Create = ps
End Function
于 2012-11-15T22:05:26.553 回答
1

您可以按值或按引用将参数传递给 VBA 过程。如果您既没有指定,也没有ByValByRef过程的声明中指定,则默认为ByRef. 所以以下两个声明是等价的......

Function DoSomething(PurchaseOrderID As Long) AS Boolean
Function DoSomething(ByRef PurchaseOrderID As Long) AS Boolean

这里的关键是ByRef允许将过程中参数的更改传输回调用者。

于 2012-11-12T23:15:52.353 回答