简体   繁体   English

使用VBA将Access中的特定数据复制到Excel的特定字段中

[英]Copying specific data from Access into specific fields on Excel using VBA

The people I work for have asked me to change up the order system they currently have in place as it is quite old and they want to update. 我工作的人员要求我更改他们现有的订单系统,因为该系统已经很老了,他们想更新。 So I have moved the table to Excel like they have asked so that it does not sit on the order form but they also want it so that when they click on the "Trade Account" button on the Excel form it opens a Query in Access (Which is all done) that asks them to enter in the account information (company name). 因此,我已按照他们的要求将表格移至Excel,这样它就不会位于订单表上,但他们也想要它,因此当他们单击Excel表格上的“交易帐户”按钮时,它将在Access中打开一个查询(要求他们输入帐户信息(公司名称)。 When that is done it will bring up all the account information for them to see, now this is where I am stuck. 完成后,它将调出所有帐户信息供他们查看,现在这就是我遇到的问题。 They would like me to make it so that the information that comes up is copied into the trade account fields in the Excel document so that they do not have to spend time typing it out by hand and I currently have no idea how to do this as I do not have experience using VBA on here. 他们希望我这样做,以便将出现的信息复制到Excel文档中的贸易帐户字段中,这样他们就不必花费时间手动输入信息了,而我目前不知道该怎么做我在这里没有使用VBA的经验。 (Probably sounds dumb but i'm trying to learn) (可能听起来很蠢,但我正在尝试学习)

I can also provide images if that would explain it better. 如果可以更好地说明,我也可以提供图像。

There are probably many ways to do this. 可能有很多方法可以做到这一点。 Here is one. 这是一个。

I am using the Northwind Database! 我正在使用罗斯文数据库!

With the following vector in Sheet1: 在Sheet1中具有以下向量:

10248
10249
10250
10251
10252

Run the following script, in Excel, and the results will be copied into Sheet2. 在Excel中运行以下脚本,结果将被复制到Sheet2中。 Oh, don't forget to set a reference to Microsoft ADO 2.8!! 哦,别忘了设置对Microsoft ADO 2.8的引用!

'Set db = ws.OpenDatabase("C:\your_path\Northwind.mdb")

Sub DAOParamTest()
Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim s As String
Dim i As Integer, j As Integer
Dim rCell As Range
Dim rRng As Range
Dim sht As Worksheet
Dim LastRow As Long
Dim MyCell As Range


''Access database

strFile = "C:\your_path\Northwind.mdb"

strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile & ";"

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open strCon

'Rough idea
Set sht = ThisWorkbook.Worksheets("Sheet1")
Worksheets("Sheet1").Select
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
Set rRng = Sheet1.Range("A1:A" & LastRow)

For Each MyCell In rRng
intID = MyCell

    strSQL = "SELECT * " _
           & "FROM [Order Details] " _
           & "WHERE OrderID = " & intID

    rs.Open strSQL, cn
        Worksheets("Sheet2").Select
        LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row + 1
        Worksheets("Sheet2").Cells(LastRow, 1).CopyFromRecordset rs
    rs.Close

Next MyCell

''Tidy up
Set rs = Nothing
cn.Close
Set cn = Nothing

End Sub

Here is yet another solution for you to consider. 这是供您考虑的另一种解决方案。

http://www.erlandsendata.no/english/index.php?d=envbadacimportado http://www.erlandsendata.no/english/index.php?d=envbadacimportado

I answered your other question with a different solution. 我用其他解决方案回答了您的其他问题。 Here is another way to do what you want, using a Query. 这是使用查询执行所需操作的另一种方法。 This is nice because you can control the query like a filter, and import the results from that object. 很好,因为您可以像过滤器一样控制查询,并从该对象导入结果。

' Set a reference to DAO 6.0

    Sub GetQuery()
        Dim dbs As DAO.Database
        Dim rst As DAO.Recordset
        Dim i As Long
        Dim wsh As Worksheet
        Set dbs = DBEngine.OpenDatabase("C:\your_path\Northwind.mdb")
        Set rst = dbs.OpenRecordset("Invoices")
        Set wsh = Worksheets("Sheet3")
        For i = 0 To rst.Fields.Count - 1
            wsh.Cells(1, i + 1).Value = rst.Fields(i).Name
        Next
        wsh.Range("A1").Resize(ColumnSize:=rst.Fields.Count).Font.Bold = True
        wsh.Range("A2").CopyFromRecordset rst
        rst.Close
        Set rst = Nothing
        dbs.Close
        Set dbs = Nothing
    End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM