Results 1 to 7 of 7

Thread: SQL Login form

  1. #1

    Thread Starter
    Hyperactive Member Pozzi's Avatar
    Join Date
    Feb 2001
    Location
    The Stones!
    Posts
    507

    SQL Login form

    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
    VB.Net (VS 2010)

  2. #2
    Hyperactive Member
    Join Date
    May 2002
    Location
    Omaha, NE
    Posts
    263
    I changed your form_load code to say:

    Code:
    Private Sub Form_Load()
        On Error GoTo ErrorHandler
        '* Database connection properties,
        '* normally be either in an ini file or Registry.
        adoCnn.ConnectionTimeout = 25
        adoCnn.Provider = "sqloledb"
        adoCnn.Properties("Data Source").Value = "ctgsql02"
        adoCnn.Properties("Initial Catalog").Value = "claimsa"
        adoCnn.Properties("Integrated Security").Value = "SSPI"
        
        '* Change mouse pointer whilst connecting.
        frmLogin.MousePointer = vbHourglass
        
        '* Open Database.
        adoCnn.Open
        
        '* Change mouse pointer back to default.
        frmLogin.MousePointer = vbDefault
        Exit Sub
        
    ErrorHandler:
        Select Case Err.Number
            Case -2147467259
                MsgBox "The database " & adoCnn.Properties("Initial Catalog").Value & " is not currently available.", vbOKOnly
            Case Else
                Err.Raise Err.Number, Err.Source, Err.Description
        End Select
    End Sub
    ...and I think it does what you are asking, unless I'm just not understanding your problem correctly.

  3. #3

    Thread Starter
    Hyperactive Member Pozzi's Avatar
    Join Date
    Feb 2001
    Location
    The Stones!
    Posts
    507
    That handles the error, but it doesn't allow the form to be shown whilst trying to establish the onnection to the DB.

    This is what I'm trying to do....

    1) App starts and loads the form.
    2) Form loaded/shown.
    3) Connection made to DB.
    4) Show connection progress in i.e. Label.

    If DB not on-line, show user a message, else allow the user to use the form and login.

    Regards
    VB.Net (VS 2010)

  4. #4
    Hyperactive Member
    Join Date
    May 2002
    Location
    Omaha, NE
    Posts
    263
    The error handling that I added, should allow you to check and see if the db is on-line. From there, you know what to do, right?

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Originally posted by Pozzi
    This is what I'm trying to do....

    1) App starts and loads the form.
    2) Form loaded/shown.
    3) Connection made to DB.
    4) Show connection progress in i.e. Label.

    If DB not on-line, show user a message, else allow the user to use the form and login.

    Regards
    Try adding a timer to the form and set the .Enabled property to
    false and the .Interval property to 10. Then add the code to the
    last line of your code in the form load event.
    Also move your form load code to a sub called ConnectToSQL.
    Code:
        'LAST LINE OF FORM_LOAD() EVENT
        Timer1.Enabled = True
    
    End Sub
    
    Private Sub Timer1_Timer()
    
        Call ConnectToDB()
        Timer1.Enabled = False
    
    End Sub
    This will let the form completely load and show. Then after 10 miliseconds the timer
    will invoke the ConnectToDB procedure. Allowing you to show
    progress, connection state, etc. on your form.

    HTH.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Lively Member
    Join Date
    Aug 2002
    Location
    India
    Posts
    97

    Use sub main

    Hi Pozzi,
    you need to put all your code establishing the connection in sub main in a standard module(preferably).
    use show method of your login form if the connection succeeds. If it fails you can stop loading the login form and inform the user that there is an error

    A better option is to create a splash screen, call sub main on its on_load event.
    if connection succeeds, unload the splash screen, show the login form else unload the splash screen and show message.
    Hope that helps
    Last edited by tvssarma; Aug 19th, 2003 at 10:31 AM.
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
    Dont quote anyone if you can help it -
    THM
    /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

  7. #7

    Thread Starter
    Hyperactive Member Pozzi's Avatar
    Join Date
    Feb 2001
    Location
    The Stones!
    Posts
    507
    Hi,

    I've tried both ways, Timer & using a Sub and still not getting it to work without problems.

    With the Timer the form is frozen, with the sub the form loads but only as an outline.

    Surely this shouldn't be this hard to do, and must have been done in the past.

    Any samples or additional help would be much appreciated.

    Regards
    VB.Net (VS 2010)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width