2015-07-01 58 views
0

我在数据库中有一个名为“UOM”的列,我想通过打开SQL查询将它显示在名为“Qty”的值旁边。但它显示错误说“没有数据存在的行/列”。这是我的代码。如何在VBA Excel中显示来自数据库的数据?

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() 
+0

是不是'Vb.Net'? –

回答

0

单独执行读卡器不会执行此操作。您还必须实际上阅读。 试试这个:

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() 
+0

我试过了,但显示这个错误“运算符'+'没有为类型'String()'和字符串”EA“”定义。 EA是列中的文本值数据@LocEngineer –

+0

请发布您的Session和UOM的样本值。 POTableCounter是一个int吗?摆脱'“”&'。 – LocEngineer

+0

嗯......是不是'insertCommands'与上面的代码有什么关系?会话变量保持什么**值**?另外,根据http://stackoverflow.com/questions/4670247/concat-strings-by-and-in-vb-net,请始终使用'&'运算符连接VB.Net中的字符串。 – LocEngineer

相关问题