473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to set 3 attempts in VBA Excel

We have a school project which is to create a VBA through Excel and I have problem about coding attempts in Log in. Everytime I open the file and put some incorrect username and password once the message box appears and say that "You have entered an invalid username/password 3 times already" what would I do to make it 3 attempts? Thanks for the help.

CommandButtons
CANCEL
OK

TextBox
Username
Password



here is my code...



Expand|Select|Wrap|Line Numbers
  1. Private Sub CANCEL_Click()
  2.  
  3. If MsgBox("Are you sure you want to quit?", vbQuestion + vbYesNo, "Exit Program?") = vbYes Then
  4. ThisWorkbook.Close 1
  5. End
  6. End If
  7.  
  8. End Sub
  9.  
  10.  
  11.  
  12. Private Sub OK_Click()
  13.  
  14. If Username.Text = "" Then
  15. MsgBox "Please enter your username to proceed.", vbExclamation, "Missing Username"
  16. Username.SetFocus
  17. ElseIf Password.Text = "" Then
  18. MsgBox "Please enter your password to proceed.", vbExclamation, "Missing Password"
  19. Password.SetFocus
  20. Else
  21.  
  22.     If Username.Text = Sheet2.Cells("2", "B") And Password.Text = Sheet2.Cells("3", "B") Then
  23.     MsgBox "Correct username and password. Access Granted.", vbInformation, "Welcome to Merry Toddlers Learning Center"
  24.     Username.Text = ""
  25.     Password.Text = ""
  26.     'Application.DisplayFullScreen = True
  27.     Login.Hide
  28.     Welcome.Show 1
  29.     Else
  30.     MsgBox "Invalid username/password. Please try again.", vbCritical, "Invalid username/password"
  31.     Tries = Tries - 1
  32.         If Tries < 1 Then
  33.         MsgBox "You have entered an invalid username/password 3 times already. Program will now terminate.", vbCritical, "Program Shutdown"
  34.         ThisWorkbook.Close 1
  35.         End
  36.         End If
  37.     End If
  38.  
  39. End If
  40.  
  41. End Sub
  42.  
  43. Private Sub Password_Change()
  44.  
  45. End Sub
  46.  
  47. Private Sub UserForm_QueryClose(CANCEL As Integer, CloseMode As Integer)
  48. CANCEL = 1
  49. Call CANCEL_Click
  50. End Sub
  51.  
  52. Private Sub Username_Change()
  53.  
  54. End Sub
Oct 23 '15 #1
3 4231
zmbd
5,501 Expert Mod 4TB
Hint Line 31
The variable is not initialized.

You should also get in to the habit of explicitly declaring your variables.
Open any Office-word/excel/database>
<alt><F11> to open the VBA editor...
Menu>Tools>Options>
>Editor tab, UNCHECK "auto Syntax Check" you're vba code will still highlight if in error, just no annoying message boxes while you type!
>CHECK the "Require Variable Declaration" option --- you will now have to use the "DIM...variablename....typecaste" construct in your code. Highly advised.
Everything else on this tab check, I prefer the tab step to be either 2 or 4 spaces.
>The other tabs are good as is for most people, review if you want.
FrenzKyleBernas
Please be aware of the fact that we do not normally answer homework questions; however, because you have made an effort and shown your work - we do often make an exception to the rule in such cases with a hint or two; however, rarely an outright answer. :)
Oct 24 '15 #2
I think using a global variable (integer type) , increasing it's value by 1 on each click of "Ok" button may help to create the logic while the variable is less than 3.
Nov 2 '15 #3
zmbd
5,501 Expert Mod 4TB
svh00987,
Unnecessary complication to the code.
OP simply needs to initialize the variable.
Nov 2 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: c duden | last post by:
Excel won't allow documents to be linked to a worksheet in office 2000 when using C# to create an add-in. The following code snippets are what was used in the attempts (there are more...
17
by: Ange T | last post by:
Hi there, I'm having pain with the VB behind an Access form. The form is used to create reports in Excel based on the details entered in the form. This has always worked without error on my...
11
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it...
0
by: Mike Knight | last post by:
(I've also posted this problem on microsoft.public.excel.programming) I have a MS Access 2003 Database named "AS400 Fields.mdb". This database contains links to tables on an AS400. In MS...
3
by: Dent2 | last post by:
I wrote a nice C# excel routine to automate the formatting of some raw CSV data. I wrote the routine on a WinXP computer with Visual Studio .NET 2003 and Office XP installed. It compiled and ran...
2
by: Steve Chatham | last post by:
I use the following code: Private Sub RbtnExport_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RbtnExport.SelectedIndexChanged Dim sFile As String =...
10
by: Steve | last post by:
I am trying to create a DLL in Visual Studio 2005-Visual Basic that contains custom functions. I believe I need to use COM interop to allow VBA code in Excel 2002 to access it. I've studied...
5
by: =?Utf-8?B?c2NobWlkdGU=?= | last post by:
Hi How can I Export an HTML Table to excel? My goal is a button, and when the user clicks this button a popup appears asking the user to 'open' or 'save' the generated Excel file. Actually...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.