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
691 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
Comment Utility
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
Comment Utility
<< 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
Comment Utility
Both solutions worked perfectly

Thanks

glen
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
Glad to help :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

771 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

11 Experts available now in Live!

Get 1:1 Help Now