locked
If Statement - VBA RRS feed

  • Question

  • Hi,

    I'm a novice VBA user and I'm having trouble with a certain code.  I'm trying to create a code for a commandbutton that links to another sheet in the same spreadsheet.  When the user clicks the commandbutton I want a message to popup to warn them where they are headed.  I only want this message to popup the first time they click it during their session.  I thought it might require an if statement, such as if the button is has not been clicked then display message, if it's been clicked once, then don't display message.  Anybody know how to create a code for this?  Thanks I appreciate any help.  

    Mike 
    Monday, September 22, 2008 8:02 PM

Answers

  • Here is a little code that will kinda depict what you are asking for with the prompt... It is more than likely not the cleanest code, but it will get the job done.

    Dim clickCheck As Boolean = False
        Private Sub btnSpreadsheet_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles NewToolStripButton.Click  
            frmNewMessage.ShowDialog()  
            If clickCheck = False Then 
                Dim res As DialogResult  
                res = MessageBox.Show("You are about to leave ... spreadsheet""Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning)  
                If res = Windows.Forms.DialogResult.OK Then 
                    Dim NewSpreadsheetForm As frmSpreadSheet  
                    NewSpreadsheetForm.show()  
                    clickCheck = True 
                End If 
            ElseIf clickCheck = True Then 
                Dim NewSpreadsheetForm As frmSpreadSheet  
                NewSpreadsheetForm.show()  
            End If 
        End Sub 
    • Edited by mkruluts Tuesday, September 23, 2008 4:06 AM
    • Proposed as answer by Danny Thompson Tuesday, September 23, 2008 8:36 AM
    • Marked as answer by Martin Xie - MSFT Wednesday, September 24, 2008 2:42 AM
    Tuesday, September 23, 2008 4:06 AM

All replies

  • Monday, September 22, 2008 8:39 PM
  • Here is a little code that will kinda depict what you are asking for with the prompt... It is more than likely not the cleanest code, but it will get the job done.

    Dim clickCheck As Boolean = False
        Private Sub btnSpreadsheet_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles NewToolStripButton.Click  
            frmNewMessage.ShowDialog()  
            If clickCheck = False Then 
                Dim res As DialogResult  
                res = MessageBox.Show("You are about to leave ... spreadsheet""Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning)  
                If res = Windows.Forms.DialogResult.OK Then 
                    Dim NewSpreadsheetForm As frmSpreadSheet  
                    NewSpreadsheetForm.show()  
                    clickCheck = True 
                End If 
            ElseIf clickCheck = True Then 
                Dim NewSpreadsheetForm As frmSpreadSheet  
                NewSpreadsheetForm.show()  
            End If 
        End Sub 
    • Edited by mkruluts Tuesday, September 23, 2008 4:06 AM
    • Proposed as answer by Danny Thompson Tuesday, September 23, 2008 8:36 AM
    • Marked as answer by Martin Xie - MSFT Wednesday, September 24, 2008 2:42 AM
    Tuesday, September 23, 2008 4:06 AM