如何通过VBA将当前的Access DB置于前台? [英] How can I bring my current Access DB to foreground via VBA?
本文介绍了如何通过VBA将当前的Access DB置于前台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据以下几行内容来使我的访问数据库进入前台。我不确定为什么它不起作用,或者是否有更好的方法来做到这一点。
I'm trying to get my access db to come to the foreground based on the couple of lines, below. I'm not sure why it isn't working though, or if there are better methods to do this.
根据我所读的内容,这应该起作用:
From what I've read, this should work:
Access.Visible = False
Access.Visible = True
但实际上并没有将数据库置于最前面。
But doesn't actually bring the database to the front.
编辑更多信息:
Private Sub Form_Open(Cancel As Integer)
getStrUserName = Environ("username")
dbName = "myDB.accdb" ' database name
versionChckDB = "versionCheckDB.accdb" ' version check db name
strServer = "C:\My\Path\to\Server" ' server location string
strDesktop = "C:\My\Path\to\Local" ' desktop location string
strVersionCheck = "C:\My\Path\to\Version" ' version check location
Static acc As Access.Application
Dim db As DAO.Database
Dim strDbName As String
If FileLocked(strDesktop & "\" & versionChckDB) Then
Dim objAccess As Access.Application
Set objAccess = GetObject(strDesktop & "\" & versionChckDB)
objAccess.Application.Quit acQuitSaveAll
Set objAccess = Nothing
DoCmd.OpenForm "frmMainMenu"
DoCmd.RunCommand acCmdAppMaximize
Access.Visible = False
Access.Visible = True
GoTo exitSub
Else
strDbName = strDesktop & "\" & versionChckDB
Set acc = New Access.Application
acc.Visible = True
Set db = acc.DBEngine.OpenDatabase(strDbName, False, False)
acc.OpenCurrentDatabase strDbName
End If
'db.Close
exitSub:
Call SetForegroundWindow(Application.hWndAccessApp) ' bringing access DB to foreground
End Sub
推荐答案
通常,人们为此使用API函数。
Usually one uses an API function for that.
来自 http:// www。 access-programmers.co.uk/forums/showthread.php?t=132129 :
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
然后
Call SetForegroundWindow(Application.hWndAccessApp)
编辑
如果您想获得在前面新打开的Access应用程序窗口中,您需要其 hWnd
:
Call SetForegroundWindow(acc.hWndAccessApp)
编辑2
这对我有用。记事本暂时位于前台,然后是访问窗口。
Edit 2
This works for me. Notepad is briefly in the foreground, then the Access window.
模块:
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Sub TestAccessToForeground()
Sleep 300
Shell "notepad.exe", vbNormalFocus
Sleep 300
Call SetForegroundWindow(Application.hWndAccessApp)
End Sub
表格:
Private Sub Form_Open(Cancel As Integer)
Call TestAccessToForeground
End Sub
这篇关于如何通过VBA将当前的Access DB置于前台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文