Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Unit Conversion
Mark Pierce 
     
3 years ago
Do any of you know of some examples that I can study that allows the user to select Imperial or metric measurements and once they do it converts designated fields?

This project is almost done and that requirement has been added.
Adam Schwanz  @Reply  
           
3 years ago
How complex is it going to be? If it's just imperial or metric and not a lot of fields, you could just use a query to do the calculations for both then display which one they want. If it's a lot more conversions you might want a table storing what the conversion is from one to another. Or you could make a global module/function to do the conversions, just kind of depends how complex it needs to be.
Scott Axton  @Reply  
        
3 years ago
Kevin Yip  @Reply  
     
3 years ago
If rounding errors are not your concern, then Scott's method of creating custom functions to do the conversations should suffice.  But if conversions are done often, and back and forth, rounding errors will result: 30 feet -> 9.2m -> 30.2ft -> etc.  

If I were in this situation and unsure if rounding errors would be a problem, and if I wanted to cover all bases, I would store each unit of measure separately, possibly in a one-to-many table relationship.  The table on the "many" side would store all the units of measure (UOM) necessary, with the precision I decide based on my business needs -- some may need 2 decimal places, others more.  For instance:

Unit     UOM
100      ft
30.48    m
3048     cm
33.33    yd
0.01894  mile

As you see, you may not know how many decimal places you need to retain certain precision.  And that's one reason automatic conversions is not preferred, because Access doesn't know how many decimal places you need.  

When you have all your UOMs entered in the table, you just do table lookups (with DLookup(), for instance) to obtain the desired UOMs.  No conversion is necessary, so no precision is lost.
Mark Pierce OP  @Reply  
     
3 years ago
There are about 200 fields that need conversion once the user selects the different units. I have created the process to change the field label for all selections using the translation concept from Richard. I am having trouble figuring out how to run the conversion. I will attach some pictures.
Mark Pierce OP  @Reply  
     
3 years ago

Mark Pierce OP  @Reply  
     
3 years ago

Mark Pierce OP  @Reply  
     
3 years ago
the first pictures is the form interface for the user to select the units for each of the types of data. The second is just a snippet of the matrix of the form, table, field, and field label that get converted. Then the columns show what the field should be converted to based on what unit the user has selected.
Mark Pierce OP  @Reply  
     
3 years ago
here is the code I use in the onload event to update the field labels on a form:

Private Sub Form_Load()
    On Error GoTo ErrHandler

    ' Check if the form and controls exist
    If Forms("UnitPreferences2F") Is Nothing Then
        MsgBox "Form 'UnitPreferences2F' does not exist!", vbExclamation, "Error"
        Exit Sub
    End If
    
    With Forms("UnitPreferences2F")
        ' Check if the control "WeightMass" exists
        If Not (.Controls("WeightMass") Is Nothing) Then
            If .Controls("WeightMass").value = "lb" Then
                EPWeightsLabel.caption = "Weight (lbs):"
            Else
                ' Metric
                EPWeightsLabel.caption = "Weight (kg):"
            End If
        End If

        ' Check if the control "Diameters" exists
        If Not (.Controls("Diameters") Is Nothing) Then
            If .Controls("Diameters").value = "in" Then
                ExpandedODLabel.caption = "OD (in):"
                ExpandedIDLabel.caption = "ID (in):"
            Else
                ' Metric
                ExpandedODLabel.caption = "OD (mm):"
                ExpandedIDLabel.caption = "ID (mm):"
            End If
        End If

        ' Check if the control "DistancesLenghts" exists
        If Not (.Controls("DistancesLenghts") Is Nothing) Then
            If .Controls("DistancesLenghts").value = "ft" Then
                EstLenLabel.caption = "Est. Len. (ft):"
            Else
                ' Metric
                EstLenLabel.caption = "Est. Len. (m):"
            End If
        End If

        ' Check if the control "Pressure" exists
        If Not (.Controls("Pressure") Is Nothing) Then
            If .Controls("Pressure").value = "psi" Then
                PreExpandedYieldLabel.caption = "Burst Rating (psi):"
            Else
                ' Metric
                PreExpandedYieldLabel.caption = "Burst Rating (Pa):"
            End If
        End If
    End With

ExitSub:
    Exit Sub

ErrHandler:
    LogError Err.Number, Err.Description, "Form_Load", Me.Name
    Resume ExitSub
End Sub
Mark Pierce OP  @Reply  
     
3 years ago
I have created a module to store the unit conversion: here is that code:

Option Compare Database
Option Explicit


Function FeetToMeters(feet As Double) As Double
    ' 1 foot equals 0.3048 meters
    FeetToMeters = feet * 0.3048
End Function

Function MetersToFeet(meters As Double) As Double
    ' 1 meter equals 3.2808399 feet
    MetersToFeet = meters * 3.2808399
End Function

Function InchesToCentimeters(inches As Double) As Double
    ' 1 inch equals 2.54 centimeters
    InchesToCentimeters = inches * 2.54
