如何在VBA Excel中顯示數據庫中的數據?

[英]How do I display Data from Database in VBA Excel?


I have a column in the database called "UOM" and I want to display it beside a value called "Qty" via opening an SQL query. But it showed an error saying "No data exists for the row/column". This is my codes.

我在數據庫中有一個名為“UOM”的列,我希望通過打開SQL查詢將其顯示在名為“Qty”的值旁邊。但它顯示錯誤說“行/列沒有數據”。這是我的代碼。

Dim Oledbconn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:/inetpub/wwwroot/ProjectIntegrated/Inven.mdb;Persist Security Info=True")
        Dim cmd As New OleDbCommand
        Dim reader As OleDbDataReader

        cmd.CommandText = "Select UOM FROM Master WHERE IPN = '" & Session("PO IPN")(SummaryCounter) & "'"
        cmd.CommandType = CommandType.Text
        cmd.Connection = Oledbconn

        Oledbconn.Open()
        reader = cmd.ExecuteReader()

        oSheet.Range("F" & ExcelCounter).Value = "" & Session("PO Qty") + reader.Item("UOM") + (POTableCounter)
        oSheet.Range("F" & ExcelCounter).HorizontalAlignment = -4108


        Oledbconn.Close()

1 个解决方案

#1


0  

Executing the reader alone will not do it. You also have to actually read. Try this:

單獨執行讀者不會這樣做。你還必須閱讀。嘗試這個:

Oledbconn.Open()
reader = cmd.ExecuteReader()

if (dreader.HasRows)
{
    dreader.Read();
    oSheet.Range("F" & ExcelCounter).Value = "" & Session("PO Qty") + reader.Item("UOM") + (POTableCounter)
    oSheet.Range("F" & ExcelCounter).HorizontalAlignment = -4108
}

Oledbconn.Close()

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/07/01/7254254df10e2ff3de62f5f270f8ab59.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com