Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    markpastoril is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2018
    Posts
    62

    Validating textbox before saving, help me through this.

    I am working in a payment system and went to save button which would like to ask for help


    Click image for larger version. 

Name:	payment1.jpg 
Views:	41 
Size:	229.1 KB 
ID:	35830 Click image for larger version. 

Name:	code.jpg 
Views:	38 
Size:	189.1 KB 
ID:	35831

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    If you pasted your code instead of pictures of it, we could copy what seems to be the problem rather than having to type that part (or Heaven forbid, most or all of it).
    Your 3rd line saves the record, so whatever validation you want to do after that is pointless. Also, only events that have a Cancel argument can be cancelled as you attempt to do. A click event is not one of them. There is a CancelEvent method, but I've never used it and don't know if it just applies to macros or not. You could simplify your code a lot, either by putting multiple validations on one line (e.g. If this OR that OR something OR... Then) or iterating over the form controls to see which required ones are blank and presenting all of them in a message in one go.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    markpastoril is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2018
    Posts
    62
    sorry this is the code sir

    Private Sub Command72_Click()

    If IsNull(Me.receipt.Value) Then
    MsgBox "OR Field is Empty", vbOKOnly

    Cancel = True
    Me.Undo
    If IsNull(Me.amountpay.Value) Then
    MsgBox "Amount Paid is Empty", vbOKOnly

    Cancel = True
    Me.Undo
    If IsNull(Me.datepay.Value) Then
    MsgBox "Date Paid is Empty", vbOKOnly

    Cancel = True
    Me.Undo

    Else
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.OpenReport "Receipt", acViewPreview, , "[BillingID] = " & Me.BILLINGID, , acHidden
    DoCmd.SelectObject acReport, "Receipt"
    DoCmd.PrintOut acSelection
    MsgBox "Record Updated", vbOKOnly
    Me.lstItems.Requery
    Me.Form.Requery = ""

    End If
    End If
    End If

    End Sub

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    OK, but that's not the same code now. If that is your new code after the suggestions, does it still not work as you wish?
    Perhaps just as important, are the fields being checked on one form (e.g. subform) and the button is on another form (e.g. main form)? If so, that could be another problem. For example, if you do anything to update a subform when you move off of it to click a button, the subform record is already saved.

    Unfortunately, I neglected to ask you to post code within code tags (my fault). Please do so next time, it makes it much easier to read as I think you'll see below.
    Thanks.

    Regardless, something like this may work
    Code:
    Private Sub Command72_Click()
    
    If IsNull(Me.receipt) Then
      MsgBox "OR Field is Empty", vbOKOnly
      me.receipt.setfocus
      exit sub
    end if
    
    If IsNull(Me.amountpay) Then
      MsgBox "Amount Paid is Empty", vbOKOnly
      me.amountpay.setfocus
      exit sub
    end if
    
    If IsNull(Me.datepay) Then
      MsgBox "Date Paid is Empty", vbOKOnly
      me.datepay.setfocus
      exit sub
    end if
    
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.OpenReport "Receipt", acViewPreview, , "[BillingID] = " & Me.BILLINGID, , acHidden
    DoCmd.SelectObject acReport, "Receipt"
    DoCmd.PrintOut acSelection
    MsgBox "Record Updated", vbOKOnly
    Me.lstItems.Requery 'may not be required if you're requerying form anyway
    'Me.Form.Requery = "" 'you cannot make anything equal the Requery method, plus Me IS the form
    Me.Requery
    
    End Sub
    The logic here is that if testing one control at a time and any one of them fails the test, simply exit the sub and then there's nothing to Cancel. If all is OK, code moves on to the part you want to have happen. Not that it really matters, but .Value is the default property for textboxes, so you don't need to use it. I removed it to show the difference, but put it back if you prefer.

    Hope the code helps.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    markpastoril is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2018
    Posts
    62
    Tried your code sir but, still it saves automatically. even if I did'nt fill up the date paid and the or#.

  6. #6
    markpastoril is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2018
    Posts
    62
    It saves automatically when i click anywhere in the form, even when i would tried to fill the textbox of the OR#

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    It appears that you have a situation where the form is being updated when you get to the button but before actually clicking on it. That could be other code such as a Lost Focus event, or the button code is on one form and the problem controls are on another. I think there are too many possibilities but the most likely is that the controls are on a datasheet subform and it's getting updated when you leave it to click. If that's the case, validation would have to move to the Before Update event of that subform. Hard to tell only by looking at your picture. If you want to post a compacted and zipped copy of your db that would help. You can remove sensitive info from the posted copy.

  8. #8
    markpastoril is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2018
    Posts
    62
    thanks sir, i've just discovered that there were code left that i've tried and now it's works properly as I would like to do it
    Thank you sir @Micron.

  9. #9
    markpastoril is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2018
    Posts
    62
    but sir, I would like to have my Payment section to function like a POS of a cashier. Like this.
    Click image for larger version. 