End Function

Function CentimetersToInches(centimeters As Double) As Double
    ' 1 centimeter equals 0.393701 inches
    CentimetersToInches = centimeters * 0.393701
End Function

Function InchesToMillimeters(inches As Double) As Double
    ' 1 inch equals 25.4 millimeters
    InchesToMillimeters = inches * 25.4
End Function
Function MillimetersToInches(mm As Double) As Double
    ' 1 inch equals 25.4 millimeters, so to convert mm to inches, divide by 25.4
    MillimetersToInches = mm / 25.4
End Function

Function MillimetersToCentimeters(mm As Double) As Double
    ' 1 centimeter equals 10 millimeters
    MillimetersToCentimeters = mm / 10
End Function
Function CentimetersToMillimeters(cm As Double) As Double
    ' 1 centimeter equals 10 millimeters
    CentimetersToMillimeters = cm * 10
End Function




' Tubular Weights
Function LbPerFootToKgPerMeter(lbPerFoot As Double) As Double
    ' 1 pound/foot equals 1.48816 kilograms/meter
    LbPerFootToKgPerMeter = lbPerFoot * 1.48816
End Function

Function KgPerMeterToLbPerFoot(kgPerMeter As Double) As Double
    ' 1 kilogram/meter equals 0.671968975 pounds/foot
    KgPerMeterToLbPerFoot = kgPerMeter * 0.671968975
End Function

' Mud/Fluid Weights
Function LbPerGalToKgPerCubicMeter(lbPerGal As Double) As Double
    ' 1 pound/gallon (US) equals 119.826427 kilograms/cubic meter
    LbPerGalToKgPerCubicMeter = lbPerGal * 119.826427
End Function

Function KgPerCubicMeterToLbPerGal(kgPerCubicMeter As Double) As Double
    ' 1 kilogram/cubic meter equals 0.0083454044873 pounds/gallon (US)
    KgPerCubicMeterToLbPerGal = kgPerCubicMeter * 0.0083454044873
End Function

Function GPerCubicCentimeterToLbPerCubicFoot(gPerCubicCentimeter As Double) As Double
    ' 1 gram/cubic centimeter equals 62.427961 pounds/cubic foot
    GPerCubicCentimeterToLbPerCubicFoot = gPerCubicCentimeter * 62.427961
End Function

Function LbPerCubicFootToGPerCubicCentimeter(lbPerCubicFoot As Double) As Double
    ' 1 pound/cubic foot equals 0.016018463 kilograms/cubic centimeter
    LbPerCubicFootToGPerCubicCentimeter = lbPerCubicFoot * 0.016018463
End Function

Function PsiPerThousandFeetToKPaPerMeter(psiPer1000ft As Double) As Double
    ' 1 psi/1000 feet equals 0.02088543423315 kPa/meter
    PsiPerThousandFeetToKPaPerMeter = psiPer1000ft * 0.02088543423315
End Function

Function KPaPerMeterToPsiPerThousandFeet(kPaPerMeter As Double) As Double
    ' 1 kPa/meter equals 47.88025898 psi/1000 feet
    KPaPerMeterToPsiPerThousandFeet = kPaPerMeter * 47.88025898
End Function

' Pressure
Function PsiToPascals(psi As Double) As Double
    ' 1 psi equals 6894.75729 Pascals
    PsiToPascals = psi * 6894.75729
End Function

Function PascalsToPsi(pa As Double) As Double
    ' 1 Pascal equals 0.0001450377377 psi
    PascalsToPsi = pa * 0.0001450377377
End Function

Function BarToPascals(bar As Double) As Double
    ' 1 Bar equals 100000 Pascals
    BarToPascals = bar * 100000
End Function

Function PascalsToBar(pa As Double) As Double
    ' 1 Pascal equals 1e-5 Bars
    PascalsToBar = pa * 0.00001
End Function

Function AtmToPascals(atm As Double) As Double
    ' 1 Atmosphere equals 101325 Pascals
    AtmToPascals = atm * 101325
End Function

Function PascalsToAtm(pa As Double) As Double
    ' 1 Pascal equals 9.86923e-6 Atmospheres
    PascalsToAtm = pa * 0.00000986923
End Function

Function KgPerCm2ToPascals(kgPerCm2 As Double) As Double
    ' 1 kilogram-force/square centimeter equals 98066.5 Pascals
    KgPerCm2ToPascals = kgPerCm2 * 98066.5
End Function

Function PascalsToKgPerCm2(pa As Double) As Double
    ' 1 Pascal equals 1.01972e-5 kilograms-force/square centimeter
    PascalsToKgPerCm2 = pa * 0.0000101972
End Function

' Volume Conversions
Function GallonsToCubicFeet(gallons As Double) As Double
    ' 1 gallon equals 0.133681 cubic feet
    GallonsToCubicFeet = gallons * 0.133681
End Function

