简体   繁体   English

通过 VBA 将 Excel 连接到访问表单

[英]Connect Excel to Access Form via VBA

I wanted to connect my Excel to my Access DB.我想将我的 Excel 连接到我的 Access 数据库。 I want to extract Data from a Form into Excel.我想将表单中的数据提取到 Excel 中。

 Sub GetDataFromDB()
    
    Dim conn As Object
    Dim DBPATH, PRVD, connString, query As String
    
    DBPATH = "...."
    PRVD = "Microsoft.ace.OLEDB.12.0;"
    
    connString = "Provider=" & PRVD & "Data Source=" & DBPATH
    Set conn = CreateObject("ADODB.Connection")
    conn.Open connString
    
    DoCmd.OpenForm Forms![frm_name], acFormDS, "", "", , acNormal
    (Do Something)
End Sub

Now I get an error 424 that an Object is needed but I don't know to to get access to that specific Form in my DB.现在我收到一个错误 424,表明需要 Object,但我不知道如何访问我的数据库中的特定表单。

Can someone help me please?有人能帮助我吗?

Best regards此致

Normally you get your data form a table or a query.通常,您从表或查询中获取数据。 May be you should explain better what you want to accomplish.也许你应该更好地解释你想要完成的事情。 Getting data directly from a form doesn't sound logical.直接从表单中获取数据听起来不合逻辑。 If you want to open an access form try;如果您想打开访问表单,请尝试;

Ps, Dim declarations are not correct, Dim x as string, y as string otherwise they will be variant. Ps,Dim 声明不正确,Dim x 为字符串,y 为字符串,否则它们将是变体。

Public Sub Displayform(conPath As String)公共子显示表单(conPath 作为字符串)

Dim objAccess As Access.Application 'Check that this is the path on YOUR PC for 'Northwind.mdb, which ships with Access. Dim objAccess As Access.Application '检查这是您的 PC 上的路径,用于 'Northwind.mdb,它随 Access 一起提供。 'Const conPATH = "C:\Users\Myname\documents\PrintMe.accdb" 'Create an instance of the Access application object. 'Const conPATH = "C:\Users\Myname\documents\PrintMe.accdb" '创建 Access 应用程序 object 的实例。 Set objAccess = CreateObject("Access.Application") 'Access and display the application window. Set objAccess = CreateObject("Access.Application") '访问并显示应用程序 window。 objAccess.OpenCurrentDatabase conPath objAccess.Visible = True objAccess.OpenCurrentDatabase conPath objAccess.Visible = True

    'Open the Employees form.
     'objAccess.DoCmd.OpenForm "Cust List"
     ' Maximize other Access window and quit current application.

objAccess.DoCmd.RunCommand acCmdAppMaximize
Application.DoCmd.RunCommand acCmdAppMinimize

End Sub结束子

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

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