[COLOR=#0000ff]Function ChangeQuantUnit(xMaterial, xquant_a, xunit_a, xunit_b)[/COLOR]
[COLOR=#0000ff]Static M As Recordset, Mat_SQL, quant_a, unit_a, unit_b, LB2KG, FT2MTR[/COLOR]
[COLOR=#0000ff]LB2KG = 2.204623 'LB/kg[/COLOR]
[COLOR=#0000ff]FT2MTR = 3.28084 'FT/MTR[/COLOR]
[COLOR=#0000ff]Select Case Trim(UCase(xunit_a))[/COLOR]
[COLOR=#0000ff] Case "LB" ' if input quant is LB, transform xquant_a first to KG (and then -> xunit_b)[/COLOR]
[COLOR=#0000ff] quant_a = xquant_a / LB2KG[/COLOR]
[COLOR=#0000ff] unit_a = "KG"[/COLOR]
[COLOR=#0000ff] Case "FT" ' if input quant is FT, transform xquant_a first to MTR (and then -> xunit_b)[/COLOR]
[COLOR=#0000ff] quant_a = xquant_a / FT2MTR[/COLOR]
[COLOR=#0000ff] unit_a = "MTR"[/COLOR]
[COLOR=#0000ff] Case "K"[/COLOR]
[COLOR=#0000ff] quant_a = xquant_a[/COLOR]
[COLOR=#0000ff] unit_a = "KG"[/COLOR]
[COLOR=#0000ff] Case Else[/COLOR]
[COLOR=#0000ff] quant_a = xquant_a[/COLOR]
[COLOR=#0000ff] unit_a = xunit_a[/COLOR]
[COLOR=#0000ff]End Select[/COLOR]
[COLOR=#0000ff]Select Case Trim(UCase(xunit_b))[/COLOR]
[COLOR=#0000ff] Case "LB" ' if output quant is LB, set first transformation to xquant_a -> KG (and then -> LB)[/COLOR]
[COLOR=#0000ff] unit_b = "KG"[/COLOR]
[COLOR=#0000ff] Case "FT" ' if output quant is FT, set first transformation to xquant_a -> MTR (and then -> FT)[/COLOR]
[COLOR=#0000ff] unit_b = "MTR"[/COLOR]
[COLOR=#0000ff] Case "K"[/COLOR]
[COLOR=#0000ff] unit_b = "KG"[/COLOR]
[COLOR=#0000ff] Case Else[/COLOR]
[COLOR=#0000ff] unit_b = xunit_b[/COLOR]
[COLOR=#0000ff]End Select[/COLOR]
[COLOR=#0000ff]If unit_a = unit_b Then[/COLOR]
[COLOR=#0000ff] ChangeQuantUnit = quant_a[/COLOR]
[COLOR=#0000ff]Else[/COLOR]
[COLOR=red]'Make the transformation where both input + output units is one of (KG, K, MTR, ST)[/COLOR]
[COLOR=red] Mat_SQL = "SELECT SAP_Materials.Material, SAP_Materials.K, SAP_Materials.Base_K, SAP_Materials.KG, SAP_Materials.Base_KG, SAP_Materials.MTR, SAP_Materials.Base_MTR, SAP_Materials.ST, SAP_Materials.Base_ST FROM SAP_Materials WHERE (((SAP_Materials.Material)=""" + xMaterial + """));"[/COLOR]
[COLOR=red] Set M = CurrentDb.OpenRecordset(Mat_SQL, dbOpenDynaset)[/COLOR]
[COLOR=red] If M.RecordCount > 0 Then[/COLOR]
[COLOR=red] If M(unit_a) * M("BASE_" + unit_b) > 0 Then[/COLOR]
[COLOR=red] ChangeQuantUnit = quant_a * M(unit_b) * M("BASE_" + unit_a) / (M(unit_a) * M("BASE_" + unit_b))[/COLOR]
[COLOR=red] End If[/COLOR]
[COLOR=blue] Select Case Trim(UCase(xunit_b))[/COLOR]
[COLOR=blue] Case "LB" ' if output quant is LB, make the final transform from KG - > LB[/COLOR]
[COLOR=blue] ChangeQuantUnit = ChangeQuantUnit * LB2KG[/COLOR]
[COLOR=blue] Case "FT" ' if output quant is FT, make the final transform from MTR - > FT[/COLOR]
[COLOR=blue] ChangeQuantUnit = ChangeQuantUnit * FT2MTR[/COLOR]
[COLOR=blue] End Select[/COLOR]
[COLOR=blue] End If[/COLOR]
[COLOR=blue] M.Close[/COLOR]
[COLOR=#0000ff]End If[/COLOR]
[COLOR=#0000ff]End Function[/COLOR]