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
727 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

752 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