向用户显示多个验证消息 [英] Display multiple validation messages to the user

查看:23
本文介绍了向用户显示多个验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

In MS-Access, how can I store the rows retrieved from my SELECT statement in an array, and show many rows in one messagebox:

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

解决方案

I would not use a Message Box for this, since you could potentially have hundreds of rows, and they wouldn't all fit. Instead, I would create a custom form with a ListBox, and whenever you get the error, do the following:

If vQnty > vSum Then
    <ErrorForm>.<ListBox>.AddItem("You have only (" & vSum & " ) of Item (" & vDes & " ) in the stock")
    Cancel = True
End If

If at the end of the loop, Cancel = True, display ErrorForm.

Another thing - you may be able to create a query which does all this in one go. You need to join the three tables, and add the vQnty > vSum condition. This would be pretty neat, because you could use the query as the source of the ListBox - no code required.

这篇关于向用户显示多个验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