我做了一个VB程序,要求连Access数据库,但有时库开的着时候 程序会出现些异常,所以我判断了一下windows所有打开的窗体的名,判断是否有这个库打开,如果有就报个错就OK,昨天程序一直正常,但是今天突然使不了了
注:Access打开库的时候里面的窗体不最大化,顶级窗体只显示的是Mircosoft Access 代码:
Function DataExist() As Boolean
    Dim lngDeskTopHandle As Long
    Dim lngHand As Long
    Dim strName As String * 255
    Dim lngWindowCount As Long
    
    lngDeskTopHandle = GetDesktopWindow()
    lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
    Do While lngHand <> 0
        GetWindowText lngHand, strName, Len(strName)
        lngHand = GetWindow(lngHand, GW_HWNDNEXT)
        If Left$(strName, 1) <> vbNullChar Then
             If InStr(1, strName, "DmsData") > 0 Then   'DmsData是我的连的库的名
               DataExist = True    ''说明打开了数据库
             End If
        End If
    Loop
End Function

解决方案 »

  1.   

    Public Function DataExist() As Boolean
        Dim lngDeskTopHandle As Long
        Dim lngHand As Long
        Dim strName As String * 255
        Dim lngWindowCount As Long
        Dim lngNameLen As Long  '改动一
        lngDeskTopHandle = GetDesktopWindow()
        lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
        Do While lngHand <> 0
            lngNameLen = GetWindowTextLength(lngHand) '改动二
            GetWindowText lngHand, strName, lngNameLen '改动三
            lngHand = GetWindow(lngHand, GW_HWNDNEXT)
            If Left$(strName, 1) <> vbNullChar Then
                 If InStr(1, strName, "DmsData") > 0 Then   
                 '要不这样试试?
                 'If (strName Like "*DmsData*") Then
                   DataExist = True    ''说明打开了数据库
                 End If
            End If
        Loop
    End Function
      

  2.   

    试试 liyibin(为人民服务!) 的办法!