ADO 返回错误的数量级

发布于 2024-12-11 00:47:19 字数 1650 浏览 0 评论 0原文

我正在为 Excel 2003 编写一个 VBA 宏。我正在将各种数据从 sql 数据库导入到 Excel 工作表中。

我尝试了两种方法来做到这一点:

  1. QueryTables.Add(...)
  2. 使用 ADO

我赞成 ADO,因为它似乎是“更好”或“更干净”的解决方案。

一切运行良好直到我尝试使用 ADO 从数据库中获取数值,而不是返回值 1842,47078 时,它返回了 0,01842 (只是为了说明我的意思。 )

奇怪的是:当我尝试使用 QueryTables.Add(...) 时,我得到了正确的值,

我完全不知道为什么会发生这种情况,在互联网上搜索了解决方案,但没有找到。任何事情都会发生。如果我从数据库中获取一个字符串,一切都很好。

驱动

程序是与 ODBC 连接的 Firebird/InterBase(r) 驱动程序。 这是我如何做事的一个小例子:

    'ADO solution = wrong value
    With adoConnection
      .Provider = "MSDASQL"
      sConnection = "ODBC;DSN=ABC;Driver=Firebird/InterBase(r) driver;Dbname=blaName.gdb;"
      ConnectionString = sConnection
      .Open
    End With
    SQL_Import = "SELECT A.PRICE AS ""Price"" FROM TABLE A WHERE A.KEY ='x1234' "
    adoRecordset.ActiveConnection = adoConnection
    adoRecordset.Open SQL_Import
    varSol = adoRecordset.Fields("Price")
    Sheets(3).Cells(1, 1).Value = varSol
    adoRecordset.Close
    adoRecordset.ActiveConnection = Nothing
    adoConnection.Close
    'QueryTables solution = right value
    Set QueryTbl = Sheets(3).QueryTables.Add(Connection:=sConnection, Destination:=Sheets(3).Cells(1, 2))
    With QueryTbl
        .CommandText = SQL_Import
        .AdjustColumnWidth = True
        .Refresh BackgroundQuery:=False
        .Delete
    End With

我希望任何人都可以帮助我。

更新:

  • 我以某种方式让它工作,但我不知道出了什么问题。
  • 如果我使用查询SELECT Price as numeric(15, 2)...,我会得到正确的结果。
  • 奇怪的是,我在我的计算机上用 C# 尝试了整个过程,并且没有任何问题。因此该错误似乎是由 Excel 和/或 VBA 中的任何内容引起的。

i'm writing a VBA macro for Excel 2003. I'm importing various data from a sql database to the Excel sheets.

I tried two ways to do that:

  1. QueryTables.Add(...)
  2. with ADO

I was in favour of ADO, because it seemed to be the 'better' or 'cleaner" solution.

Everything worked well until i tried to get a numeric value with ADO out of the database. Instead of returning the value 1842,47078 it returned 0,01842. (just to show what i mean)

Strange thing is: when i try it with QueryTables.Add(...) i get the right value.

I have totally no clue why this happens, searched the internet for a solution but didn't found anything. It just happens with numeric values. If i get a string from the database everything is fine.

Can anyone help me with this one?

Driver is Firebird/InterBase(r) driver connecting with ODBC.
Here is a little example how i do things:

    'ADO solution = wrong value
    With adoConnection
      .Provider = "MSDASQL"
      sConnection = "ODBC;DSN=ABC;Driver=Firebird/InterBase(r) driver;Dbname=blaName.gdb;"
      ConnectionString = sConnection
      .Open
    End With
    SQL_Import = "SELECT A.PRICE AS ""Price"" FROM TABLE A WHERE A.KEY ='x1234' "
    adoRecordset.ActiveConnection = adoConnection
    adoRecordset.Open SQL_Import
    varSol = adoRecordset.Fields("Price")
    Sheets(3).Cells(1, 1).Value = varSol
    adoRecordset.Close
    adoRecordset.ActiveConnection = Nothing
    adoConnection.Close
    'QueryTables solution = right value
    Set QueryTbl = Sheets(3).QueryTables.Add(Connection:=sConnection, Destination:=Sheets(3).Cells(1, 2))
    With QueryTbl
        .CommandText = SQL_Import
        .AdjustColumnWidth = True
        .Refresh BackgroundQuery:=False
        .Delete
    End With

I hope anyone can help me.

Update:

  • I got it working somehow, but i don't know what was wrong.
  • I get the right results if i use the query SELECT Price as numeric(15, 2)....
  • Strange thing is that i tried the whole thing with C# on my computer and it worked without any problems. So the error seems to be caused by anything in Excel and/or VBA.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一紙繁鸢 2024-12-18 00:47:19

尝试对单元格应用 NumberFormat

varSol = adoRecordset.Fields("Price") 
Sheets(3).Cells(1, 1).NumberFormat = "###0,#######0" 
Sheets(3).Cells(1, 1).Value = varSol 

Try applying a NumberFormat to the cell

varSol = adoRecordset.Fields("Price") 
Sheets(3).Cells(1, 1).NumberFormat = "###0,#######0" 
Sheets(3).Cells(1, 1).Value = varSol 
蓝海似她心 2024-12-18 00:47:19

我对 firebird 一无所知,但听起来有问题的列是货币或货币类型(ADO 称之为 adCurrency),并且您没有告诉 ADO 这一点,所以它没有正确转换它。

看看 adoRecordset.Fields("Price").Type,我敢打赌您会看到它是 adCurrency 或类似的内容。

I don't know anything about firebird but it sounds like the column in question is in Money or Currency type (what ADO calls adCurrency) and you haven't told ADO this, so it's not converting it appropriately.

Look at adoRecordset.Fields("Price").Type and I'll bet you'll see that it's adCurrency or the like.

走野 2024-12-18 00:47:19

我真的不知道问题是什么,但我有一个解决方案。
如果我将 SQL 字符串更改为

SQL_Import = "SELECT A.PRICE*1  FROM TABLE A WHERE A.KEY ='x1234' "

一切按预期工作并且我得到正确的结果。

所以我的解决方案是乘以一或加零。

I don't know really what the problem is/was, but i have a solution.
If i alter my SQL string to

SQL_Import = "SELECT A.PRICE*1  FROM TABLE A WHERE A.KEY ='x1234' "

everything works as expected and i get the right result.

So my solution is multiplying with one or adding zero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文