469,301 Members | 2,282 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,301 developers. It's quick & easy.

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 13248
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,321 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,321 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,321 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,321 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

Post your reply

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

Similar topics

2 posts views Thread by ambersaria420 | last post: by
2 posts views Thread by subba_reddy | last post: by
reply views Thread by tilak p | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.