home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l407 / 5.ddi / OPENDB.FR_ / OPENDB.bin (.txt)
Encoding:
Visual Basic Form  |  1993-04-28  |  9.9 KB  |  317 lines

  1. VERSION 2.00
  2. Begin Form fOpenDB 
  3.    BackColor       =   &H00C0C0C0&
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Open DataBase"
  6.    ClientHeight    =   2160
  7.    ClientLeft      =   2460
  8.    ClientTop       =   3840
  9.    ClientWidth     =   4395
  10.    ControlBox      =   0   'False
  11.    ForeColor       =   &H00C0C0C0&
  12.    Height          =   2565
  13.    Left            =   2400
  14.    LinkTopic       =   "Form2"
  15.    MaxButton       =   0   'False
  16.    MinButton       =   0   'False
  17.    ScaleHeight     =   2119.728
  18.    ScaleMode       =   0  'User
  19.    ScaleWidth      =   4447.084
  20.    Top             =   3495
  21.    Width           =   4515
  22.    Begin ComboBox cDBName 
  23.       BackColor       =   &H00FFFFFF&
  24.       Height          =   300
  25.       Left            =   1680
  26.       Sorted          =   -1  'True
  27.       TabIndex        =   0
  28.       Tag             =   "OL"
  29.       Top             =   105
  30.       Width           =   2655
  31.    End
  32.    Begin TextBox cDataBase 
  33.       BackColor       =   &H00FFFFFF&
  34.       Height          =   285
  35.       Left            =   1680
  36.       TabIndex        =   1
  37.       Tag             =   "OL"
  38.       Top             =   465
  39.       Width           =   2655
  40.    End
  41.    Begin TextBox cUserName 
  42.       BackColor       =   &H00FFFFFF&
  43.       Height          =   285
  44.       Left            =   1680
  45.       TabIndex        =   2
  46.       Tag             =   "OL"
  47.       Top             =   825
  48.       Width           =   2655
  49.    End
  50.    Begin TextBox cPassword 
  51.       BackColor       =   &H00FFFFFF&
  52.       Height          =   285
  53.       Left            =   1680
  54.       PasswordChar    =   "*"
  55.       TabIndex        =   3
  56.       Tag             =   "OL"
  57.       Top             =   1185
  58.       Width           =   2655
  59.    End
  60.    Begin CommandButton OkayButton 
  61.       BackColor       =   &H00C0C0C0&
  62.       Caption         =   "&Open"
  63.       Default         =   -1  'True
  64.       Height          =   375
  65.       Left            =   300
  66.       TabIndex        =   4
  67.       Top             =   1680
  68.       Width           =   1575
  69.    End
  70.    Begin CommandButton CancelButton 
  71.       BackColor       =   &H00C0C0C0&
  72.       Cancel          =   -1  'True
  73.       Caption         =   "&Cancel"
  74.       Height          =   375
  75.       Left            =   2460
  76.       TabIndex        =   5
  77.       Top             =   1680
  78.       Width           =   1575
  79.    End
  80.    Begin Label DataBaseLabel 
  81.       BackColor       =   &H00C0C0C0&
  82.       Caption         =   "DataBase:"
  83.       Height          =   255
  84.       Left            =   120
  85.       TabIndex        =   9
  86.       Top             =   465
  87.       Width           =   1335
  88.    End
  89.    Begin Label DBNameLabel 
  90.       BackColor       =   &H00C0C0C0&
  91.       Caption         =   "Source/Server:"
  92.       Height          =   255
  93.       Left            =   120
  94.       TabIndex        =   6
  95.       Top             =   105
  96.       Width           =   1470
  97.    End
  98.    Begin Label UserNameLabel 
  99.       BackColor       =   &H00C0C0C0&
  100.       Caption         =   "User ID:"
  101.       Height          =   255
  102.       Left            =   120
  103.       TabIndex        =   7
  104.       Top             =   825
  105.       Width           =   1335
  106.    End
  107.    Begin Label PasswordLabel 
  108.       BackColor       =   &H00C0C0C0&
  109.       Caption         =   "Password:"
  110.       Height          =   255
  111.       Left            =   120
  112.       TabIndex        =   8
  113.       Top             =   1170
  114.       Width           =   1335
  115.    End
  116. Option Explicit
  117. Dim BeenLoaded As Integer
  118. 'ODBC.DLL APIs
  119. Declare Function SQLAllocEnv Lib "odbc.dll" (env As Long) As Integer
  120. Declare Function SQLDataSources Lib "ODBC.DLL" (ByVal henv As Long, ByVal fdir As Integer, ByVal szDSN As String, ByVal cbDSNMAx As Integer, pcbDSN As Integer, ByVal szDesc As String, ByVal cbDescMax As Integer, pcbDesc As Integer) As Integer
  121. Sub CancelButton_Click ()
  122.   gfDBOpenFlag = False
  123.   Unload Me
  124. End Sub
  125. Sub cDBName_Click ()
  126.   On Error Resume Next
  127.   Dim tmp As String
  128.   Dim x As Integer
  129.   cDataBase = ""
  130.   cUserName = ""
  131.   cPassword = ""
  132.   'get the database name if there is one
  133.   tmp = String$(255, 32)
  134.   x = OSGetPrivateProfileString(cDBName, "database", "", tmp, Len(tmp), "ODBC.INI")
  135.   cDataBase = Mid$(tmp, 1, x)
  136.   'get the last user name is there is one
  137.   tmp = String$(255, 32)
  138.   x = OSGetPrivateProfileString(cDBName, "lastuser", "", tmp, Len(tmp), "ODBC.INI")
  139.   cUserName = Mid$(tmp, 1, x)
  140.   cPassword = ""
  141.   If cUserName <> "" Then
  142.     cPassword.SetFocus
  143.   Else
  144.     cDataBase.SetFocus
  145.   End If
  146. End Sub
  147. Sub Form_Load ()
  148.   Left = (Screen.Width - Width) / 2
  149.   Top = (Screen.Height - Height) / 2
  150.   GetDataSources cDBName
  151.   MsgBar "Enter DataBase Parameters", False
  152.   BeenLoaded = True
  153. End Sub
  154. Sub Form_Paint ()
  155.   Outlines Me
  156. End Sub
  157. Sub Form_Unload (Cancel As Integer)
  158.   MsgBar "", False
  159. End Sub
  160. 'this routine fills a list box with all available
  161. 'ODBC data sources found in ODBC.INI
  162. Sub GetDataSources (listctrl As Control)
  163.   Dim DataSource As String, Description As String
  164.   Dim DataSourceLen As Integer, DescriptionLen As Integer
  165.   Dim retcode As Integer
  166.   Dim henv As Long
  167.   If SQLAllocEnv(henv) <> -1 Then
  168.     DataSource = String$(32, 32)
  169.     Description = String$(255, 32)
  170.     'get the first one
  171.     retcode = SQLDataSources(henv, 2, DataSource, Len(DataSource), DataSourceLen, Description, Len(Description), DescriptionLen)
  172.     While retcode = 0 Or retcode = 1
  173.       listctrl.AddItem Mid(DataSource, 1, DataSourceLen)
  174.       DataSource = String$(32, 32)
  175.       Description = String$(255, 32)
  176.       'get all the others
  177.       retcode = SQLDataSources(henv, 1, DataSource, Len(DataSource), DataSourceLen, Description, Len(Description), DescriptionLen)
  178.     Wend
  179.   End If
  180. End Sub
  181. Sub OkayButton_Click ()
  182.    Dim Connect As String, DataSource As String
  183.    Dim x As Integer
  184.    Dim st As String
  185.    Dim i As Integer
  186.    Dim s As String, t As String
  187.    Dim dbq As String
  188.    On Error GoTo OpenError
  189.    MsgBar "Opening DataBase", True
  190.    If VDMDI.PrefOpenOnStartup.Checked = True Then
  191.      Me.Refresh
  192.    End If
  193.    SetHourglass Me
  194.    'check for blank server name and clear other parms
  195.    If cDBName = "" Then
  196.      cDataBase = ""
  197.      cUserName = ""
  198.      cPassword = ""
  199.    End If
  200.    'build connect string
  201.    Connect = "ODBC;"
  202.    If cUserName <> "" Then
  203.      Connect = Connect + "UID=" + cUserName + ";PWD=" + cPassword
  204.    End If
  205.    If cDataBase <> "" Then
  206.      Connect = Connect + ";DATABASE=" + cDataBase
  207.    End If
  208.    'add login timeout
  209.    Connect = Connect + ";LoginTimeout=" & glLoginTimeout
  210.    DataSource = cDBName
  211.    'save the values
  212.    gstDBName = cDBName
  213.    gstDatabase = cDataBase
  214.    gstUserName = cUserName
  215.    gstPassword = cPassword
  216.    gstDataType = "ODBC"
  217.    Me.Hide
  218.    Set gCurrentDB = OpenDatabase(DataSource, False, False, Connect)
  219.    If gfDBOpenFlag = True Then
  220.      CloseAllDynasets
  221.    End If
  222.    gfTransPending = False
  223.    VDMDI.ToolBar.Visible = True
  224.    VDMDI.QueryBuilder.Visible = True
  225.    VDMDI.TblAttach.Visible = False
  226.    fSQL.CreateQueryDefbtn.Visible = False
  227.    'process the connect string just in case the
  228.    'values came from the ODBC dialogs
  229.    t = gCurrentDB.Connect
  230.    If InStr(t, "=") Then
  231.      i = 1
  232.      While i <= Len(t) + 1
  233.        If Mid(t, i, 1) = ";" Or i = Len(t) + 1 Then
  234.          If s <> "" And InStr(s, "=") > 0 Then
  235.            Select Case Mid(s, 1, InStr(1, s, "=") - 1)
  236.              Case "DSN"
  237.                gstDBName = Mid(s, InStr(1, s, "=") + 1, Len(s))
  238.              Case "DATABASE"
  239.                gstDatabase = Mid(s, InStr(1, s, "=") + 1, Len(s))
  240.              Case "DBQ"
  241.                gstDatabase = Mid(s, InStr(1, s, "=") + 1, Len(s))
  242.              Case "UID"
  243.                gstUserName = Mid(s, InStr(1, s, "=") + 1, Len(s))
  244.              Case "PWD"
  245.                gstPassword = Mid(s, InStr(1, s, "=") + 1, Len(s))
  246.               Case Else
  247.                'nothing
  248.            End Select
  249.          End If
  250.          s = ""
  251.        Else
  252.          s = s + Mid(t, i, 1)
  253.        End If
  254.        i = i + 1
  255.      Wend
  256.    End If
  257.    cDBName = gstDBName
  258.    cDataBase = gstDatabase
  259.    cUserName = gstUserName
  260.    cPassword = gstPassword
  261.    x = OSWritePrivateProfileString(gstDBName, "Database", gstDatabase, "ODBC.INI")
  262.    x = OSWritePrivateProfileString(gstDBName, "LastUser", gstUserName, "ODBC.INI")
  263.    fTables.Caption = gstDBName + "." + gstDatabase
  264.    gCurrentDB.QueryTimeout = glQueryTimeout
  265.    'success
  266.    gfDBOpenFlag = True
  267.    ResetMouse Me
  268.    Unload Me
  269.    GoTo OkayEnd
  270. OpenError:
  271.    ResetMouse Me
  272.    gfDBOpenFlag = False
  273.    If cDBName <> "" Then
  274.      If InStr(1, Error$, "Data source not found") > 0 Then
  275.        Beep
  276.        MsgBox "This DataBase has not been Registered, this will now be attempted for you!", 48
  277.        cDataBase = ""
  278.        cUserName = ""
  279.        cPassword = ""
  280.        If RegisterDB((cDBName)) = True Then
  281.          MsgBox "'" + cDBName + "' has been Registered, proceed with Open.", 48
  282.        End If
  283.      ElseIf InStr(1, Error$, "Login failed") > 0 Then
  284.        Beep
  285.        MsgBox "Invalid Parameter(s), Please try again!", 48
  286.      ElseIf InStr(1, Error$, "QueryTimeout property") > 0 Then
  287.        If glQueryTimeout <> 5 Then
  288.          Beep
  289.          MsgBox "Query Timeout Could not be set, default will be used!", 48
  290.        End If
  291.        Resume Next
  292.      Else
  293.        ShowError
  294.      End If
  295.    End If
  296.    MsgBar "Enter DataBase Parameters", False
  297.    Me.Show MODAL
  298.    Resume OkayEnd
  299. OkayEnd:
  300. End Sub
  301. Function RegisterDB (dbname As String) As Integer
  302.    On Error GoTo RDBErr
  303.    Dim driver As String
  304.    driver = InputBox("Enter Driver Name from ODBCINST.INI File:", "Driver Name", DEFAULTDRIVER)
  305.    If driver <> DEFAULTDRIVER Then
  306.      RegisterDatabase cDBName, driver, False, ""
  307.    Else
  308.      RegisterDatabase cDBName, driver, True, ""
  309.    End If
  310.    RegisterDB = True
  311.    GoTo RDBEnd
  312. RDBErr:
  313.    RegisterDB = False
  314.    Resume RDBEnd
  315. RDBEnd:
  316. End Function
  317.