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 
Field Type
Mark Pierce 
     
3 years ago
Hello,
I am having in issue with some code and a unit conversion. The field in the table MUD is called Mud_Weight and it is set as a double.

in my code I do a conversion of the value in Mud_Weight based on the unit type selected.

however, I am getting a type mismatch error, using debugging code, I am getting the message that the field Mud_Weight is set as a long, but I have triple checked and it is as a double.

Here is the code relevant to this issue:

DetailsForm_UnitPreferences2F
______________________________________________________________

Option Compare Database
Option Explicit

Dim PreviousSelectMudFluidWeightsValue As String

_______________________________________________________

Private Sub Form_Load()
   On Error GoTo ErrorHandler
PreviousSelectMudFluidWeightsValue = Me.SelectMudFluidWeights.value
Exit Sub
ErrorHandler:
    Debug.Print "Error Number: "; Err.Number; " Description: "; Err.Description
End Sub
_______________________________________________________

Private Sub SelectMudFluidWeights_AfterUpdate()
    On Error GoTo ErrorHandler

Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim rsHeader As DAO.Recordset
    Dim strSQL As String
    Dim currentIDHeader As Long
    Dim originalMud_Weight As Variant
    Dim originalEnv_Lube_Weight As Variant
    Dim originalMudFluidWeight As Variant
    Dim currentFieldName As Variant

' Retrieve the latest ID_Header value from ProjectT
    Debug.Print "Querying for latest ID_Header from ProjectT." ' Debugging
    Set rsHeader = db.OpenRecordset("SELECT TOP 1 ID_Header FROM ProjectT ORDER BY ID_Header DESC", dbOpenSnapshot)
    Debug.Print "Recordset for ID_Header obtained." ' Debugging
    
    If Not (rsHeader.EOF And rsHeader.BOF) Then
        currentIDHeader = rsHeader!ID_Header
        Debug.Print "Current ID_Header: " & currentIDHeader ' Debugging
    Else
        MsgBox "No records found in ProjectT.", vbExclamation
        Debug.Print "No records found in ProjectT." ' Debugging
        GoTo CleanUp
    End If
    
    rsHeader.Close
    Set rsHeader = Nothing

     ' Define the SQL query to select relevant records in Mud
    strSQL = "SELECT * FROM Mud WHERE HeaderID = " & currentIDHeader
    Debug.Print "SQL Query: " & strSQL
    Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)

    If Not (rs.EOF And rs.BOF) Then
        rs.MoveFirst
        Do While Not rs.EOF
            rs.Edit
        ' Perform conversions based on your conditions
                If PreviousSelectMudFluidWeightsValue = "lb/gal" And Me.SelectMudFluidWeights.value = "kg/m3" Then
                    Debug.Print "Converting lb/gal to kg/m3"
                    originalMud_Weight = rs!mud_Weight ' Store the original value
                    rs!mud_Weight = KgPerCubicMeterToLbPerGal(rs!mud_Weight)
                    LogChange "Mud", "mud_Weight", originalMud_Weight, rs!mud_Weight, Me.Name, currentIDHeader
                    Debug.Print "Step 2"
                
                ElseIf PreviousSelectMudFluidWeightsValue = "lb/gal" And Me.SelectMudFluidWeights.value = "g/m3" Then
                    Debug.Print "Converting lb/gal to g/m3"
                    ' Conversion from Lb Per Gal To GPer Cubic Centimeter
                    originalMud_Weight = rs!mud_Weight ' Store the original value
                    rs!mud_Weight = LbPerGalToGPerCubicCentimeter(rs!mud_Weight)
                    LogChange "Mud", "mud_Weight", originalMud_Weight, rs!mud_Weight, Me.Name, currentIDHeader
                
                 Debug.Print "Step 3"



_______________________________________________________

Module: MetricImperalConversion


' Mud/Fluid Weights..............................................................

' Conversions from Pounds per Gallon (lb/gal)
Function LbPerGalToKgPerCubicMeter(lbPerGal As Double) As Double
    LbPerGalToKgPerCubicMeter = Round(lbPerGal * 119.826427, 3)
End Function

Function LbPerGalToGPerCubicCentimeter(lbPerGal As Double) As Double
    LbPerGalToGPerCubicCentimeter = Round(LbPerGalToKgPerCubicMeter(lbPerGal) * 0.001, 3)
End Function

Function LbPerGalToLbPerCubicFoot(lbPerGal As Double) As Double
    LbPerGalToLbPerCubicFoot = LbPerGalToKgPerCubicMeter(lbPerGal) * 0.06242796
End Function

Kevin Robertson  @Reply  
          
3 years ago
Which line of code is causing the issue?
Mark Pierce OP  @Reply  
     
3 years ago
this line:
If PreviousSelectMudFluidWeightsValue = "lb/gal" And Me.SelectMudFluidWeights.value = "kg/m3" Then
Kevin Yip  @Reply  
     
3 years ago
Does your custom function KgPerCubicMeterToLbPerGal(rs!mud_Weight) receive and return the right data type?  If not, it will cause a "type mismatch" error.  If a function expects an argument with one data type but receives a different type, and/or returns a data type that is different from what is expected to be returned, it will cause the error.
Mark Pierce OP  @Reply  
     
3 years ago
Kevin,
No it does not, it should be a double but it is a long.
Mark Pierce OP  @Reply  
     
3 years ago
I have the exact same setup for other unit conversions, and they are working fine. So, I feel it an issue with the calculation in the module or a corrupt Mud_weight field.
Kevin Yip  @Reply  
     
3 years ago
When the line is highlighted in yellow during debugging, check the values of all the variables by hovering your mouse over them in the code.  "Type mismatch" errors are usually easy to spot because they usually involve text and numeric variables (dates and Yes/No fields are considered "numbers" too).  If X is numeric, then this code:

     If X = "text" Then ...

will cause a type mismatch error.

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/6/2026 11:24:43 AM. PLT: 0s