Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loggin On or Not 1

Status
Not open for further replies.
Dec 5, 2001
82
GB
Hi,

This probably is fairly simple. but I've got a userform which kicks off on the workbook open event on this is 2 labels into which the user enters their password and username. When they click a command button this is passed to a logon string. This works fine. But what I'm trying to do is anticipate the user putting in the wrong information what I want to do is when the user puts this in on the click of the command button if the connection fails for it to go back to the userform and allow the user to enter again, but I only want this to happen 3 times. Once it fails a 3rd time I want the workbook to close.

Where do I put the code this is the code I'm presently using:

Workbook open()
Private Sub Workbook_Open()
Load UserForm1
UserForm1.StartUpPosition = 2
UserForm1.Show

Private Sub CommandButton1_Click()

sUserid = LCase(TextBox1.Value)

sPassw = LCase(TextBox2.Value)



Dim Application As CRPEAuto.Application
Dim Report As CRPEAuto.Report
Dim CrystalExportOptions As ExportOptions
Dim connectionid
Set Application = CreateObject("Crystal.CRPE.Application")

Dim counter As Integer


If counter > 3 Then Workbooks("Work").Close (False)

On Error Resume Next

connectionid = Application.LogOnServer("pdsodbc.dll", "XXXX", "XXXX", sUserid, sPassw)

If Err.Number = 20536 Then
MsgBox "Incorrect Password and/or User id. You have another " & 3 - counter & " attempts", 48, "WARNING"
Else: UserForm.Show
counter = counter + 1
TextBox1.SetFocus
End If


Next counter
End Sub


I'm using VBA and calling a crystal object which is fine it just the looping of the login which I'm trying to get

Thanks

[elephant2]
 
Hi just set a counter, incrementing it each time it loops.
With each failure until the limit use the GoTo command with a label.

blah
blah
countme = 0

tryagain:

blah
countme = countme + 1
blah

If countme < 4 Then GoTo tryagain Thank you,
Dave Rattigan
 
Ratman,

Thanks for the response. Where do I put these, which event kicks them off.


Thanks
 
I think the &quot;countme&quot; variable should be declared as a Static variable in the form's module level, as opposed to the procedure level.

That way, the variable will retain its previous value from the last time you called the procedure to check the logon ID/password. That way, you can avoid the looping. The way I interpret this is that there will be a click event on a button, and unless the counter retains its value from the last click event, it will start at the 0 value each time.
 
Thanks Kitter,

Your input put me on the right track I've made the Counter Public. Once the commandbutton is clicked in goes there the process of connecting to the database, if it's incorrect I send it back to the Userform_Activate sub and as it keeps the value if the counter is greater than 3 it closes the workbook otherwise it just waits until the commandbutton is clicked again which it then runs the commandbutton click module again but retaining the counter value. And so on and so on until the counter hits 4.

[roll2]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top