Stockcheck更新库存表

时间:2012-02-20 01:30:16

标签: sql ms-access vba access-vba

所以我的项目现在进展顺利,并且我为用户创建了一个新表格,以便在拍下库存后手动输入库存水平。

我到目前为止的编码如下

Private Sub StockTake_Click()

'Introduce items and variables
Dim Carling As Integer
Dim Carlsburg As Integer
Dim IPA As Integer
Dim Strongbow As Integer
Dim RevJames As Integer
Dim Becks As Integer
Dim WKDBlue As Integer
Dim WKDRed As Integer
Dim SmirnoffIce As Integer
Dim KoppaburgPear As Integer
Dim KoppaburgSum As Integer
Dim Bulmers As Integer
Dim Vodka As Integer
Dim Gin As Integer
Dim Sherry As Integer
Dim Sambuca As Integer
Dim Rum As Integer
Dim Port As Integer
Dim Whiskey As Integer
Dim Baileys As Integer
Dim Jagermeister As Integer
Dim Martini As Integer
Dim CokeCan As Integer
Dim Coke As Integer
Dim LemonadeCan As Integer
Dim Lemonade As Integer
Dim Squash As Integer
Dim Tonic As Integer
Dim RedBull As Integer
Dim Nuts As Integer
Dim Crisps As Integer
Dim SQLError As String
Dim SQLDelete As String
Dim SQLUpdate As String
Dim SQLBackup As String
Dim PubStock As Integer

'define items. these "Txtname" are text boxes on the form that the user can enter the values into... already this seems like a massive 'code.
Carling = txtCarling
Carlsburg = txtCarlsburg
IPA = txtIPA
Strongbow = txtStrongbow
RevJames = txtRevJames
Becks = txtBecks
WKDBlue = txtWKDBlue
WKDRed = txtWKDRed
SmirnoffIce = txtSmirnoffIce
KoppaburgPear = txtKopPear
KoppaburgSum = txtKopSum
Bulmers = txtBulmers
Vodka = txtVodka
Gin = txtGin
Sherry = txtSherry
Sambuca = txtSambuca
Rum = txtRum
Port = txtPort
Whiskey = txtWhiskey
Baileys = txtBaileys
Jagermeister = txtJagermeister
Martini = txtMartini
CokeCan = txtCokeCan
Coke = txtCokeDra
LemonadeCan = txtLemonadeCan
Lemonade = txtLemonadeDra
Squash = txtSquash
Tonic = txtTonic
RedBull = txtRedBull
Nuts = txtNuts
Crisps = txtCrisps

'introduce SQL coding
SQLDelete = "DELETE * FROM TblStock"

SQLUpdate = "update the tblstock with the values from the form based on the name of the product. tblStock contains StockID*, ProductID and stockLevel which is just the added stock values, not the on hand stock. thus the new value inputed on the form should replace all current values in the table. "

SQLBackup = "export all data in tblStock to update values in an excel table, ie, add to the data allready there, not replace it."

'Start actual coding
If Carling = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Carlsburg = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If IPA = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Strongbow = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If RevJames = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Becks = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If WKDBlue = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If WKDRed = Null Then MsgBox "Plese Fill in all areas of the form to complete the  Stock Take and try again" Else
If SmirnoffIce = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If KoppaburgPear = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If KoppaburgSum = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Bulmers = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Vodka = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Gin = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Sherry = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Sambuca = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Rum = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Port = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Whiskey = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Baileys = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Jagermeister = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Martini = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If CokeCan = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If Coke = Null Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again" Else
If LemonadeCan = Null Then MsgBox "Please Fill in all areas of the form to complete the Stock Take and try again" Else
If Lemonade = Null Then MsgBox "Please Fill in all areas of the form to complete the Stock Take and try again" Else
If Squash = Null Then MsgBox "Please Fill in all areas of the form to complete the Stock Take and try again" Else
If Tonic = Null Then MsgBox "Please Fill in all areas of the form to complete the Stock Take and try again" Else
If RedBull = Null Then MsgBox "Please Fill in all areas of the form to complete the Stock Take and try again" Else
If Nuts = Null Then MsgBox "Please Fill in all areas of the form to complete the Stock Take and try again" Else
If Crisps = Null Then MsgBox "Please Fill in all areas of the form to complete the Stock Take and try again" Else PubStock = 1


