473,394 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Check if user enter a value in a text box when click on save button

Hi All,

Thanks you in advance for your help. I am working on a form that has text box for Budget information, combo box for location and list box for population which is related to locations combo items.

I would like to make sure that all information has been selected before clicking the save record button. If not, prompt users to make the proper selection based on the information provided.

When I check if the combo box item has been selected, everything works fine as well as for the list box. The only issue is checking on the text box whether data has been entered or not. Below is my code to check this one out. Please let me know what the best solution to this problem is.

Private Sub cmdSaveDWData_Click()

If Me.cboLocationID.ListIndex = "-1" Then
MsgBox "A selection is requiered for location and Budget information", vbCritical, EnterLocation
Me.cboLocationID.SetFocus
Exit Sub
End If


If Len(Me.txtBudgetDW & "") = 0 Then
MsgBox "Enter Buget Information", vbInformation, BudgetInfoDW
Me.txtBudgetDW.SetFocus
Exit Sub
End If


If Me.lstTargetPopDW.ListIndex < 0 Then
MsgBox "A selection is required for Target Population", vbInformation, USLocation
Me.lstTargetPopDW.SetFocus
Exit Sub
End If

End sub
Mar 4 '11 #1

✓ answered by beacon

Hi Joe,

I think you would be better off using the IsNull() function when trying to determine if there's a value in the textbox. By using the Len() function, you are assuming that there is actually a value in there for the function to work with.

I'm not 100% on this, but I imagine that the Len() function requires SOMETHING to be there in order for it to return a value...even if the value is zero. You could check my theory by putting a msgbox in the code after you check the condition:
Expand|Select|Wrap|Line Numbers
  1. If Len(Me.txtBudgetDW & "") = 0 Then
  2.      'Check the length
  3.      Msgbox Len(Me.txtBudgetDW & "")
  4.      MsgBox "Enter Buget Information", vbInformation, BudgetInfoDW
  5.      Me.txtBudgetDW.SetFocus
  6.      Exit Sub
  7. End If
  8.  
Here's the code that I would personally use instead:
Expand|Select|Wrap|Line Numbers
  1. If IsNull(Me.txtBudgetDW) Then
  2.      MsgBox "Enter Buget Information", vbInformation, BudgetInfoDW
  3.      Me.txtBudgetDW.SetFocus
  4.      Exit Sub
  5. End If
  6.  
Hope this helps and good luck,
beacon

10 13868
beacon
579 512MB
Hi Joe,

I think you would be better off using the IsNull() function when trying to determine if there's a value in the textbox. By using the Len() function, you are assuming that there is actually a value in there for the function to work with.

I'm not 100% on this, but I imagine that the Len() function requires SOMETHING to be there in order for it to return a value...even if the value is zero. You could check my theory by putting a msgbox in the code after you check the condition:
Expand|Select|Wrap|Line Numbers
  1. If Len(Me.txtBudgetDW & "") = 0 Then
  2.      'Check the length
  3.      Msgbox Len(Me.txtBudgetDW & "")
  4.      MsgBox "Enter Buget Information", vbInformation, BudgetInfoDW
  5.      Me.txtBudgetDW.SetFocus
  6.      Exit Sub
  7. End If
  8.  
Here's the code that I would personally use instead:
Expand|Select|Wrap|Line Numbers
  1. If IsNull(Me.txtBudgetDW) Then
  2.      MsgBox "Enter Buget Information", vbInformation, BudgetInfoDW
  3.      Me.txtBudgetDW.SetFocus
  4.      Exit Sub
  5. End If
  6.  
Hope this helps and good luck,
beacon
Mar 4 '11 #2
TheSmileyCoder
2,322 Expert Mod 2GB
You should put validation in the form's before update event, as access will automatically update your record regardless of whether the save button was clicked or not, when you browse to a new record, close the form, close access.

So in your form's BeforeUpdate it could look like this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.  
  3.  'Check that required fields are filled out
  4.     Dim strErrMsg As String
  5.  
  6.     If IsNull(Me.cboLocationID) Then
  7.        strErrMsg= "A selection is requiered for location and Budget information" & vbNewline
  8.     End If
  9.  
  10.     If Me.txtBudgetDW & "" = "" Then
  11.        strErrMsg=strErrMsg & "Enter Budget Information" & vbnewline
  12.     End If
  13.  
  14.     If Me.lstTargetPopDW.ListIndex < 0 Then
  15.        strErrMsg=strErrMsg &  "A selection is required for Target Population" & vbnewline
  16.     End If
  17.  
  18.  
  19.     If strErrMsg & "" = "" Then
  20.         'No errors
  21.         Cancel = False
  22.     Else
  23.         'Errors found
  24.         Cancel = True
  25.         strErrMsg = strProjectName & " could not save your record at this time for the following reasons:" & _
  26.                     vbNewLine & vbNewLine & _
  27.                     strErrMsg & _
  28.                     vbNewLine & vbNewLine & _
  29.                     "Please correct and try again"
  30.         MsgBox strErrMsg, vbOKOnly + vbInformation, strProjectName
  31.     End If
  32.  
  33. End Function
strProjectName is a constant I have defined in a module, so that If I should ever change the project name I only have to change it in 1 place.

