我试图让我的访问数据库根据下面几行来到前台。我不确定为什么它不起作用,或者有更好的方法可以做到这一点。
根据我的阅读,这应该有效:
Access.Visible = False
Access.Visible = True
但实际上并没有把数据库带到前面。
编辑以获取更多信息:
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
答案 0 :(得分:4)
通常会使用API函数。
来自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
这适合我。记事本位于前台,然后是Access窗口。
模块:
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