Name:	pos like payment..jpg 
Views:	35 
Size:	216.2 KB 
ID:	35851 When the Cashier inputs the Amount of the Customer they pay in the Tendered textbox, the form calculates the Amount to be Paid and Calculates the change.

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Glad that I could help.
    You dont have to call me sir - I get called lots of things but not that so much.

  11. #11
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    What they pay you enter in the Tendered textbox. You would then have to tab out or click somewhere else (to use the after update event for tendered) or have a calculate button. In Change textbox, you would have =Tendered - Total as the control's record source, assuming the amount tendered would always be equal to or greater than the amount owing. If you think it is possible that this could not be the case, such as when taking partial payments, you'd need to handle that.

    In doing this, the Change textbox cannot be bound to any table field because it would be a calculated control. If you must store the change amount, you'd need to do this with code.
    Last edited by Micron; 10-17-2018 at 09:41 AM. Reason: clarification

  12. #12
    markpastoril is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2018
    Posts
    62
    thanks sir, I will try this later, I would like to just when the user click the tab it calculates automatically.
    but how will the amount paid be posted in the amount paid?does this automatically be save?

  13. #13
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    I would like to just when the user click the tab it calculates automatically.
    then use the control's AfterUpdate event.
    how will the amount paid be posted in the amount paid?
    You have bound that field (Amount Tendered in picture?) to the table or select query that the form is bound to, yes? Then moving off of that control will update the record field it's bound to. That doesn't necessarily mean you will update the record. You still have to know that this part of your form works as it should. I'm thinking that maybe you should research bound forms in Access to gain more understanding of how this works.

  14. #14
    markpastoril is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2018
    Posts
    62
    yes my Tendered textbox is bound, and my Change textbox is bound just to calculate Tendered-Balance.
    My question is when the User is saving the form, I want my Amount Paid to post just the exact Balance amount of the bill,so it will be hidden in my query that the criteria is >0.
    it should Look like this:

    (Before the payment it will show on my form& query.)

    Amount Due Penalty DatePaid AmountPaid OR# Amount_Tendered Change Balance(Criteria: >0)
    150 7.50 0 157.50

    After payment
    This will not show in my forms listbox coz it returns 0.
    Amount Due Penalty DatePaid AmountPaid OR# Amount_Tendered Change Balance(Criteria: >0)
    150 7.50 10/18/2018 157.50 20180010 200 0

    Hope you could help me sir/maam, or you can give any suggestion?

  15. #15
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    I want my Amount Paid to post just the exact Balance
    If you mean post the balance to a table, it is generally considered bad practice to store calculations such as balance owing. You can easily end up with balance errors because a debit or credit fails to be applied (the balance fails to recalculate). It is best to allow queries/forms/reports to calculate the balance based on the sum of purchases minus the sum of payments.

    If you insist on saving the balance to a table, and the balance is calculated on the form by using a calculated control, then as I said, you'd need code to write the calculation to the table because the calculated control cannot be bound. Now you need another control to bind to the table field just to retrieve and show the balance field value that the form can show without that field (as already mentioned). It just makes no sense.

    Will suggest that when posting columns of information in your post that you click Go Advanced and insert a table. Not only does your data not line up with your column headings and is therefore difficult to relate to, you're showing 157.50 as the date paid in the first example.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Validating the table
    By XPeriment in forum Access
    Replies: 5
    Last Post: 08-10-2016, 09:43 AM
  2. Saving unbound textbox data
    By joecamel9166 in forum Forms
    Replies: 3
    Last Post: 02-22-2016, 09:42 PM
  3. Validating Email Address in VBA
    By Phil1 in forum Programming
    Replies: 4
    Last Post: 04-19-2012, 09:54 AM
  4. validating fields
    By bhatia.puja in forum Access
    Replies: 3
    Last Post: 07-28-2011, 05:24 AM
  5. Validating tables
    By JVagenheart in forum Database Design
    Replies: 1
    Last Post: 06-10-2006, 09:03 AM

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