I've created a login form for users to log into a database.

The User ID's & Passwords are stored within the DB. On starting my application I want to connect and open the database, then when the user enters the details carry out the nessary look-up.

Here's what I've done so far...
VB Code:
  1. Option Explicit
  2.     Dim adoCnn As New ADODB.Connection
  3.  
  4. Private Sub cmdCancel_Click()
  5.  
  6.     '* User cancelled, close application.
  7.     Unload Me
  8.    
  9. End Sub
  10.  
  11. Private Sub cmdOK_Click()
  12.  
  13.     Dim adoRstOps As ADODB.Recordset
  14.  
  15.     Dim strUser As String
  16.     Dim strPass As String
  17.     Dim strSQL As String
  18.    
  19.     '* Check that user has entered numeric values.
  20.     If Not IsNumeric(txtUser.Text) Then
  21.         '* Not all numeric, prompt user.
  22.         MsgBox "User ID must be numeric.", vbOKOnly + vbInformation, "Invalid User ID"
  23.         txtUser.Text = ""
  24.         txtUser.SetFocus
  25.         Exit Sub
  26.     End If
  27.    
  28.     If Len(txtPassword.Text) > 0 Then
  29.         If Not IsNumeric(txtPassword.Text) Then
  30.             '* Not all numeric, prompt user.
  31.             MsgBox "Password must be numeric.", vbOKOnly + vbInformation, "Invalid Password"
  32.             txtPassword.Text = ""
  33.             txtPassword.SetFocus
  34.             Exit Sub
  35.         End If
  36.     Else
  37.         '* Password not entered, advise user.
  38.         MsgBox "Please enter Password.", vbOKOnly + vbInformation, "Invalid Password"
  39.         txtPassword.SetFocus
  40.         Exit Sub
  41.     End If
  42.    
  43.     strUser = txtUser.Text
  44.     strPass = txtPassword.Text
  45.    
  46.     Set adoRstOps = New ADODB.Recordset
  47.     adoRstOps.CursorLocation = adUseClient
  48.     adoRstOps.CursorType = adOpenStatic
  49.    
  50.     strSQL = "SELECT CODE, PASSWORD FROM TB_OPERATORS"
  51.    
  52.     adoRstOps.Open strSQL, adoCnn, adOpenStatic, adLockOptimistic
  53.    
  54.     '* Check that records were returned i.e. User ID.
  55.     If adoRstOps.RecordCount > 0 Then
  56.         '* Record returned check password.
  57.         If strPass = adoRstOps!PASSWORD Then
  58.             MsgBox "You're in!"
  59.         Else
  60.             MsgBox "Password not valid.", vbOKOnly + vbInformation, "Invalid Password"
  61.         End If
  62.     Else
  63.         MsgBox "User ID not valid.", vbOKOnly + vbInformation, "Invalid User ID"
  64.         txtUser.SetFocus
  65.     End If
  66.    
  67. End Sub
  68.  
  69. Private Sub Form_Load()
  70.  
  71.     '* Database connection properties,
  72.     '* normally be either in an ini file or Registry.
  73.     adoCnn.ConnectionTimeout = 25
  74.     adoCnn.Provider = "sqloledb"
  75.     adoCnn.Properties("Data Source").Value = "Datasource"
  76.     adoCnn.Properties("Initial Catalog").Value = "Database"
  77.     adoCnn.Properties("User ID").Value = "sa"
  78.     adoCnn.Properties("Password").Value = "sapwd"
  79.    
  80.     '* Change mouse pointer whilst connecting.
  81.     frmLogin.MousePointer = vbHourglass
  82.    
  83.     '* Open Database.
  84.     adoCnn.Open
  85.    
  86.     '* Change mouse pointer back to default.
  87.     frmLogin.MousePointer = vbDefault
  88.    
  89. End Sub
  90.  
  91. Private Sub Form_Unload(Cancel As Integer)
  92.  
  93.     '* If connected close connection to Database.
  94.     If adoCnn.State = adStateOpen Then
  95.         adoCnn.Close
  96.     End If
  97.    
  98.     End
  99.    
  100. End Sub
  101.  
  102. Private Sub txtUser_Change()
  103.  
  104.     '* Enable OK command button if entry greater than 0.
  105.     If Len(txtUser.Text) > 0 Then
  106.         cmdOK.Enabled = True
  107.     Else
  108.         '* Disable OK command button if entry is 0.
  109.         cmdOK.Enabled = False
  110.     End If
  111.    
  112. End Sub

The above code works fine.

I want the form to show and then establish the connection to the database, and in the event of the database not being available advise the user.

How can I do this?

I've tried putting the Form_Load() event code into the Form_GotFocus() event but the code doesn't run.

Any ideas?

Regards