473,395 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to calculate depending on database value?

i have two text box's and compare from two columns in database the result auto show another text box and then following below code using my program
Expand|Select|Wrap|Line Numbers
  1. Public Sub cal()
  2.         Dim sum As Double
  3.         Dim res As Double
  4.         With Common_Class.GetSrvRec("i_Unit_Con", "", "QTY_UNIT='" & txtitemunit1.Text.Trim & "'")
  5.             If .Fields("QTY_UNIT").Value = txtitemunit1.Text Then
  6.                 If .Fields("RATE_UNIT").Value = txtitemunit2.Text Then
  7.                     sum = CInt(txtitemquant.Text) * 1 * CInt(txtitemrate.Text) * CInt(110%) / 100
  8.                     res = sum
  9.                     txtitempmv.Text = res
  10.                 Else
  11.                     sum = CInt(txtitemquant.Text) * 1000 * CInt(txtitemrate.Text) * CInt(110%) / 100
  12.                     res = sum
  13.                     txtitempmv.Text = res
  14.                 End If
  15.             End If
  16.         End With
  17.     End Sub
Attached Files
File Type: txt VME.txt (1.7 KB, 431 views)
Mar 10 '11 #1
3 2099
Public Sub unitcal()
Dim sum As Double
Dim res As Double
Dim dr As OleDbDataReader
Dim qry As String
Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " & "Data Source=C:\Focus.net\ServerDB.mdb" & ";Persist Security Info=False")
qry = ("SELECT QTY_UNIT,RATE_UNIT,FACTOR FROM i_Unit_Con WHERE QTY_UNIT ='" & txtitemunit1.Text & "' AND RATE_UNIT ='" & txtitemunit2.Text & "'")
Dim cmd As New OleDbCommand(qry, conn)
conn.Open()
dr = cmd.ExecuteReader()
If dr.Read Then
If (dr("QTY_UNIT") = (txtitemunit1.Text) AndAlso dr("RATE_UNIT") = (txtitemunit2.Text)) = (dr("FACTOR") = 1) Then
sum = CInt(txtitemquant.Text) * 1 * CInt(txtitemrate.Text) * CInt(110%) / 100
res = sum
txtitempmv.Text = res
ElseIf (dr("QTY_UNIT") = (txtitemunit1.Text) AndAlso dr("RATE_UNIT") = (txtitemunit2.Text)) = (dr("FACTOR") = 1000) Then
sum = CInt(txtitemquant.Text) * 1000 * CInt(txtitemrate.Text) * CInt(110%) / 100
res = sum
txtitempmv.Text = res

ElseIf (dr("QTY_UNIT") = (txtitemunit1.Text) AndAlso dr("RATE_UNIT") = (txtitemunit2.Text)) = (dr("FACTOR") = 144) Then
sum = CInt(txtitemquant.Text) * 144 * CInt(txtitemrate.Text) * CInt(110%) / 100
res = sum
txtitempmv.Text = res
ElseIf (dr("QTY_UNIT") = (txtitemunit1.Text) AndAlso dr("RATE_UNIT") = (txtitemunit2.Text)) = (dr("FACTOR") = 0.001) Then
sum = CInt(txtitemquant.Text) * 0.001 * CInt(txtitemrate.Text) * CInt(110%) / 100
res = sum
txtitempmv.Text = res
ElseIf (dr("QTY_UNIT") = (txtitemunit1.Text) AndAlso dr("RATE_UNIT") = (txtitemunit2.Text)) = (dr("FACTOR") = 0.01) Then
sum = CInt(txtitemquant.Text) * 0.01 * CInt(txtitemrate.Text) * CInt(110%) / 100
res = sum
txtitempmv.Text = res
ElseIf (dr("QTY_UNIT") = (txtitemunit1.Text) AndAlso dr("RATE_UNIT") = (txtitemunit2.Text)) = (dr("FACTOR") = 100) Then
sum = CInt(txtitemquant.Text) * 100 * CInt(txtitemrate.Text) * CInt(110%) / 100
res = sum
txtitempmv.Text = res
ElseIf (dr("QTY_UNIT") = (txtitemunit1.Text) AndAlso dr("RATE_UNIT") = (txtitemunit2.Text)) = (dr("FACTOR") = 12) Then
sum = CInt(txtitemquant.Text) * 12 * CInt(txtitemrate.Text) * CInt(110%) / 100
res = sum
txtitempmv.Text = res
ElseIf (dr("QTY_UNIT") = (txtitemunit1.Text) AndAlso dr("RATE_UNIT") = (txtitemunit2.Text)) = (dr("FACTOR") = 0.08333) Then
sum = CInt(txtitemquant.Text) * 0.08333 * CInt(txtitemrate.Text) * CInt(110%) / 100
res = sum
txtitempmv.Text = res
ElseIf (dr("QTY_UNIT") = (txtitemunit1.Text) AndAlso dr("RATE_UNIT") = (txtitemunit2.Text)) = (dr("FACTOR") = 0.45359) Then
sum = CInt(txtitemquant.Text) * 0.45359 * CInt(txtitemrate.Text) * CInt(110%) / 100
res = sum
txtitempmv.Text = res
ElseIf (dr("QTY_UNIT") = (txtitemunit1.Text) AndAlso dr("RATE_UNIT") = (txtitemunit2.Text)) = (dr("FACTOR") = 2.2046) Then
sum = CInt(txtitemquant.Text) * 2.2046 * CInt(txtitemrate.Text) * CInt(110%) / 100
res = sum
txtitempmv.Text = res
ElseIf (dr("QTY_UNIT") = (txtitemunit1.Text) AndAlso dr("RATE_UNIT") = (txtitemunit2.Text)) = (dr("FACTOR") = 0.3048) Then
sum = CInt(txtitemquant.Text) * 0.3048 * CInt(txtitemrate.Text) * CInt(110%) / 100
res = sum
txtitempmv.Text = res
ElseIf (dr("QTY_UNIT") = (txtitemunit1.Text) AndAlso dr("RATE_UNIT") = (txtitemunit2.Text)) = (dr("FACTOR") = 3.2808) Then
sum = CInt(txtitemquant.Text) * 3.2808 * CInt(txtitemrate.Text) * CInt(110%) / 100
res = sum
txtitempmv.Text = res
Else
End If

