Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用Excel Vba Ado导出带有计算字段的Msaccess查询表时,是否有解决方法?_Excel_Vba_Ms Access_Ado - Fatal编程技术网

在使用Excel Vba Ado导出带有计算字段的Msaccess查询表时,是否有解决方法?

在使用Excel Vba Ado导出带有计算字段的Msaccess查询表时,是否有解决方法?,excel,vba,ms-access,ado,Excel,Vba,Ms Access,Ado,嗨,我有一个计算过的查询表名DataQuery,如 日期/欧元/金额 其中Rjournal是一个计算字段 Rjournal:DLookUpREFjournal,DV,ChckID>0,收款人='&[paye]&'和Dvnumber=&[Dvnumber]& 而且效果很好 但由于MS Access是我的数据库,Excel是我的前端,我的大多数用户都是Excel用户。我创建了一个导出按钮,以便在excel中使用ADO将此查询导出到excel。由于某些原因,RJournal字段不会捕获其数据,它只是保

嗨,我有一个计算过的查询表名DataQuery,如

日期/欧元/金额

其中Rjournal是一个计算字段

Rjournal:DLookUpREFjournal,DV,ChckID>0,收款人='&[paye]&'和Dvnumber=&[Dvnumber]&

而且效果很好

但由于MS Access是我的数据库,Excel是我的前端,我的大多数用户都是Excel用户。我创建了一个导出按钮,以便在excel中使用ADO将此查询导出到excel。由于某些原因,RJournal字段不会捕获其数据,它只是保留为空

但如果我使用access菜单“外部数据”,则会将所有数据导出到Excel

我想知道ADO是否支持导出计算表查询

Private Sub Export_Click()

Dim cnn As ADODB.Connection 'dim the ADO collection class
Dim rs As ADODB.Recordset 'dim the ADO recordset class
Dim dbPath As String
Dim SQL As String

'add error handling
On Error GoTo errHandler:
'Disable screen flickering.
Application.ScreenUpdating = False
'clear the values from the worksheet
Sheets("Data").Range("A2:C500000").ClearContents

'get the path to the database
dbPath = Sheets("Update Version").Range("b1").Value

Set cnn = New ADODB.Connection ' Initialise the collection class variable

'Connection class is equipped with a —method— named Open
'—-4 aguments—- ConnectionString, UserID, Password, Options
'ConnectionString formula—-Key1=Value1;Key2=Value2;Key_n=Value_n;
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath

SQL = "SELECT * FROM DATAQUERY"

'Create the ADODB recordset object.

Set rs = New ADODB.Recordset 'assign memory to the recordset

'ConnectionString Open '—-5 aguments—-
'Source, ActiveConnection, CursorType, LockType, Options
rs.Open SQL, cnn

'Check if the recordset is empty.
If rs.EOF And rs.BOF Then
'Close the recordet and the connection.
rs.Close
cnn.Close
'clear memory
Set rs = Nothing
Set cnn = Nothing
'Enable the screen.
Application.ScreenUpdating = True
'In case of an empty recordset display an error.
MsgBox "There are no records in the recordset!", vbCritical, "No Records"

Exit Sub
End If


'Write the reocrdset values in the sheet.
Sheets("DATA").Range("A2").CopyFromRecordset rs

'Close the recordset and the connection.
rs.Close
cnn.Close
'clear memory
Set rs = Nothing
Set cnn = Nothing

'Enable the screen.
Application.ScreenUpdating = True



'Inform the user that the macro was executed successfully.
MsgBox "Congratulation the data has been successfully Imported", vbInformation, "Import successful"
'error handler
On Error GoTo 0
Exit Sub
errHandler:
'clear memory
Set rs = Nothing
Set cnn = Nothing
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Import_Data"

END SUB
我期待这样的结局

日期/欧元/金额 01/CRJ/1000 02/CDJ/1000 03/CRJ/1000 04/CRJ/1500

但结果是这样的

日期/欧元/金额 01 / / 1000 02 / / 1000 03 / /1000
04//1500

在MSAccess查询中使用内部选择查询将完成这项工作

我做了3个表格查询

表1构成 日期 DV号码 受款人 数量

表2组成 参考期刊 DV号码 受款人

所以在表3中 日期 RJournal:从表2中选择REFjournal,其中Table1.DvNumber=Table2.DvNumber和Table1.paye=Table2.paye 数量

或者在SQL下

   SELECT Table1.Date, (Select REFjournal From Table2 where Table1.DvNumber = 
   Table2.DVnumber and Table1.Payee=Table2.Payee) as Rjournal, Table1.AMOUNT
   FROM Table1;

唯一的缺点是,如果使用Excel ADO VBA导出到Excel,速度会非常慢。

使用ADODB和DAO进行测试并不表明导出计算字段有任何问题。此外,我很难理解预期输出和实际输出。您给出了RJournal示例,但在预期输出示例中,此字段没有问题。请快照或表格格式您当前的结果,期望的结果和我猜测的是计算字段CRJ。