Join 136,549 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,874 people online right now. Registration is fast and FREE... Join Now!
If you want full application to be closed then use END keyword (Its not really recommended) or use below code
vb
Dim frm As Form For Each frm In Forms Unload frm Next frm
If you want a particular form to be closed use Me.Hide. Bascially Unload Me also should work.
What if I were to want to have it perform some checks...and if there's a problem, it will pop up a MsgBox indicating the problem & giving the user the option to click OK to go back to the form & fix the problem, or if they hit Cancel it will end everything? How do I get it to momentarily hide the form where they input the stuff, but when there's a problem (if they click OK to fix it), go back to the form it just hid & have them be able to edit to fix the value that caused a problem?
Does that make any sense?
Example: If the entry in the one TextBox is a duplicate somewhere, it will say ______ already exists in list! Do you want to go back & re-enter a new entry (click OK) or cancel to go back to the spreadsheet?
If you want full application to be closed then use END keyword (Its not really recommended) or use below code
vb
Dim frm As Form For Each frm In Forms Unload frm Next frm
If you want a particular form to be closed use Me.Hide. Bascially Unload Me also should work.
What if I were to want to have it perform some checks...and if there's a problem, it will pop up a MsgBox indicating the problem & giving the user the option to click OK to go back to the form & fix the problem, or if they hit Cancel it will end everything? How do I get it to momentarily hide the form where they input the stuff, but when there's a problem (if they click OK to fix it), go back to the form it just hid & have them be able to edit to fix the value that caused a problem?
Does that make any sense?
Example: If the entry in the one TextBox is a duplicate somewhere, it will say ______ already exists in list! Do you want to go back & re-enter a new entry (click OK) or cancel to go back to the spreadsheet?
I think I have it...at least it does what I want.
Code so far (adapting from code on previous project...see my thread about incorporating cmd .exe with parameters thread)
Dim OutFile As Integer Dim retVal As Variant 'Dim DiscNumber As Integer Dim DiscNumber As Integer 'FOR Error CHECKING, would probably need to change to boolean
Dim OutFileNamePath Dim CDDriveLetter Dim CDDrivePath
Dim sFilename, shtMain As Worksheet, shtTemp As Worksheet Dim shtFull As Worksheet, r1 As Long, r2 As Long, nBottomRow As Long
Dim rowCountTemp As Long, rowCountTemp2 As Long Dim discBy Dim findDiscStr As String Dim Cancel As Boolean Dim warningCellBox Dim warningFileBox Dim rngFound As Range Dim lastPrompt Dim lastWarning Dim discNumberError1 ' Dim isDiscInteger As Integer ' Dim A As Single ' Dim B As Long ' Dim C As Single Dim diagnosMsgBox Dim byWarnMsgBox Dim driveNotReadyMsgBox 'Dim tempMsgBox1 'Dim tempMsgBox2 'Dim tempMsgBox3 'Dim tempMsgBox4 Dim MsgNotNumeric Dim MsgDriveLetterBlank
Dim fs, d, s, t 'this is to initialize the parts to check if drive is ready!
OutFile = FreeFile()
InputDiscNumber: Application.EnableCancelKey = xlErrorHandler ' this allows user to cancel by escape key '
' On Error GoTo ErrorCheck 'I DON'T LIKE HOW IT WON'T PROMPT USER IF THEY LEAVE 'FORM BLANK OR ENTER NON INTEGER DISC #. ' I might want to do something On Error goto a line that checks if it's an ' Integer, if not then ask you if you want to cancel or try again. Then goto ' InputDiscNumber
' On Error Resume Next 'ANOTHER THOUGHT I HAD...commenting out for now.
'TryAgain: 'On Error GoTo EndCancel 'neither of these seem to help 2nd time cancelled Me.Hide If IsNumeric(DiscNumberInput) = False Then MsgNotNumeric = MsgBox("The value you input for Disc Number, " & DiscNumberInput _ & " appears to not be a number! Press OK to re-enter or Cancel to End", vbOKCancel) If MsgNotNumeric = vbCancel Then End Else Me.Show End If End If
DiscNumber = DiscNumberInput 'below code was just copied/pasted & modified for diagnostic: 'DiscNumber = InputBox("Enter Disc NUMBER ONLY (only type the number.) Example: If you're on Disc # 4, you need to type just the number 4-NOT Disc 4, NOT Disc # 4. Just type 4. Otherwise it will error." & vbCrLf & vbCrLf & "NOTE: if you cancel this window twice you'll get a run-time error the second time around when you hit cancel. I have not found a way around this. If this happens, just hit End & run the program again")
'On Error GoTo EndCancel 'neither of these seem to help 2nd time cancelled 'tempMsgBox1 = MsgBox("You entered " & DiscNumber)
'NEED TO ADD NEW CODE TO HANDLE THIS!!!!!!!!!!!
ErrorCheck:
' COMMENTED OUT FOR NOW; NOT WORKING!!!
' isDiscInteger = IsNumeric(DiscNumber) ' If isDiscInteger Then ' A = CSng(DiscNumber) ' B = CLng(A) ' C = CSng(B) ' End If
' If isDiscInteger And C = A Then ' GoTo Part1 ' Else
' discNumberError1 = MsgBox("It appears you did not enter a valid, " _ ' & "POSITIVE, INTEGER value for DiscNumber. Try again.") ' GoTo EndCancel
'CHECK TO SEE IF THAT TEXT FILE ALREADY EXISTS: If Len(Dir(OutFileNamePath)) > 0 Then 'NOTE: I might also be able to accomplish this with FileExists() 'MsgBox ("OutFileNamePath > 0") 'On Error GoTo EndCancel
warningFileBox = MsgBox("Uh oh! " & OutFileNamePath & " already exists! " _ & vbCrLf & "Please select another disc number or cancel. " _ & vbCrLf & vbCrLf & "You may need to delete the existing file: " & OutFileNamePath, vbOKCancel)
' Case vbOK ' ACTUALLY THIS WAS CAUSING MY PROBLEM; NEED vbCancel first then CASE ELSE ' tempMsgBox3 = MsgBox("Case vbOK 1") ' On Error GoTo EndCancel ' DiscNumber = InputBox("Enter Disc Number--Positive Integer Only") ' tempMsgBox2 = MsgBox("2nd attempt at entering DiscNumber results in: " & DiscNumber)
' GoTo Part1
'Better, fixed code below: If warningFileBox = vbCancel Then ' tempMsgBox4 = MsgBox("vbCancel for 1st part on check file name duplicate") 'JUST DIAGNOSTIC LINE; commented out End ' switched these around!! Else Me.Show
'DiscNumber = InputBox("Enter Disc Number--Positive Integer Only") ' this is old code! 'tempMsgBox2 = MsgBox("2nd attempt at entering DiscNumber results in: " & DiscNumber)
End If
'Above code is to CHECK TO SEE IF TEXT FILE ALREADY EXISTS.
If rngFound Is Nothing Then GoTo InputDriveLetter ' PROBABLY WON'T NEED THIS CODE! Else 'On Error GoTo EndCancel warningCellBox = MsgBox("Uh oh! " & findDiscStr _ & " already exists in spreadsheet! " _ & vbCrLf & vbCrLf & "Click OK to re-enter or Cancel to End" _ & "(Click OK if you want to check the spreadsheet, for example.)", vbOKCancel)
If warningCellBox = vbCancel Then ' On Error GoTo EndCancel End Else
Me.Show
End If
End If
GoTo InputDriveLetter ' Important!
InputDriveLetter:
' On Error GoTo EndCancel ' I don't particularly like this solution; worried might cancel ' when it shouldn't. This SHOULD cancel the macro running ' if user clicks cancel or clicks OK without entering anything. ' It does this OK, but I'm worried if there's another error ' it might cancel when another action would be appropriate
CDDriveLetter = CDDriveLetterInput
If CDDriveLetter = "" Then ' NOT SURE IF THIS WILL WORK! NEED TO CHECK! 'MsgBox "vbCancel" MsgDriveLetterBlank = MsgBox("CD Drive Letter TextBox appears to have been " _ & "left blank! Click OK to re-enter or Cancel to end", vbOKCancel)
'WANT TO CHECK ABOVE CODE!!!!!!!!!!!!
If MsgDriveLetterBlank = vbCancel Then End Else Me.Show End If
Else 'MsgBox "runScript" GoTo DriveReadyCheck
End If
DriveReadyCheck:
'THIS CODE CHECKS TO SEE IF THE CD DRIVE IS READY BEFORE STARTING FILELIST.EXE:
CDDrivePath = CDDriveLetter & ":\"
Set fs = CreateObject("Scripting.FileSystemObject") Set d = fs.GetDrive(CDDrivePath) Select Case d.DriveType Case 0: t = "Unknown" Case 1: t = "Removable" Case 2: t = "Fixed" Case 3: t = "Network" Case 4: t = "CD-ROM" Case 5: t = "RAM Disk" End Select
If d.IsReady And t = "CD-ROM" Then GoTo RunScript
Else driveNotReadyMsgBox = MsgBox("Drive with path: " & CDDrivePath & " is not ready! " _ & vbCrLf & vbCrLf & " Would you like to RETRY (wait for drive) *OR* CANCEL (re-enter drive letter/" _ & "stop script?). " & vbCrLf & vbCrLf _ & "If you want to cancel out of project entirely, just click cancel (again) in " _ & "next window that will pop up." & vbCrLf & "Or if you want to WAIT for drive to become ready, " _ & "hit Retry after drive is ready.", vbRetryCancel)
If driveNotReadyMsgBox = vbRetry Then GoTo DriveReadyCheck
USername is a textbox control right? if it is a textbox you are goint to change the property value to text.
vb
Private Sub CMD_CANCEL_Click() 'ISDR_1.Show Unload Me End Sub
Private Sub CMD_CONFIRM_Click()
'IF STATEMENT TO CHECK FOR PASSWORD VALIDITY 'Check if user left it blank/INCORRECT
if USERNAME.TEXT = "" OR PASSWORD.TEXT = "" THEN if USERNAME.TEXT = "" then msgbox "Please type your username",vbexclamation else if PASSWORD = "" then msgbox "please type the password!",vbexclamation
Exit sub End if
If USERNAME.TEXT = "ADMIN" AND PASSWORD.TEXT ="ADMIN" THEN Msgbox "Access Granted",vbinformation else Msgbox "Incorrect username and password", vbexclamation Unload me end if
Private Sub CMD_CANCEL_Click() 'ISDR_1.Show Unload Me End Sub Private Sub CMD_CONFIRM_Click() 'IF STATEMENT TO CHECK FOR PASSWORD VALIDITY 'Check if user left it blank/INCORRECT
if USERNAME.TEXT = "" OR PASSWORD.TEXT = "" THEN if USERNAME.TEXT = "" then msgbox "Please type your username",vbexclamation else if PASSWORD = "" then msgbox "please type the password!",vbexclamation Exit sub End if
If USERNAME.TEXT = "ADMIN" AND PASSWORD.TEXT ="ADMIN" THEN Msgbox "Access Granted",vbinformation else
Unload me end if
End Sub
private Sub Form_unload(Cancel) if vbok = Msgbox ("TryAgain" ,vbOkCacnel) then cancel =true End If End Sub
This post has been edited by thava: 19 Aug, 2008 - 04:32 PM
Hi everyone and thank you so much for helping me out... I'm just starting out on VB so pretty confused. I tried all the codes shown but the previous form still doesnt unload. this are the codes that are working fine but still when i click confirm, the next page appears on top of the login page...the login page still doesnt disappear... Please help!!
[code] Private Sub CMD_CANCEL_Click() ISDR_1.Show 'Unload Me End Sub
Private Sub CMD_CONFIRM_Click() 'IF STATEMENT TO CHECK FOR PASSWORD VALIDITY 'Check if user left it blank/INCORRECT
If USERNAME.Text = "" Then MsgBox "Please enter your username", vbExclamation
ElseIf PASSWORD = "" Then MsgBox "Please enter your password!", vbExclamation
ElseIf USERNAME.Text = "ADMIN" And PASSWORD.Text = "ADMIN" Then MsgBox "Access Granted", vbInformation ADMIN_MENU.Show 'unload me or End or Me.Hide -tHIS FUNCTION DOESNT WORK!
Else MsgBox "Incorrect username and password", vbExclamation ISDR_1.Show End If
Hi everyone and thank you so much for helping me out... I'm just starting out on VB so pretty confused. I tried all the codes shown but the previous form still doesnt unload. this are the codes that are working fine but still when i click confirm, the next page appears on top of the login page...the login page still doesnt disappear... Please help!!
[code] Private Sub CMD_CANCEL_Click() ISDR_1.Show 'Unload Me End Sub
Private Sub CMD_CONFIRM_Click() 'IF STATEMENT TO CHECK FOR PASSWORD VALIDITY 'Check if user left it blank/INCORRECT
If USERNAME.Text = "" Then MsgBox "Please enter your username", vbExclamation
ElseIf PASSWORD = "" Then MsgBox "Please enter your password!", vbExclamation
ElseIf USERNAME.Text = "ADMIN" And PASSWORD.Text = "ADMIN" Then MsgBox "Access Granted", vbInformation ADMIN_MENU.Show 'unload me or End or Me.Hide -tHIS FUNCTION DOESNT WORK!
Else MsgBox "Incorrect username and password", vbExclamation ISDR_1.Show End If
End Sub
I'm assuming you're meaning to comment out the unload statements (so they won't run)?
When you say it doesn't work, Unload Me isn't hiding the form? Are you getting an error?
Sorry I'm just confused...could you explain a little more, please?
Another thing you can do is instead of Me...you might be able to use the name of the form
Like MyForm1.Hide rather than Me.Hide
Also, in looking at your code: ElseIf USERNAME.Text = "ADMIN" And PASSWORD.Text = "ADMIN" Then MsgBox "Access Granted", vbInformation ADMIN_MENU.Show 'unload me or End or Me.Hide -tHIS FUNCTION DOESNT WORK!
Are you wanting to unload the Admin_menu? That's what it would appear your code is telling it to do (I'm a n00b, so I'm not sure if unload Me always does the thing currently loaded or if it loads up something new & you say Unload Me, if it'll unload the new thing you showed.
What happens when you move the Me.Hide above ADMIN_Menu.Show?
Hi everyone and thank you so much for helping me out... I'm just starting out on VB so pretty confused. I tried all the codes shown but the previous form still doesnt unload. this are the codes that are working fine but still when i click confirm, the next page appears on top of the login page...the login page still doesnt disappear... Please help!!
[code] Private Sub CMD_CANCEL_Click() ISDR_1.Show 'Unload Me End Sub
Private Sub CMD_CONFIRM_Click() 'IF STATEMENT TO CHECK FOR PASSWORD VALIDITY 'Check if user left it blank/INCORRECT
If USERNAME.Text = "" Then MsgBox "Please enter your username", vbExclamation
ElseIf PASSWORD = "" Then MsgBox "Please enter your password!", vbExclamation
ElseIf USERNAME.Text = "ADMIN" And PASSWORD.Text = "ADMIN" Then MsgBox "Access Granted", vbInformation ADMIN_MENU.Show 'unload me or End or Me.Hide -tHIS FUNCTION DOESNT WORK!
Else MsgBox "Incorrect username and password", vbExclamation ISDR_1.Show End If
End Sub
I'm assuming you're meaning to comment out the unload statements (so they won't run)?
When you say it doesn't work, Unload Me isn't hiding the form? Are you getting an error?
Sorry I'm just confused...could you explain a little more, please?
Another thing you can do is instead of Me...you might be able to use the name of the form
Like MyForm1.Hide rather than Me.Hide
Also, in looking at your code: ElseIf USERNAME.Text = "ADMIN" And PASSWORD.Text = "ADMIN" Then MsgBox "Access Granted", vbInformation ADMIN_MENU.Show 'unload me or End or Me.Hide -tHIS FUNCTION DOESNT WORK!
Are you wanting to unload the Admin_menu? That's what it would appear your code is telling it to do (I'm a n00b, so I'm not sure if unload Me always does the thing currently loaded or if it loads up something new & you say Unload Me, if it'll unload the new thing you showed.
What happens when you move the Me.Hide above ADMIN_Menu.Show?
Hello
Yes i just commented the unload me cuz it wasnt working and i had to remind myself it needed working on
initially the unload me didnt hide the forms..i.e after each click the forms just kept piling one on top of the other I did try replacing me.hide with me.userform1 but it didnt work either
I wanted to unload the admin menu or any of the forms that had served their purpose after the user had clicked on them I followed ur suggestion. I placed the unload me above the admin_menu.show and walla! it works!
Thank you so much one problem solved...few more to go!