+ Reply to Thread
Results 1 to 4 of 4

VBA to make numeric value short text

Hybrid View

  1. #1
    Registered User
    Join Date
    07-27-2021
    Location
    NL
    MS-Off Ver
    2013
    Posts
    8

    VBA to make numeric value short text

    Good morning everyone,

    I'm getting Data type mismatch in criteria expression on a VBA that I'm using, I'm not the author, it was working fine until upgrade from office 2013 to 365.

    The code:
    Function UpdateLocalCurrencyAmount()
    On Error GoTo Err_UpdateLocalCurrencyAmount
    
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Dim rst2 As DAO.Recordset
    Dim qdf As DAO.QueryDef
    Dim sql As String
    
    Dim RRTerCode As String
    Dim RRMonth As String
    Dim RRServiceType As String
    Dim CB_Amount As Double
    
    
    
    Set dbs = CurrentDb
    
    sql = "SELECT [Financial Reporting - Recharge Records].[Territory-code], [Financial Reporting - Recharge Records].[Process Month (YYMM)], [Financial Reporting - Recharge Records].[Service Type] " & _
    "FROM [Financial Reporting - Recharge Records] " & _
    "ORDER BY [Financial Reporting - Recharge Records].[Territory-code], [Financial Reporting - Recharge Records].[Process Month (YYMM)], [Financial Reporting - Recharge Records].[Service Type];"
    
    Set qdf = dbs.CreateQueryDef("", sql)
    Set rst = qdf.OpenRecordset
    
    
    If rst.EOF = False Then
        
        Do Until rst.EOF
    
            CB_Amount = -1
            
            RRTerCode = rst.Fields("Territory-code")
            RRMonth = rst.Fields("Process Month (YYMM)")
            RRServiceType = Nz(rst.Fields("Service Type"), "")
    
            
            'first lookup if a fixed amount was set at the period in question
            
            sql = "SELECT Mtbl_02_Recharge_FixedAmounts.[Fixed amount for all Recharges (Provide in local currency)] " & _
                    "FROM Mtbl_02_Recharge_FixedAmounts " & _
                    "WHERE (((Mtbl_02_Recharge_FixedAmounts.TerrCode_FA)='" & RRTerCode & "') AND ((Mtbl_02_Recharge_FixedAmounts.[Service Type])='" & RRServiceType & "') AND ((Mtbl_02_Recharge_FixedAmounts.[YYMM First month of New Amount])<=" & RRMonth & ") AND ((Mtbl_02_Recharge_FixedAmounts.[YYMM Last month of Fixed Amount])>=" & RRMonth & "));"

    Using debug.pring I know that second sql string results in this:

    SELECT Mtbl_02_Recharge_FixedAmounts.[Fixed amount for all Recharges (Provide in local currency)] FROM Mtbl_02_Recharge_FixedAmounts WHERE (((Mtbl_02_Recharge_FixedAmounts.TerrCode_FA)='602') AND ((Mtbl_02_Recharge_FixedAmounts.[Service Type])='RL') AND ((Mtbl_02_Recharge_FixedAmounts.[YYMM First month of New Amount])<=1904) AND ((Mtbl_02_Recharge_FixedAmounts.[YYMM Last month of Fixed Amount])>=1904));

    If my understanding is correct the error comes from 1904 value that is numeric in sql while Mtbl_02_Recharge_FixedAmounts.[YYMM Last month of Fixed Amount] is stored as short text.
    I tried to modify like this:
            sql = "SELECT Mtbl_02_Recharge_FixedAmounts.[Fixed amount for all Recharges (Provide in local currency)] " & _
                    "FROM Mtbl_02_Recharge_FixedAmounts " & _
                    "WHERE (((Mtbl_02_Recharge_FixedAmounts.TerrCode_FA)='" & RRTerCode & "') AND ((Mtbl_02_Recharge_FixedAmounts.[Service Type])='" & RRServiceType & "') AND ((Mtbl_02_Recharge_FixedAmounts.[YYMM First month of New Amount])<='" & RRMonth & "') AND ((Mtbl_02_Recharge_FixedAmounts.[YYMM Last month of Fixed Amount])>='" & RRMonth & "'));"
    But it makes no difference. I'm not so good with VBA in access, I don't think I can solve it on my own, please help!

  2. #2
    Registered User
    Join Date
    03-19-2022
    Location
    TBD
    MS-Off Ver
    O365
    Posts
    46

    Re: VBA to make numeric value short text

    Hi Marcinrd,

    Since RRMonth is already declared as string, but if the source data is numeric then the problem could be at the expression : RRMonth = rst.Fields("Process Month (YYMM)").
    My guess is the value stored in Process Month (YYMM) field may be a numeric value. You may try to use function CStr() to convert numeric data type to string data type.

    Please try to change RRMonth = rst.Fields("Process Month (YYMM)") to RRMonth = CStr(rst.Fields("Process Month (YYMM)")).
    Another way is you may try to run in debug mode, so it will show the line that caused an error.

  3. #3
    Registered User
    Join Date
    07-27-2021
    Location
    NL
    MS-Off Ver
    2013
    Posts
    8

    Re: VBA to make numeric value short text

    Thank you chocolate_kitten! It put me on right track
    CStr(rst.Fields("Process Month (YYMM)")) alone didn't work but combined with my first attempt of adding ' in sql those two methods together finally gave the right result!

  4. #4
    Registered User
    Join Date
    03-19-2022
    Location
    TBD
    MS-Off Ver
    O365
    Posts
    46

    Re: VBA to make numeric value short text

    congratulations!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Want to make short a formula
    By Mofasol in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-16-2020, 10:57 PM
  2. Replies: 2
    Last Post: 01-16-2018, 06:52 PM
  3. How to make the short border?
    By ajing in forum Excel General
    Replies: 3
    Last Post: 01-27-2016, 02:56 PM
  4. Userform find/search text, can i make it's short code ?
    By johnreid7477 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-03-2014, 04:28 PM
  5. [SOLVED] How to Short sheet name by two kinds of sheets names( alpabet then numeric )
    By herukuncahyono in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-19-2013, 12:16 AM
  6. [SOLVED] VBA code to make multiple text boxes numeric entry only
    By JAL_0110 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-20-2013, 01:57 AM
  7. Make Excel allow numeric fields as text
    By Michael A. Covington in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-15-2006, 11:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1