+ Reply to Thread
Results 1 to 6 of 6

How do I prevent empty TextBoxes from updating the database

  1. #1
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    How do I prevent empty TextBoxes from updating the database

    This follows in the footsteps of my previous posting from 10 minutes ago, and is part of the same UserForm code:
    Below is the example of what I've written (I'm only showing 2 out of the 20 Loops). This works fine, except that when I click cmdOK, all twenty items (rows) are added to the database. I only want to add the rows for which the user has added a value into the TextBox. For instance. the user may only enter a value in TextBox1 and TextBox8. Those are the only lines (row) I want to see added to the database.

    Thanks for any help you may be able to provide!

    Private Sub cmdOK_Click()
    ActiveWorkbook.Sheets("TESTTYPE").Activate
    Range("A1").Select
    Do
    If IsEmpty(ActiveCell) = False Then
    ActiveCell.Offset(1, 0).Select
    End If
    Loop Until IsEmpty(ActiveCell) = True
    ActiveCell.Value = cboName1.Value
    ActiveCell.Offset(0, 1) = ComboBox.Value
    ActiveCell.Offset(0, 2).Value = Label1
    ActiveCell.Offset(0, 3).Value = txtStartDate.Value
    ActiveCell.Offset(0, 4).Value = TextBox1.Value
    Do
    If IsEmpty(ActiveCell) = False Then
    ActiveCell.Offset(1, 0).Select
    End If
    Loop Until IsEmpty(ActiveCell) = True
    ActiveCell.Value = cboName1.Value
    ActiveCell.Offset(0, 1) = ComboBox.Value
    ActiveCell.Offset(0, 2).Value = Label12
    ActiveCell.Offset(0, 3).Value = txtStartDate.Value
    ActiveCell.Offset(0, 4).Value = TextBox2.Value
    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: How do I prevent empty TextBoxes from updating the database

    Where exactly are you trying to put the data?

    Is it a row for each textbox?

    Also, why 20 loops?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: How do I prevent empty TextBoxes from updating the database

    The data goes into a sheet which functions as a database (table). Other sheets create reports from that database, based on whatever criteria you select in those reports.

    yes, in the database it is a row for each text box.

    The 20 loops is the only way I knew how to do this. There are 20 labels, 20 textboxes with unique values (hours). I suspect there is a more elegant way of doing this?
    Did I mention I am a beginner with VBA?

    FYI: This UserForm is like a timesheet, or resource log, where users enter hours for any of the (up to) 20 types of work, or tasks, for any given day. There is one more combobox on this form, to select the Name of the user (which is populated from yet another sheet, "Employees", which is a database populated by data entered into an Administrator UserForm)

  4. #4
    Registered User
    Join Date
    07-25-2005
    Location
    Leeds, UK
    MS-Off Ver
    2010
    Posts
    12

    Re: How do I prevent empty TextBoxes from updating the database

    Could you not use an IF statement to test if the TextBox is empty...

    Please Login or Register  to view this content.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: How do I prevent empty TextBoxes from updating the database

    I think you only really need one loop.

    That would be to go through the textboxes, checking if they contain anything and if they do put the data on the worksheet.

    Before the loop you could have code to find the next blank row.

    Something like this perhaps, which assumes the textboxes are named Textbox1, Textbox2,...,Textbox20 and the labels are named Label1, Label2,...,Label20.
    Please Login or Register  to view this content.
    Last edited by Norie; 01-03-2013 at 03:47 AM.

  6. #6
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: How do I prevent empty TextBoxes from updating the database

    May I say to all of you who have responded (to any of my questions) a heartfelt Thank You! This user group is a life saver!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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