0

如何在一个 msgbox 中检索存储在 vSum 变量多行中的值,在多个 msgbox 中逐个检索

Dim rSEL, rSUM, rDes As DAO.Recordset
Dim vItem_id, vQnty, vSum As Integer
Dim vDes As String
If Not IsNull(itemId) And Not IsNull(qnty_in) Then
    If qnty_in <= 0 Or qnty_in > balance Or IsNull(balance) Then
        Cancel = True
    End If
    Set rSEL = CurrentDb.OpenRecordset("SELECT item_id,item_qnty 
               FROM basketQnty_tbl WHERE basket_id=" & basketId)
    'Check to see if the recordset actually contains rows
    If Not (rSEL.EOF And rSEL.BOF) Then
    rSEL.MoveFirst
    Do Until rSEL.EOF
        'Save itemId into a variable
        vItem_id = rSEL!item_id
        vQnty = (rSEL!item_qnty) * qnty_in
        Set rSUM = CurrentDb.OpenRecordset("SELECT sum(qnty_in*qnty_type) 
                   as QN FROM sales_tbl WHERE itemid=" & vItem_id)
        Set rDes = CurrentDb.OpenRecordset("SELECT itemDesc 
                   FROM items_main WHERE itemId=" & vItem_id)
        vSum = rSUM!QN
        vDes = rDes!itemDesc
        'Move to the next record. Don't ever forget to do this.
        If vQnty > vSum Then
            MsgBox "you have only (" & vSum & " ) of Item (" & vDes & " ) in the stock"
            Cancel = True
        End If
    rSEL.MoveNext
    Loop
    End If
   rSEL.Close
End If

我该如何解决?!

4

2 回答 2

2

创建一个数组变量来保存所有的消息字符串。让它比你需要的大,然后 Redim Preserve 在你知道你有多少条消息后将它减小到适当的大小。最后,使用 Join 将所有消息显示在一个 MsgBox 中。这是一个例子。

Dim rSEL, rSUM, rDes As DAO.Recordset
Dim vItem_id, vQnty, vSum As Integer
Dim vDes As String
Dim aMsg() As String
Dim lCnt As Long

If Not IsNull(itemId) And Not IsNull(qnty_in) Then

    If qnty_in <= 0 Or qnty_in > balance Or IsNull(balance) Then
        Cancel = True
    End If

    Set rSEL = CurrentDb.OpenRecordset("SELECT item_id,item_qnty FROM basketQnty_tbl WHERE basket_id=" & basketId)
    'Check to see if the recordset actually contains rows
    If Not (rSEL.EOF And rSEL.BOF) Then
        rSEL.MoveFirst

        ReDim aMsg(1 To rSEL.RecordCount * 10) 'make it bigger than you'll need

        Do Until rSEL.EOF
            'Save itemId into a variable
            vItem_id = rSEL!item_id
            vQnty = (rSEL!item_qnty) * qnty_in
            Set rSUM = CurrentDb.OpenRecordset("SELECT sum(qnty_in*qnty_type) as QN FROM sales_tbl WHERE itemid=" & vItem_id)
            Set rDes = CurrentDb.OpenRecordset("SELECT itemDesc FROM items_main WHERE itemId=" & vItem_id)
            vSum = rSUM!QN
            vDes = rDes!itemDesc
            'Move to the next record. Don't ever forget to do this.
            If vQnty > vSum Then
                lCnt = lCnt + 1
                aMsg(lCnt) = "you have only (" & vSum & " ) of Item (" & vDes & " ) in the stock"
            End If
           rSEL.MoveNext
        Loop
        If lCnt >= 1 Then
            ReDim Preserve aMsg(1 To lCnt)
            MsgBox Join(aMsg, vbNewLine)
            Cancel = True
        End If
    End If
   rSEL.Close

End If
于 2013-04-11T16:08:01.400 回答
0

您可以使用 aStringBuilder来构造生成的消息。这比串联字符串更高效,因为StringBuilder有一个高效的内存管理,即它不会在每次字符串操作后分配新的内存;相反,它与内部缓冲区一起使用。

Dim sb As StringBuilder

sb = New StringBuilder()

...
Do Until rSEL.EOF
    ....
    sb.Append("you have only (") _
      .Append(vSum) _
      .Append(" ) of Item (") _
      .Append(vDes) _
      .AppendLine(" ) in the stock")
    rSEL.MoveNext
Loop
MsgBox sb.ToString()

或者,您可以将字符串生成器与String.Format

...
Do Until rSEL.EOF
    ....
    sb.AppendLine( _
        String.Format("you have only ({0} ) of Item ({1} ) in the stock", vSum, vDes) _
    )
    rSEL.MoveNext
Loop
MsgBox sb.ToString()

这更容易阅读。

或者更简单:

    ...
    sb.AppendFormat("you have only ({0} ) of Item ({1} ) in the stock", vSum, vDes) _
      .AppendLine()
    ...
于 2013-04-11T14:26:46.363 回答