Welcome to Dream.In.Code
Getting VB Help is Easy!

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!




AUTO CLOSE FORMS AFTER SELECTION

 
Reply to this topicStart new topic

AUTO CLOSE FORMS AFTER SELECTION, VB/EXCEL CODING

ALVINCHAAND
17 Aug, 2008 - 11:39 AM
Post #1

New D.I.C Head
*

Joined: 17 Aug, 2008
Posts: 18


HOW DO I AUTO CLOSE THE FORMS AFTER USER SELECTION. I HAVE TRIED USING THE "UNLOAD ME" METHOD BUT IT DOESNT WORK IN ALL INSTANCES


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 Me.USERNAME.Value = "" And Me.PASSWORD.Value = "" Then
    MsgBox "Please fill in the required fields", vbExclamation
    
    ElseIf Me.USERNAME.Value = "" Then
    MsgBox "Please enter USERNAME", vbExclamation
    
    ElseIf Me.PASSWORD.Value = "" Then
    MsgBox "Please enter PASSWORD", vbExclamation
    
    ElseIf USERNAME.Value = "ADMIN" And PASSWORD.Value = "ADMIN" Then
    ADMIN_MENU.Show
  
    Else: MsgBox "Please enter VALID USERNAME/PASSWORD", vbExclamation
    End If

'Unload Me

End Sub




' NEED TO CLOSE THIS DIALOG AFTER CORRECT ENTRY IS MADE BUT THE UNLOAD ME DOESNT WORK


User is offlineProfile CardPM
+Quote Post

dineeshd
RE: AUTO CLOSE FORMS AFTER SELECTION
18 Aug, 2008 - 12:49 AM
Post #2

D.I.C Addict
Group Icon

Joined: 30 Jun, 2008
Posts: 578



Thanked: 16 times
Dream Kudos: 575
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

cmount
RE: AUTO CLOSE FORMS AFTER SELECTION
18 Aug, 2008 - 06:15 AM
Post #3

New D.I.C Head
*

Joined: 1 Aug, 2008
Posts: 41



Thanked: 1 times
My Contributions
QUOTE(dineeshd @ 18 Aug, 2008 - 01:49 AM) *

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?


User is offlineProfile CardPM
+Quote Post

cmount
RE: AUTO CLOSE FORMS AFTER SELECTION
18 Aug, 2008 - 07:26 AM
Post #4

New D.I.C Head
*

Joined: 1 Aug, 2008
Posts: 41



Thanked: 1 times
My Contributions
QUOTE(cmount @ 18 Aug, 2008 - 07:15 AM) *

QUOTE(dineeshd @ 18 Aug, 2008 - 01:49 AM) *

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)

CODE

Public Sub ButtonCancel_Click()

Unload Me

End Sub

Public Sub ButtonOK_Click()

Beginning:
    Const MY_FILENAME = "c:\TEMP\RUNFILELIST_EXE.BAT"
    
    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
    
   'GoTo InputDiscNumber
'    End If
  
Part1:
    
    OutFileNamePath = "C:\Temp\Disc" & DiscNumber & ".txt"
    
    '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.
    
Else
        
FileNameOK:
    
    findDiscStr = "Disc " & DiscNumber

CellReCheck:
    
    Worksheets("Full").Activate
    Sheets("Full").Select
    
    Set rngFound = Cells.Find(What:=findDiscStr, After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
        SearchFormat:=False)
    
        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
        
        Else
            End
        
        End If
        
        
    End If

    
    
        
End If

End Sub



Again, this is just what I have so far.

User is offlineProfile CardPM
+Quote Post

thava
RE: AUTO CLOSE FORMS AFTER SELECTION
18 Aug, 2008 - 05:53 PM
Post #5

D.I.C Regular
Group Icon

Joined: 17 Apr, 2007
Posts: 450



Thanked: 18 times
Dream Kudos: 50
My Contributions
sorry for the interruption

there is a event form_unload(cancel)

if you set cancel = true

then the form unload will be canceled

i think this would solve your problem


User is offlineProfile CardPM
+Quote Post

tope10
RE: AUTO CLOSE FORMS AFTER SELECTION
18 Aug, 2008 - 09:12 PM
Post #6

New D.I.C Head
*

Joined: 25 Jun, 2008
Posts: 22



Thanked: 2 times
My Contributions
QUOTE
Is this a login form?

if it is a login form here is the modification..

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

End Sub



User is offlineProfile CardPM
+Quote Post

thava
RE: AUTO CLOSE FORMS AFTER SELECTION
19 Aug, 2008 - 04:25 PM
Post #7

D.I.C Regular
Group Icon

Joined: 17 Apr, 2007
Posts: 450



Thanked: 18 times
Dream Kudos: 50
My Contributions
try this code
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 = "" 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
User is offlineProfile CardPM
+Quote Post

ALVINCHAAND
RE: AUTO CLOSE FORMS AFTER SELECTION
21 Aug, 2008 - 04:51 AM
Post #8

New D.I.C Head
*

Joined: 17 Aug, 2008
Posts: 18

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






User is offlineProfile CardPM
+Quote Post

cmount
RE: AUTO CLOSE FORMS AFTER SELECTION
21 Aug, 2008 - 06:06 AM
Post #9

New D.I.C Head
*

Joined: 1 Aug, 2008
Posts: 41



Thanked: 1 times
My Contributions
QUOTE(ALVINCHAAND @ 21 Aug, 2008 - 05:51 AM) *

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?
User is offlineProfile CardPM
+Quote Post

ALVINCHAAND
RE: AUTO CLOSE FORMS AFTER SELECTION
21 Aug, 2008 - 12:30 PM
Post #10

New D.I.C Head
*

Joined: 17 Aug, 2008
Posts: 18

QUOTE(cmount @ 21 Aug, 2008 - 07:06 AM) *

QUOTE(ALVINCHAAND @ 21 Aug, 2008 - 05:51 AM) *

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 smile.gif one problem solved...few more to go!

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:08PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month