Arrays MsgBox中的多行值
如何检索存储在vSum变量中的值 一个msgbox中的多行 代替在多个msgbox中逐个检索Arrays MsgBox中的多行值,arrays,ms-access,vb6,vba,Arrays,Ms Access,Vb6,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(b
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
Dim rSEL、rSUM、rDes作为DAO.Recordset
Dim vItem_id、vQnty、vSum作为整数
将VDE设置为字符串
如果不为IsNull(itemId)且不为IsNull(qnty_in),则
如果qnty_处于平衡状态或为空(平衡),则
取消=真
如果结束
Set rSEL=CurrentDb.OpenRecordset(“选择项目id,项目qnty
来自basketQnty_tbl,其中basket_id=“&basketId”)
'检查记录集是否实际包含行
如果不是(rSEL.EOF和rSEL.BOF),则
首先
直到rSEL.EOF为止
'将itemId保存到变量中
vItem_id=rSEL!项目编号
vQnty=(rSEL!项目数量)*数量单位
Set rSUM=CurrentDb.OpenRecordset(“选择sum(qnty_in*qnty_类型)
作为来自销售的QN,其中itemid=“&vItem\u id”)
设置rDes=CurrentDb.OpenRecordset(“选择itemDesc
来自items_main,其中itemId=“&vItem_id”)
vSum=rSUM!QN
vDes=rDes!itemDesc
'移动到下一个记录。永远不要忘记做这件事。
如果vQnty>vSum,则
MsgBox“您的库存中只有(“&vSum&”)项(“&vDes&”)
取消=真
如果结束
下一个
环
如果结束
瑟尔,结束
如果结束
我怎么才能解决它 您可以使用
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()
...
创建一个数组变量来保存所有消息字符串。将其设置为超出您需要的大小,然后在您知道有多少封邮件后,重拨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
Dim rSEL、rSUM、rDes作为DAO.Recordset
Dim vItem_id、vQnty、vSum作为整数
将VDE设置为字符串
Dim aMsg()作为字符串
暗lCnt为长
如果不为IsNull(itemId)且不为IsNull(qnty_in),则
如果qnty_处于平衡状态或为空(平衡),则
取消=真
如果结束
Set rSEL=CurrentDb.OpenRecordset(“从basketQnty中选择item_id,item_qnty,其中basket_id=“&basketId”)
'检查记录集是否实际包含行
如果不是(rSEL.EOF和rSEL.BOF),则
首先
ReDim aMsg(1到rSEL.RecordCount*10)'使其比您需要的更大
直到rSEL.EOF为止
'将itemId保存到变量中
vItem_id=rSEL!项目编号
vQnty=(rSEL!项目数量)*数量单位
设置rSUM=CurrentDb.OpenRecordset(“选择sum(qnty_in*qnty_type)作为来自sales_tbl的QN,其中itemid=“&vItem_id”)
Set rDes=CurrentDb.OpenRecordset(“从items\u main中选择itemDesc,其中itemId=“&vItem\u id”)
vSum=rSUM!QN
vDes=rDes!itemDesc
'移动到下一个记录。永远不要忘记做这件事。
如果vQnty>vSum,则
lCnt=lCnt+1
aMsg(lCnt)=“库存中只有(“&vSum&”)项(“&vDes&”)
如果结束
下一个
环
如果lCnt>=1,则
ReDim保留aMsg(1至lCnt)
MsgBox连接(aMsg、vbNewLine)
取消=真
如果结束
如果结束
瑟尔,结束
如果结束
StringBuilder不包含在使用系统的中。Text
请如何描述我?!抱歉,这是VB中的“导入”(使用的是C#)。在代码顶部包括一行导入系统。文本或使用出现在StringBuilder上的智能标记。看到这个答案都是VB.net。问题是VBA。非常感谢迪克先生,这正是我需要的