Results 1 to 8 of 8
  1. #1
    blueraincoat is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Shanghai,CHN
    Posts
    99

    Using VB to save data in a form

    Hi All,
    I have a form(frmInventoryDecrease) based on a table, the table is for recording every inventory decrement. And on the form I have a command button, in the command click event, I made a update query using vb.(the idea comes for our forums too, thanks again...)By this time it all works fine.



    Then come the problem. I add a BeforeUpdate event(using msgbox) to prevent user's misoperation like changed the decrement history, so every time after I update the inventory and go to the new record, the beforupdate event is triggered and the msgbox show up......I don't think there is a need to confirm in this condition, I tried to make the form dirty property to false right after the update query but the beforeupdate msgbox still shows up.

    Any comment is appreciated!

    For detail information, see the codes below.


    Private Sub Command32_Click()
    Dim mySQL As String
    mySQL = "UPDATE tblInventoryInfo SET tblInventoryInfo.Qty= tblInventoryInfo.Qty-" & Me.Qty
    mySQL = mySQL & " Where tblInventoryInfo.PartNumber='" & Me.PartNumber & "' AND tblInventoryInfo.LotNumber='" & Me.LotNumber & "'"
    Select Case MsgBox("You are going to decrease the Stock Level of " & Me.PartNumber & " by " & Me.Qty _
    & "." & vbCrLf & "Are you sure?" & vbCrLf & "Yes for sure,No for cancel.", vbYesNo, "Inventory Update Confirmation")
    Case vbYes
    CurrentDb.Execute mySQL, dbFailOnError
    MsgBox ("Inventory Update Successfully Done!")
    Case vbNo
    MsgBox ("You have canceled the inventory update.")
    End Select
    If Me.Dirty Then
    Me.Dirty = False
    End If

    End Sub

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    On Error GoTo BeforeUpdate_Error

    If Me.Dirty Then
    If MsgBox("The record has changed - do you want to save it?", _
    vbYesNo + vbQuestion, "Save Changes") = vbNo Then
    Me.Undo
    End If
    End If
    BeforeUpdate_Exit:
    Exit Sub
    BeforeUpdate_Error:
    MsgBox Err.Description
    Resume BeforeUpdate_Exit
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    The code you have probably doesn't do what you think it does. This:

    Me.Dirty = False

    forces a save of the record, it doesn't undo it. Typically you would not have a bound form plus code to save, unless you're updating a second table. If you need the form bound, it sounds like you want Undo there.

    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    blueraincoat is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Shanghai,CHN
    Posts
    99
    Hi Paul, the second code was to save records in the a bound form, and the first is to update a filed in another tbl I think what I need is take off the second code and let the user be careful when changing from record to record.Thanks anyway.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    I must have misunderstood. You're looking to avoid the before update code/message box if the user clicked on the button? One way would be to set a form level variable in the button's code. In the before update code, test that variable and only execute the before update code if it doesn't have the value set in the button.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    blueraincoat is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Shanghai,CHN
    Posts
    99
    Thank you Paul, it's a clear idea.
    I'll work on it and let you know the result.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    No problem, sorry I misunderstood the question to begin with.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    blueraincoat is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Shanghai,CHN
    Posts
    99
    Hey Paul, your idea works excellent!
    I give the variable a value when the user run the update query, and the before update code and MsgBox do not popup any more as I wanted.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Excellent, glad we found a solution for you.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 11
    Last Post: 07-24-2012, 07:50 PM
  2. Replies: 9
    Last Post: 01-20-2011, 02:22 PM
  3. Replies: 4
    Last Post: 01-05-2011, 07:56 AM
  4. Replies: 4
    Last Post: 04-01-2009, 11:48 AM
  5. Find data, load data, and save as a new record
    By hawzmolly in forum Access
    Replies: 0
    Last Post: 10-05-2008, 03:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums