【问题标题】:multi line value in MsgBox [duplicate]MsgBox中的多行值[重复]
【发布时间】:2013-04-12 05:02:48
【问题描述】:

如何检索存储在 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

我该如何解决?!

【问题讨论】:

    标签: arrays ms-access vb6 vba


    【解决方案1】:

    创建一个数组变量来保存所有的消息字符串。让它比你需要的大,然后 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
    

    【讨论】:

    • 非常感谢迪克先生,这正是我所需要的
    【解决方案2】:

    您可以使用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()
        ...
    

    【讨论】:

    • StringBuilder 不包含
    • 添加Using System.Text
    • 请如何形容我?!
    • 对不起,它是 VB 中的“导入”(使用的是 C#)。在代码的顶部包含一行Imports System.Text 或使用出现在StringBuilder 处的智能标记。见VB.NET StringBuilder
    • 这个答案全是VB.net。问题是 VBA。
    猜你喜欢
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    相关资源
    最近更新 更多