If PubStock = 1 Then DoCmd.RunSQL SQLBackup
DoCmd.SetWarnings False
DoCmd.RunSQL SQLDelete
DoCmd.SetWarnings True
DoCmd.RunSQL SQLUpdate

If PubStock <> 1 Then MsgBox "Plese Fill in all areas of the form to complete the Stock Take and try again"

End Sub

kk,除了我错过了两个sql查询这一事实外,任何人都可以看到编码的任何其他问题吗?它似乎过于庞大,我觉得我严重缺少一些重要的或明显的东西,这会使这更容易管理。

除了它相当大的事实,我认为我的大量“If”段错误至少可以说,任何人都可以帮助我使用可能用于SQLUpdate的sql吗?我担心这也将是一大块......必须有一个比这更容易的方式...... :(

如果可以提供帮助,可以附加数据库。

由于 萨姆

1 个答案:

答案 0 :(得分:3)

你可以让这更容易。最初易于编码,以后更容易维护。

创建一个表 tblStockItems ,包含2个字段:item_id(主键);和item_name。

item_id item_name
      1 Baileys
      2 Becks
      3 Bulmers

使用2个数字字段创建另一个表 tblScratchInventory :item_id;和数量。

创建此查询并将其另存为 qryScratchInventory

SELECT
    inv.item_id,
    items.item_name,
    inv.quantity
FROM
    tblScratchInventory AS inv
    INNER JOIN tblStockItems AS items
    ON inv.item_id = items.item_id
ORDER BY items.item_name;

创建一个使用qryScratchInventory作为记录源的连续表单。将表单的“允许添加”和“允许删除”属性设置为“否”。在表单打开期间,您可以“刷新”tblScratchInventory以准备记录一组新的库存值。

Private Sub Form_Open(Cancel As Integer)
    Dim db As DAO.Database
    Dim strSql As String
    Set db = CurrentDb
    strSql = "DELETE FROM tblScratchInventory;"
    db.Execute strSql, dbFailOnError
    strSql = "INSERT INTO tblScratchInventory ( item_id )" & vbCrLf & _
        "SELECT tblStockItems.item_id" & vbCrLf & _
        "FROM tblStockItems;"
    db.Execute strSql, dbFailOnError
    Set db = Nothing
    Me.Requery
End Sub

您还应该包含一个错误处理程序来处理 dbFailOnError 出现的任何问题。

向表单页脚添加一个命令按钮 cmdDone 。使用此代码作为其单击事件。

Private Sub cmdDone_Click()
    Dim lngNumNulls As Long
    Dim strNullItems As String
    Dim strMsg As String

    If Me.Dirty Then Me.Dirty = False
    lngNumNulls = DCount("*", "tblScratchInventory", "quantity Is Null")
    Select Case lngNumNulls
    Case 0
        ' do what you need here '
        ' then close the form: '
        DoCmd.Close acForm, Me.name
    Case Is <= 6 ' list individual items if not too many of them '
        Dim rs As DAO.Recordset
        Dim db As DAO.Database
        Dim strSql As String
        strSql = "SELECT item_name" & vbCrLf & _
            "FROM qryScratchInventory" & vbCrLf & _
            "WHERE quantity Is Null" & vbCrLf & _
            "ORDER BY item_name;"
        Set db = CurrentDb
        Set rs = db.OpenRecordset(strSql)
        With rs
            Do While Not .EOF
                strNullItems = strNullItems & vbCrLf & !item_name
                .MoveNext
            Loop
            .Close
        End With
        Set rs = Nothing
        Set db = Nothing
        strMsg = "Please input quantities for these items: " & strNullItems
    Case Else
        strMsg = "Please input quantities for all items."
    End Select
    If Len(strMsg) > 0 Then
        MsgBox strMsg
    End If
End Sub

在确定所有库存商品都具有非空值后,我不确定您要对库存值做什么(案例0,'在cmdDone_Click中执行您需要的操作')。也许你想要一个UPDATE语句将数量转移到另一个表(TblStock?)。或者,您可以将表单基于TblStock的查询,而不是使用qryScratchInventory作为中间步骤。