1

I'm fetching data from a db2 in VB6 using ODBC. The connection and setup is fine, but there's a problem in the returned data.

The fields in my record set are all rounded down to integer, so if the db column has a 0.99 it gets returned as 0, which isn't great.

The same query in Microsoft Query results in the proper values, with decimals.

This is the pertinent part of the code:

Set objCommand As New ADODB.Command
Set recordSet As New ADODB.Recordset

sqlQuery= "Select f2scop Price from database.mpline  where ibsuno='" & Order & "' order by ibpuno, ibpnli"
                   
With objCommand
   .ActiveConnection = ConnectDB2
   .CommandType = adCmdText
   .CommandText = sqlQuery
End With
                
With recordSet
   .CursorType = adOpenStatic
   .CursorLocation = adUseClient
   .LockType = adLockOptimistic
   .Open objCommand
End With

myVariable.value = recordSet("Price")

Debugging the recordSet("Price").OriginalValue returns the same rounded down value.
recordSet("Price").precision, .numericscale and .type return the expected results for fields that accept decimals.

Is there anything I could do about the recordset, ODBC, SQL query or anything else?

Any and all help is appreciated!

PS: I answer with a way to work around my problem.

8
  • vb6 is long obsolete, it is time to embrace VB NET Commented Oct 19, 2023 at 19:47
  • unfortunately it's not my call to make, there's a lot more code already in use Commented Oct 20, 2023 at 10:35
  • 4
    There's loads of VB6 code still out there being maintained. Commented Oct 20, 2023 at 18:17
  • Does DB2 SQL have any sort of CAST() operator that you could use to force it to return a decimal value? Even just as a test? Commented Oct 20, 2023 at 18:24
  • 1
    @StayOnTarget, yes, I tried CAST(f2scop AS DECIMAL(19, 8)) Price, same result Commented Oct 23, 2023 at 8:38

2 Answers 2

0

I have come up with a workaround for this issue.

In the sql query I multiply the data by 100 and then, when assigning the value to the variable, divide it by 100.

Like so:

sqlQuery= "Select (f2scop * 100) Price from database.mpline where ibsuno='" & Order & "' order by ibpuno, ibpnli"

myVariable.value = recordSet("Price") / 100

It's not a solution, but it bypasses the problem, and that's good enough for me.

There might be someone for whom this answer is not applicable, so more help is always appreciated! Thx

Sign up to request clarification or add additional context in comments.

Comments

-1

I had such problems with databases due to locale! If you have eg greek windows, then the point in decimals is a comma, and the point is the thousands separator. Then, if the decimals in your database table is point- and not comma-separated, odbc will truncate the number.

1 Comment

This only happens when you store numbers as strings, which you should not do to begin with. The OP stores numbers as numbers as seen from the Precision, NumericScale and Type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.