ADO返回错误的数量级 [英] ADO returns wrong order of magnitude

查看:118
本文介绍了ADO返回错误的数量级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我尝试了两种方法


  1. QueryTables.Add(...)

  2. 使用ADO

我赞成ADO,因为它似乎是更好或更干净的

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

在我尝试从数据库中获取ADO的数值之前,一切工作正常。而不是返回值 1842,47078 它返回 0,01842 。(只是为了说明我的意思)

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)

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

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.

有人可以帮我吗?

驱动程序是Firebird / InterBase(r)驱动程序与ODBC连接。
这是我的工作方式的一个小示例:

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

我希望有人能帮助我。

更新:


  • 我以某种方式使它工作了,但我不知道那是什么错了。

  • 我得到了正确的答案t结果,如果我使用查询 SELECT Price作为数字(15,2)...

  • 奇怪的是,我尝试过在我的计算机上使用C#可以完成整个工作,并且没有任何问题。因此,错误似乎是由Excel和/或VBA中的任何内容引起的。

推荐答案

尝试将NumberFormat应用于单元格

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

这篇关于ADO返回错误的数量级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