Solved

Syntax to divide form values and update table as percentage with 2 decimals; close form and open another form

Posted on 2013-12-12
4
702 Views
Last Modified: 2013-12-12
The following is the VBA code I wrote to update a table from a form.  I have two questions.

First I want to divide the form entries:

Me.txtXM / Me.txtTM and update the table field XMPERC as a Percentage with 2 decimal places.

Second I need to close the open form frmSR1 and reopen frmEvalMenu.

What is the syntax for this?

Private Sub Command27_Click()
 'Update tbl_SR1 with Blood Data
     Dim db As DAO.Database
     Dim rs As DAO.Recordset
     
     Set db = CurrentDb
     Set rs = db.OpenRecordset("tbl_SR1", dbOpenDynaset)
     
     rs.AddNew
     rs.Fields("ProviderID") = Me.Text16
     rs.Fields("XM") = Me.txtXM
     rs.Fields("TX") = Me.txtTX
     
     rs.Fields("RptDate") = Me.Text20
     rs.Fields("XMPERC") = Format(Me.txtXM / Me.txtTX, "000.00")
     
     rs.Update
     
     rs.Close
     
     Set rs = Nothing
     
     'Clear All Selections and Return to Physician Evaluation Menu
     
     DoCmd.Close acForm, "frm_SR1"
     
 End Sub

thanks

Glen
0
Comment
Question by:GPSPOW
  • 3
4 Comments
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 500 total points
ID: 39715673
Take a look at the tables design, and ensure that the XMPERC field has it's Length property set to Double (this property is found in the box under the list of fields, and defaults to Integer)

Then, write your equation like this:

rs.Fields("XMPERC") =  Me.txtXM / Me.txtTX

And handle the formatting through the field properties and/or through the textboxes used to display it.

If txtTX can ever be null or zero, you would need special handling to avoid errors:

If NZ(me.txtTX, 0) = 0 then
    ' do something to handle nulls and zeros to avoid division errors
Else
    if isnumeric( Me.txtXM ) = true then
         rs.Fields("XMPERC") =  Me.txtXM / Me.txtTX
    else
        ' Handle null or non-numeric txtXM)
    end if 
End if

Open in new window

0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39715679
<< Second I need to close the open form frmSR1 and reopen frmEvalMenu. >>

You've got the first part figured out (closing frmSR1).  This will open frmEvalMenu:

Docmd.OpenForm "EvalMenu"

But I recommend doing it in the reverse order.  Open the menu first, then close the frmSR1.  That way, you won't be left looking at a blank screen if frmEvalMenu takes a while to load:

  Docmd.OpenForm "EvalMenu"
  DoCmd.Close acForm, "frm_SR1"

Open in new window

0
 

Author Closing Comment

by:GPSPOW
ID: 39715682
Both solutions worked perfectly

Thanks

glen
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39715690
Glad to help :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now