; Begin-----------------------------------------------------------------

  ; Includes------------------------------------------------------------
    #Include "..\Include\GUIConstantsEx.au3"

  ; Constants-----------------------------------------------------------
    Const $ODBC = "odbccp32.cpl"
  
  ; Variables-----------------------------------------------------------
    Dim $ODBCPID = 0
    Dim $DataSourceName = 0
    Dim $DataSourceDescription = 0
    Dim $DataSourceFileName = 0
    Dim $DataSourceSelectFileName = 0
    Dim $OkButton = 0
    Dim $CancelButton = 0
    Dim $ODBCButton = 0
  
  ; Procedure Proceed---------------------------------------------------
    Func Proceed()
      ; Variables-------------------------------------------------------
        Dim $TabNum = 0
        Dim $i
        ; WindowTitles--------------------------------------------------
          Dim $WinTitle = "ODBC-Datenquellen-Administrator"
          Dim $Sub1WinTitle = "Neue Datenquelle erstellen"
          Dim $Sub2WinTitle = "ODBC Microsoft Access Setup"
          Dim $Sub3WinTitle = "Neue Datenbank"
      ; Start ODBC-Administrator----------------------------------------
        $ODBCPID = Run("control.exe " & $ODBC, @SystemDir, @SW_MAXIMIZE)
        If $ODBCPID <> 0 Then
          WinWaitActive($WinTitle)
          ; Check if the first tab is active----------------------------
            $TabNum = ControlCommand($WinTitle, "", _
              "[Class:SysTabControl32]", "CurrentTab")
            ; If not, set the first tab active--------------------------
              If $TabNum > 1 Then
                For $i = $TabNum To 1 Step -1
                  ControlCommand($WinTitle, "", _
                    "[Class:SysTabControl32]", "TabLeft")
                Next
              EndIf 
          ; Press button Hinzufuegen------------------------------------
            ControlClick($WinTitle, "", "[CLASS:Button; " & _
              "CLASSNN:Button1]")
            WinWaitActive($Sub1WinTitle)
          ; Select Microsoft Access Driver------------------------------    
            ControlListView($Sub1WinTitle, "", _
              "[CLASS:SysListView32; CLASSNN:SysListView321]", _
              "FindItem", "Microsoft Access Driver (*.mdb)")
          ; Press button Fertig stellen---------------------------------
            ControlClick($Sub1WinTitle, "", "[CLASS:Button; " & _
              "CLASSNN:Button4]")
            WinWaitActive($Sub2WinTitle)
          ; Fill the fields Datenquellenname and Beschreibung-----------  
            ControlSetText($Sub2WinTitle, "", "[CLASS:Edit; " & _
              "CLASSNN:Edit1]", GuiCtrlRead($DataSourceName))
            ControlSetText($Sub2WinTitle, "", "[CLASS:Edit; " & _
              "CLASSNN:Edit2]", GuiCtrlRead($DataSourceDescription))
          ; Press button Erstellen--------------------------------------
            ControlClick($Sub2WinTitle, "", "[CLASS:Button; " & _
              "CLASSNN:Button3]")
            WinWaitActive($Sub3WinTitle)
          ; Fill the field Datenbankname--------------------------------
            ControlSetText($Sub3WinTitle, "", "[CLASS:Edit; " & _
              "CLASSNN:Edit1]", GuiCtrlRead($DataSourceFileName))
          ; Press button Ok---------------------------------------------
            ControlClick($Sub3WinTitle, "", "[CLASS:Button; " & _
              "CLASSNN:Button1]")
            WinWaitNotActive($Sub3WinTitle)
          ; Send Enter for the dialog box-------------------------------
            Sleep(256)
            Send("{ENTER}")
          ; Press button Ok---------------------------------------------            
            ControlClick($Sub2WinTitle, "", "[CLASS:Button; " & _
              "CLASSNN:Button13]")
            WinWaitNotActive($Sub2WinTitle)
          ; Press button Ok---------------------------------------------
            ControlClick($WinTitle, "", "[CLASS:Button; " & _
              "CLASSNN:Button5]")
        EndIf
    EndFunc

  ; Procedure SelectFileName--------------------------------------------
    Func SelectFileName()
      GUICtrlSetData($DataSourceFileName, _
        FileOpenDialog("Wähle Access-Datenbank", "", _
          "Access-Datenbank (*.mdb)", 0, "", $ODBCPID))
    EndFunc

  ; Procedure Init------------------------------------------------------
    Func Init()
      ; GUI-------------------------------------------------------------
        GUICreate("Create Access Database", 360, 134)
        ; DataSourceName------------------------------------------------
          GUICtrlCreateLabel("Datenquellenname: ", 10, 10)
          $DataSourceName = GUICtrlCreateInput("", 150, 5, 200, 24)
        ; DataSourceDescription-----------------------------------------  
          GUICtrlCreateLabel("Beschreibung:", 10, 40)
          $DataSourceDescription = GUICtrlCreateInput("", 150, 35, _
            200, 24)
        ; DataSourceFileName--------------------------------------------    
          GUICtrlCreateLabel("Dateiname:", 10, 70)
          $DataSourceFileName = GUICtrlCreateInput("", 150, 65, _
            176, 24)
          $DataSourceSelectFileName = GUICtrlCreateButton("...", 326, _
            65, 24, 24)
        ; Buttons-------------------------------------------------------    
          $OkButton = GUICtrlCreateButton("Ok", 10, 100, 107, 24)
          $CancelButton = GUICtrlCreateButton("Abbrechen", 127, 100, _
            107, 24)
          $ODBCButton = GUICtrlCreateButton("ODBC-Administrator", _
            244, 100, 107, 24)  
        GUISetState(@SW_SHOW)
    EndFunc
  
  ; Procedure Main------------------------------------------------------
    Func Main()
      ; Variables-------------------------------------------------------
        Dim $Msg
        Dim $Quit = False
      ; MainLoop--------------------------------------------------------  
        While $Quit = False
          Sleep(32)
          $Msg = GUIGetMsg()
          Select
            Case $Msg = $OkButton
              If GuiCtrlRead($DataSourceName) <> "" And _
                GuiCtrlRead($DataSourceDescription) <> "" And _
                GuiCtrlRead($DataSourceFileName) <> "" Then  
                Proceed()
              Else
                MsgBox(0, "Wichtiger Hinweis", _
                  "Alle Felder sind auszufüllen.")
              EndIf  
            Case $Msg = $CancelButton
              $Quit = True
            Case $Msg = $ODBCButton
              Run("control.exe " & $ODBC, @SystemDir, @SW_MAXIMIZE)                
            Case $Msg = $DataSourceSelectFileName
              SelectFileName()
            Case $Msg = $GUI_EVENT_CLOSE
              $Quit = True
          EndSelect
        Wend
    EndFunc

  ; Procedure Done------------------------------------------------------
    Func Done()
      GUIDelete()
    EndFunc

  ; Main----------------------------------------------------------------
    Init()
    Main()
    Done()

; End-------------------------------------------------------------------