+ Reply to Thread
Results 1 to 3 of 3

Validation Msg Box Issue

Hybrid View

  1. #1
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Validation Msg Box Issue

    Hi

    I have a userform which has validated text box's, when trying to input invalid data a msg box appears.

    Issue that i am having is that when i click my update command button and data is exported to my sheet it displays the all of the msg box messages for each text box. I presume that this is caused by the text box's being cleared after they have been copied to my excel sheet but I dont know how to fix it.

    This is my Update command code

    Private Sub CommandButton1_Click()
    Dim iRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("CVdata")
    
    'find first empty row in database
    iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
    
    'check for a Name number
    If Trim(Me.CVdate.Value) = "" Then
    Me.CVdate.SetFocus
    MsgBox "Please complete the form"
    Exit Sub
    End If
    
    'copy the data to the database
    ws.Cells(iRow, 1).Value = Me.Calendar1.Value
    ws.Cells(iRow, 2).Value = Me.ComboBox1.Value
    ws.Cells(iRow, 3).Value = Me.TextBox1.Value
    ws.Cells(iRow, 4).Value = Me.TextBox2.Value
    ws.Cells(iRow, 5).Value = Me.TextBox3.Value
    ws.Cells(iRow, 6).Value = Me.TextBox4.Value
    ws.Cells(iRow, 7).Value = Me.TextBox6.Value
    ws.Cells(iRow, 8).Value = Me.TextBox8.Value
    ws.Cells(iRow, 9).Value = Me.TextBox9.Value
    ws.Cells(iRow, 10).Value = Me.TextBox7.Value
    ws.Cells(iRow, 11).Value = Me.TextBox10.Value
    
    
    MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
    'clear the data
    Me.CVdate.Value = ""
    Me.ComboBox1.Value = ""
    Me.TextBox1.Value = ""
    Me.TextBox2.Value = ""
    Me.TextBox3.Value = ""
    Me.TextBox4.Value = ""
    Me.TextBox6.Value = ""
    Me.TextBox8.Value = ""
    Me.TextBox9.Value = ""
    Me.TextBox7.Value = ""
    Me.TextBox10.Value = ""
    Me.CVdate.SetFocus
    End Sub
    
    Private Sub CVdate_Change()
    CVdate.Value = Format(CVdate.Value, "dd-mm-yyyy")
    If Not IsDate(CVdate) Then CVdate = ""
    If Not IsDate(Me.CVdate) Then
            MsgBox "Select Date from Calender"
            Cancel = True
        End If
    End Sub
    This is the code i use to validate my textboxs

    Private Sub TextBox2_Change()
    Dim Num As Variant
    Num = TextBox2.Value
    If Not IsNumeric(TextBox2) Then TextBox2 = ""
    If Not IsNumeric(Num) Then
    MsgBox "You must enter a number"
    Exit Sub
    End If
    End Sub
    Have attached the spreadsheet as well

    Please can you help?
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    11-20-2012
    Location
    Seattle, WA USA
    MS-Off Ver
    Excel 2010
    Posts
    597

    Re: Validation Msg Box Issue

    http://www.fontstuff.com/ebooks/free/fsUserForms.pdf

    take a look at this, i would suggest not using the private sub, but rather have the validation as part of the command button click macro

  3. #3
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Validation Msg Box Issue

    Thank you so much, all working now and better than what i wanted

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1