MsgBox中的多行值

时间:2013-04-11 14:19:22

标签: arrays vba ms-access vb6 access-vba

如何检索存储在vSum变量中的值 一个msgbox中的多行 而不是一个接一个地在多个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

我该如何解决?!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您可以使用StringBuilder来构建生成的消息。这比串联字符串更有效,因为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()
    ...
相关问题