Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conversions URGENT (which is the problem with this code?)

Status
Not open for further replies.

1forino

Programmer
Apr 26, 2001
14
PT
Hi,

I'm making a program in VB with a SQL SERVER database.

In my database I have two fields that they are numerical, Montante_PTE and Montante_EUR (this field is numerical with scale 2).

In my code I have 2 functions that they convert the values to Euros or PTE (portuguese money) and places the values in the respective text boxes. This is the code of the functions:
-----------------------------------------------------------
Function Converte_EUR(valor As String)

If IsNumeric(valor) And txtPTE <> Format(txtPTE, &quot;###0.00&quot;) Then
txtEUR = Format((txtPTE / 200.482), &quot;###0.00&quot;)
txtEUR.Locked = True
End If

End Function

Function Converte_PTE(valor As String)

If IsNumeric(valor) Then
txtPTE = Format((txtEUR * 200.482), &quot;###0&quot;)
txtPTE.Locked = True
End If
End Function

-----------------------------------------------------------

When I make the Update in the database(adoDespesasRs.UpdateBatch adAffectAll), the program generates an error: error number -2147217887 (80040e21) Multiple-step operation generated errors. Check each states value).
If the user write the values in the text boxes, the program wok fine.
Which is the problem with this code?

Thanks for any tip.
 
Well 1forino,

one or more fields are casuing trouble. Try writing them one by one to the database:

rs.edit
rs.field1 = value1
rs.field2 = value2
....
rs.update

Maybe the database does not accept formatted fields?

Success,

Herman
 
The error message:
error number -2147217887 (80040e21) Multiple-step operation generated errors. Check each states value).

USUALLY means that either you're trying to assign a variable with an incompatible type to a field (i.e. applying a string to a field of type adInteger, etc), OR, You're trying to add a string that is TOO long to a field...


MAKE SURE that if the field you're applying to is of type adVarchar that you convert any numeric values to Varchar first using Str().. If you're putting a string value in a numeric field, use cInt, cLng, or cDbl(?) ...

Hope this helps...

--NipsMG



 
In first place, thanks for the tips.

Removing the FORMAT of the strings resolved part of the problem.
At this moment my code is the following one:

-----------------------------------------------------------
Function Converte_EUR(valor As String)

Dim intLen As Integer
Dim strValor As String

If IsNumeric(valor) Then
strValor = (txtPTE / 200.482)
intLen = InStr(strValor, &quot;,&quot;)
txtEUR = Left(strValor, intLen + 2)
End If

End Function

Function Converte_PTE(valor As String)

If IsNumeric(valor) Then
txtPTE = CLng(txtEUR * 200.482)
End If

End Function

-----------------------------------------------------------

If text box txtEUR will be that the user fills, the called function will be Function Converte_PTE, the program work fine with thus.
But the problem continues to exist in relation to Function Converte_EUR, error always appears when I try to make update (error number -2147217887 (80040e21)).
The value of txtEUR it has to have two decimal houses (ex. 25,23).
I used the function INSTR that gave the position to me of the comma in string and join more two houses.
I think that the problem is putting one string in a numerical field.
But with the functions CInt,CLng, the fractions are rounded, and I do not want that this happens.
Which is the best solution resolve this problem?

Thanks for any tip.
 
If IsNumeric(valor) Then
' strValor = (txtPTE / 200.482)
' intLen = InStr(strValor, &quot;,&quot;)
txtEUR = (txtPTE / 200.482) 'Left(strValor, intLen + 2)
End If


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
I can see why you would still have a problem w/ the Converte_EUR Function... You are STILL returning a string.

Code:
Function Converte_EUR(valor As String)

Dim intLen As Integer
Dim strValor As String
    
If IsNumeric(valor) Then
    strValor = (txtPTE / 200.482)
    intLen = InStr(strValor, &quot;,&quot;)
txtEUR = Left(strValor, intLen + 2)
Code:
End If
    
End Function


Instead.. try cDbl(left(strValor, intLen + 1))

The easiest way to avoid this is to use correct function declarations... Instead of:
Code:
Function Converte_EUR(valor As String)
End Function
Try:
Code:
Public Function Converte_EUR(valor as String) as DOUBLE
End Function
This way you ALWAYS KNOW you are returning a NUMERIC, DECIMAL VALUE instead of a string.

If you try to assign a string to a numeric field you WILL get that error.. every time.

--NipsMG
 
Thanks for the tips, but the problem was the comma:)

Changing the comma for the point solve the problem...

Function Converte_EUR(valor As String)

Dim intLen As Integer
Dim strValor As String

If IsNumeric(valor) Then
strValor = (txtPTE / 200.482)
intLen = InStr(strValor, &quot;,&quot;) - 1
strValor = Left(strValor, intLen) & &quot;.&quot; & Mid(strValor, intLen + 2, 2)
txtEUR = strValor
End If

End Function

The cDbl(left(strValor, intLen + 1)) also it does not result.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top