Replace your current code with:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSaveDWData_Click()
  2. On error goto Err_Handler:
  3. Me.Dirty=false
  4.  
  5. exit Sub
  6. Err_Handler:
  7. If Err.Number=2455
  8.  'Form validation failed, informaiton is missing
  9.  'This error basicly says that the save was cancelled (as we wanted it to be)
  10.   Err.Clear
  11.   Exit Sub
  12. Else
  13.   Msgbox "Error [" & err.number & "] occured." & vbnewline & err.description
  14. End If
  15. End Sub
Setting Cancel=True basicly means that the code is cancelling the update of the form, if the validation fails. If you have questions please ask.
Mar 4 '11 #3
beacon
579 512MB
@Joe - Is your form bound or unbound? I got the impression that it was unbound since you are using a command button to invoke a save procedure. After seeing Smiley's post, I should have offered up a response similar to Smiley's.

The good thing is that you've got two sides of the coin with our responses. If your form is unbound and you are using DAO, ADO, or a SQL command as your save procedure to input data into your table, then you could use a command button to submit your data. This method wouldn't check the data until the user was ready to submit the data.

If your form is bound, then you can use Smiley's code in the BeforeUpdate event to check the data immediately once it's input by the user. You could also use this in an unbound form too...it's all a matter of preference whether or not you want to notify the user once he/she has entered all the data collectively or immediately after entering one piece of data in a single control.

@Smiley - Cheers to you for picking up my slack.
Mar 5 '11 #4
TheSmileyCoder
2,322 Expert Mod 2GB
@Beacon, please don't take my posting after you as a criticism of what you posted. The original post could very well indicate an unbound form.
Besides its merely a statement of how I personally do, and what I find best. I know that others prefer to do it differently.
Mar 6 '11 #5
beacon
579 512MB
@Smiley - No offense taken...I was glad that you covered that method.
Mar 7 '11 #6
Hi Guys,

I apologies for not replying to you earlier, I do appreciate your (TheSmileyCoder & Beacon) support on this request.

Indeed, I am using an unbound form that insert data items selected into a table, but the only issue I have is that my code won’t work for the text box when there is not data entered. In other words, when Users select data from the list box and not values for text box (Budget) the code execute anyways leaving the budget field with null value which is not what I am looking for. I am so confused by this; any help will be greatly appreciated.

Beacon,

I tried your suggestion and it still the code bypasses the validation check on the text box, so the table got data from list boxes, but not from the text box object.


Smiley,

Given the fact I am using an unbound form would beforeupdate event still the best option? Should I use on Current event for this exercise. Please help me!

Thanks again!

Joe
Mar 7 '11 #7
TheSmileyCoder
2,322 Expert Mod 2GB
The reason for using the Before_Update was based on using a bound form, becauses access automatically updates the record in certain cases. Since your using an unbound field that is no longer relevant.

Personally when I check a textbox on whether or not something has been entered I use:
Expand|Select|Wrap|Line Numbers
  1. If Me.txtBudgetDW & "" = "" Then
Mar 7 '11 #8
Hey Guys,

I found the solution. I didn’t realized that I had put the following code (me.txtBudgetDW = “ “)on the open event form. I guess that was the issues because once I remove it from that event then your code work perfectly well. Thank you both so much.

I’d like to give you both credits on this, but not sure if that is doable. What should I do in this case?

Joe
Mar 7 '11 #9
TheSmileyCoder
2,322 Expert Mod 2GB
The Best answer is not about credit. Its intended to give other readers a quick guide to the answer.

So choose the answer which you think best suits the original question whether mine or Beacons.
Mar 7 '11 #10
Great! Thanks again for your valuable help
Mar 7 '11 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: msnews.microsoft.com | last post by:
Hello! I'm Jim by asp How can show "abc" in textbox when click one botton?
5
by: Matt | last post by:
I want to write a generic clearForm function to clear the form when the user click CLEAR button. Here's my attempts, because I want to take care all html controls. I think I need to test if the...
2
by: Shyguy | last post by:
Is there any way to stop Access from highlighting all the text in a memo box? If I click again the text is no longer highlighted, but I would like to get it to not highlight at all. Thanks for...
2
by: ambersaria420 | last post by:
I need to prompt the user to enter ID. I can open up prompt fine using javascript code below. However I am not sure how to get value that user entered in user prompt window. I have to check this...
2
by: subba_reddy | last post by:
Hi, I have iFrame and command button(webser control) in my aspx page, I want to populate the iframe with someohter page when I click on command button . - SUBBA
0
by: ayneekeaw | last post by:
When I click the update button/link at the Gridview to update the data from drop down list into the SQL. The selected value of drop down list change to default value. How can I fix this problem
8
by: wish | last post by:
Dear all, I would to ask that why the user always faced the different page when user click the back button instead back to page2.php but it go to page5.php..I remember in asp, we use the...
5
by: prebeni | last post by:
Hi I'm new into vb.net - so please forgive me :-) It's in a windows form - in visual basic 2005. How do I make a small text be visual when the user move the mouse over a button? In html you can...
0
by: tilak p | last post by:
hiiiii i have a asp.net page which contains gridview after entering data to gridview and updating in gridview i have given a save button on pressing it the data in grid will be sent to save...
4
by: negus | last post by:
so basically i have a textbox and i need to be able to hit the enter key and have it click a button its for my web browser project im doing for a final and cant seem to figure it out i have the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.