Function CubicFeetToGallons(cubicFeet As Double) As Double
    ' 1 cubic foot equals 7.48052 gallons
    CubicFeetToGallons = cubicFeet * 7.48052
End Function

Function BarrelsToCubicMeters(barrels As Double) As Double
    ' 1 barrel equals 0.158987 cubic meters
    BarrelsToCubicMeters = barrels * 0.158987
End Function

Function CubicMetersToBarrels(cubicMeters As Double) As Double
    ' 1 cubic meter equals 6.28981 barrels
    CubicMetersToBarrels = cubicMeters * 6.28981
End Function

' Weight/Mass Conversions
Function PoundsToKilograms(pounds As Double) As Double
    ' 1 pound equals 0.453592 kilograms
    PoundsToKilograms = pounds * 0.453592
End Function

Function KilogramsToPounds(kilograms As Double) As Double
    ' 1 kilogram equals 2.20462 pounds
    KilogramsToPounds = kilograms * 2.20462
End Function

' Torque Conversions
Function FootPoundsToNewtonMeters(ftLbs As Double) As Double
    ' 1 foot-pound equals 1.35582 Newton meters
    FootPoundsToNewtonMeters = ftLbs * 1.35582
End Function

Function NewtonMetersToFootPounds(nM As Double) As Double
    ' 1 Newton meter equals 0.737562 foot-pounds
    NewtonMetersToFootPounds = nM * 0.737562
End Function

' Flow Rate Conversions
Function GallonsPerMinuteToBarrelsPerMinute(galPerMin As Double) As Double
    ' 1 gallon/minute equals 0.0238095 barrels/minute
    GallonsPerMinuteToBarrelsPerMinute = galPerMin * 0.0238095
End Function

Function BarrelsPerMinuteToGallonsPerMinute(bblPerMin As Double) As Double
    ' 1 barrel/minute equals 42 gallons/minute
    BarrelsPerMinuteToGallonsPerMinute = bblPerMin * 42
End Function

Function LitersPerMinuteToGallonsPerMinute(lPerMin As Double) As Double
    ' 1 liter/minute equals 0.264172 gallons/minute
    LitersPerMinuteToGallonsPerMinute = lPerMin * 0.264172
End Function

Function GallonsPerMinuteToLitersPerMinute(galPerMin As Double) As Double
    ' 1 gallon/minute equals 3.78541 liters/minute
    GallonsPerMinuteToLitersPerMinute = galPerMin * 3.78541
End Function

Function LitersPerSecondToCubicFeetPerMinute(lPerSec As Double) As Double
    ' 1 liter/second equals 2.11888 cubic feet/minute
    LitersPerSecondToCubicFeetPerMinute = lPerSec * 2.11888
End Function

Function CubicFeetPerMinuteToLitersPerSecond(cubicFeetPerMin As Double) As Double
    ' 1 cubic foot/minute equals 0.471947 liters/second
    CubicFeetPerMinuteToLitersPerSecond = cubicFeetPerMin * 0.471947
End Function

Function CubicMetersPerMinuteToCubicFeetPerMinute(m3PerMin As Double) As Double
    ' 1 cubic meter/minute equals 35.3147 cubic feet/minute
    CubicMetersPerMinuteToCubicFeetPerMinute = m3PerMin * 35.3147
End Function

Function CubicFeetPerMinuteToCubicMetersPerMinute(cubicFeetPerMin As Double) As Double
    ' 1 cubic foot/minute equals 0.0283168 cubic meters/minute
    CubicFeetPerMinuteToCubicMetersPerMinute = cubicFeetPerMin * 0.0283168
End Function

Function CubicFeetPerHourToCubicMetersPerHour(cubicFeetPerHr As Double) As Double
    ' 1 cubic foot/hour equals 0.0283168 cubic meters/hour
    CubicFeetPerHourToCubicMetersPerHour = cubicFeetPerHr * 0.0283168
End Function

Function CubicMetersPerHourToCubicFeetPerHour(m3PerHr As Double) As Double
    ' 1 cubic meter/hour equals 35.3147 cubic feet/hour
    CubicMetersPerHourToCubicFeetPerHour = m3PerHr * 35.3147
End Function
Kevin Yip  @Reply  
     
3 years ago
You can create a table that stores info about each conversion type: ID, description, from UOM, to UOM, multiplier, etc.   Then you create just *one* function that takes a conversion ID and a "from UOM" value.  The function looks up the ID, gets the multiplier, and returns the result.  If a division is involved, use the inverse value as the multiplier.  E.g. to divide by 4, use 0.25 as the multiplier.

This not only reduces the amount of code, but also lets you add new conversion types by just adding to the table instead of adding new code.
Mark Pierce OP  @Reply  
     
3 years ago
Thanks Kevin! This helps me wrap my head around a process to attack this problem. I will work on this.
Mark Pierce OP  @Reply  
     
3 years ago
I think this would be a great video for Richard.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/1/2026 5:42:46 PM. PLT: 0s