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
722 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

735 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