End If
conn.Close()
End Sub
Mar 11 '11 #2
First off...walk away from the horrible if elseif redundancy. Clean it up, and simplify.

Instead, step back and analyze the calcs done.
How many are there?
Can you break them into logical groupings?
Are they defined correctly?
(i.e. dr("RATE_UNIT") = (txtitemunit2.Text)) = (dr("FACTOR") = 0.001)
This does not seem to make any sense to me. RATE_UNIT is a boolean value???

Build a nice select case based on the analysis.

If you post back what the heck: dr("RATE_UNIT") = (txtitemunit2.Text)) = (dr("FACTOR") = 0.001 means, then I'll give it a try with you.
Mar 25 '11 #3
For instance...

Expand|Select|Wrap|Line Numbers
  1. Dim factorCheck As Decimal = dr("FACTOR")
  2. Dim CalcType As Integer
  3. Dim qtyCheck As Boolean = dr("QTY_UNIT") = (txtitemunit1.Text)
  4. If qtyCheck = True Then
  5.     'By refactoring just a little, its clear that this must be true in all of your conditions.
  6.  
  7.     Select Case factorCheck
  8.     Case 1000
  9.         CalcType = "FactorBy1000"
  10.         'I'd use some enumeration instead of strings.  But you see the idea.
  11.     Case 144
  12.         CalcType = "FactorBy144"
  13.     Case 1
  14.         CalcType = "FactorBy1"
  15.     Case 100
  16.         CalcType = "FactorBy100"
  17.  
  18.         'etc...
  19.     Case Else
  20.         'No calc found
  21.         CalcType = "NOT FOUND"
  22.     End Select
  23.  
  24.     Dim bCalcDone = True
  25.  
  26.     Select Case CalcType
  27.     Case "NOT FOUND"
  28.         bCalcDone = False  'or error it out
  29.     Case "FactorBy1"
  30.         sum = CInt(txtitemquant.Text) * 1 * CInt(txtitemrate.Text) * CInt(110%) / 100
  31.         bCalcDone = True
  32.     Case "FactorBy144"
  33.         sum = CInt(txtitemquant.Text) * 144 * CInt(txtitemrate.Text) * CInt(110%) / 100
  34.         bCalcDone = True
  35.     End Select
  36.  
  37.     If bCalcDone = True Then
  38.     res = sum
  39.     txtitempmv.Text = res
  40.     End If
  41. End If
Mar 25 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Kerri | last post by:
Hi, I have an aspx page with a drop down. This is a page where a user can edit their user profile. When they go to this page, I want the dropdownlists to default to what it is in teh user...
4
by: dmiller23462 | last post by:
So here's my problem.....I need to set up different email distributions based on which option in the following Select form has been chosen....For instance if "Putaway" is chosen it needs to email...
2
by: Ivan | last post by:
Hi All, I just wanna ask one simply question.. Is there anyone know how to calculate the output value from a string like that?? Dim strFormula As String = "20+30+(100+20+30)*0.1" Dim...
2
by: Renato Cramer | last post by:
Hello All, There is data of several enterprises (ours clients) in a single database. All tables have a column on primary key what identify the enterprise called id_enterprise. My objective is...
2
by: Michael Bohman | last post by:
Hi, i have a small problem with assigning a database value to a RadioButtonList control. On my form i have 3 user admin=1, premium=2 and basic=3, theese values is stored in an access database in a...
1
by: jwright1 | last post by:
I am trying to set the selected value of a radio button list from a value stored in a table of a database. using vb codebehind asp.net. I tried binding the selected index item and selected index...
1
by: Mario1776 | last post by:
I have several textboxes programmatically bound to several database fields. For some reason, when I move from one particular textbox (lets say "FieldG") to another control on the form the original...
2
by: beish1 | last post by:
I want to have simple aspx page which reads a database record, selecting it according to parameter received (ie request.querystring). I then want it to check a field on the database record and...
1
by: arunbojan | last post by:
Hi All, I have an editable datagrid with 10 columns, one of those is ddl column which I used for showing the status.... By default all the columns are readonly, when user clicks edit,...
2
by: thecheyenne | last post by:
Hi and good evening / good morning (depending on your location on this planet) Despite my limited - o.k, non-existing - knowledge of vba, I'd like to design a database to help with the admin of...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.