简体   繁体   English

VB6- Open Access DB或其中一个带有ADO对象的表单

[英]VB6- Open Access DB or one of it's forms with ADO object

My application written with visual basic 6 and it has an Access database. 我的应用程序用visual basic 6编写,它有一个Access数据库。 I want to add a VB form and open the database in this form to make db edits the DB in that. 我想添加一个VB表单并以这种形式打开数据库,以使db编辑DB。 I have this code for oppenning: 我有这个代码用于oppenning:

Dim db As DAO.Database

Set db = DBEngine.workspaces(0).opendatabase("c:\ss.mdb")

I have a form inside that database. 我在该数据库中有一个表单。 This form makes the data insertion process faster. 此表单使数据插入过程更快。 I want to open this access form with my application. 我想用我的应用程序打开这个访问表单。 How i should do this?? 我该怎么办?

Note: I have this code that uses Microsoft access 14 object library. 注意:我有这个使用Microsoft访问14对象库的代码。

Dim appaccess As Access.Application, dbstr As String

On Error Resume Next
Set appaccess = New Access.Application
Set appaccess = CreateObject("Access.Application")
dbstr = "c:\ss.mdb"
'Or dbstr="c:\my documents\yourfile.mdb
'put the correct path here.
appaccess.OpenCurrentDatabase dbstr
appaccess.DoCmd.OpenForm "aa", acLayout
appaccess.Visible = True

But when i run this code the form appears and after a while it gets disapear. 但是,当我运行此代码时,表单出现,一段时间后它就会消失。 besides using access object library creates some access version conflicts. 除了使用访问对象库创建一些访问版本冲突。 So, Although it is not necessary but i prefer to do that with ADO object. 所以,虽然没有必要,但我更喜欢用ADO对象。 Anyway, i' looking for e method to solve my problem. 无论如何,我正在寻找解决我问题的方法。

Thank you for your help 谢谢您的帮助

One thing is that you're using DAO in that first chunk of code rather than ADO. 有一点是你在第一块代码而不是ADO中使用DAO。 Maybe that doesn't matter though because there's no reference to db in your second chunk of code. 也许这无关紧要,因为在你的第二块代码中没有对db的引用。 So the first chunk of code looks to be unecessary. 所以第一块代码看起来是不必要的。

I don't know why you have 2 set statements for appaccess, you don't need the second one. 我不知道为什么你有appaccess的2个set语句,你不需要第二个。 Also I'd suggest that you comment out the 'on error resume next' statement so you can see which line is causing the error while debugging. 另外我建议你注释掉'on next error next'语句,这样你就可以看到哪一行在调试时导致了错误。

ADO provides methods to interact with various data sources. ADO提供与各种数据源交互的方法。 Although you can use it for Access db files, it doesn't provide methods to utilize Access forms. 虽然您可以将它用于Access数据库文件,但它不提供使用Access表单的方法。

With your current approach, you use CreateObject to set the object variable appaccess to a new Access application instance, then open your form within that instance. 使用当前方法,使用CreateObject将对象变量appaccess设置为新的Access应用程序实例,然后在该实例中打开表单。 However, when the variable goes out of scope, the Access instance closes and the form disappears. 但是,当变量超出范围时,Access实例将关闭,表单将消失。

You might revise your VB6 code to keep the variable in scope until you're finished with the Access instance. 在完成Access实例之前,您可能会修改VB6代码以将变量保留在范围内。 Unfortunately, I don't know how to fit that change into the rest of your code, and I've never used VB6. 不幸的是,我不知道如何在代码的其余部分中适应这种变化,而且我从未使用过VB6。

Alternatively, you could use the Shell Function to start the Access instance, then use GetObject() to set your object variable to that instance. 或者,您可以使用Shell函数启动Access实例,然后使用GetObject()将对象变量设置为该实例。

With the Shell() approach, you would need to supply the full path to MSACCESS.EXE . 使用Shell()方法,您需要提供MSACCESS.EXE的完整路径。 You can find the folder where it's located by reading the registry. 您可以通过阅读注册表找到它所在的文件夹。 Here's a VBScript sample which does that, and I'm hoping you can adapt it easily for VB6. 这是一个VBScript示例,它可以做到这一点,我希望你可以轻松地适应VB6。

Option Explicit
Dim MSAccFolder
Dim RegKey
Dim WSHShell

RegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\" _
    & "CurrentVersion\App Paths\MSACCESS.EXE\Path"
Set WSHShell = WScript.CreateObject("WScript.Shell")
MSAccFolder= WSHShell.RegRead(RegKey)
WScript.Echo "MS Access Folder: " & MSAccFolder
Set WSHShell = Nothing

If you're going to use VB, you should abandon the Access Automation stuff and create your form in VB. 如果您要使用VB,您应该放弃Access Automation的东西并在VB中创建表单。 Then use ADO to open and work with the Access file. 然后使用ADO打开并使用Access文件。 This is much more efficient than using VB to automatically open an Access form. 这比使用VB自动打开Access表单更有效。

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

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