|
-
May 1st, 2011, 09:42 AM
#1
Thread Starter
New Member
Requre field based on combobox value selected
[Access 2010] Require field entry if certain combobox value selected
While googling I found an entry from this forum very similar to my question. However the answer did not exactly work. I have a combobox that has just two choices "open" or "closed", if closed is chosen I want to require a text box (date) to be entered.
The suggested code I used is:
Private Sub opcl_BeforeUpdate(Cancel As Integer)
If opcl = "closed" Then
MsgBox "Please enter date closed", vbOKOnly
compdate.SetFocus
Cancel = True
End If
End Sub
However, this produces the following error message:
Run-Time error 2108
You must save the field before you execute the GoToControl action, the GoTo mtehod, or the SetFocus method.
Thanks in advance!
PS newbie to prgramming in access
-
May 1st, 2011, 06:01 PM
#2
Hyperactive Member
Re: Requre field based on combobox value selected
For Starters you have the code placed in the "BeforeUpdate" Event, which means it will try to run the code after something is entered/selected, but before it is completely entered. You need to place the code in the "AfterUpdate" event.
Also, that code doesn't force anyone to enter the date, it just gives you a message box asking the person to enter it, then they have to click Ok, then it sets the focus to the textbox where the date needs to be entered.
-
May 1st, 2011, 06:16 PM
#3
Hyperactive Member
Re: Requre field based on combobox value selected
If you have buttons on your form to save the entry, you can put a proceedure for the button click that checks the combobox value (kind of like what you have now). If the value is closed Then it will check to see if your textbox for the date has a valid date in it...
This checks the value in your date textbox, and it compairs it to the date from 1 week ago, if it is within a week, if not it will give a message box
Code:
If opcl = "closed" Then
If compdate < dateadd("d",-7,date) then
MsgBox "Please enter a valid date for when the item was closed", vbOKOnly
compdate.SetFocus
ElseIf compdate > dateadd("d",-7,date) then
'your code for saving the record
End If
Else:
'your code for saving the record
End If
-
May 1st, 2011, 07:00 PM
#4
Thread Starter
New Member
Re: Requre field based on combobox value selected
THank No One...
No buttons on the form for saving just using the built in access save button. There would only be one date, the date the item was closed.
-
May 1st, 2011, 07:28 PM
#5
Hyperactive Member
Re: Requre field based on combobox value selected
What do you mean by the built in save button? How do you move to the next record, do you have any buttons on your form for navigating through records?
Last edited by nO_OnE; May 1st, 2011 at 11:40 PM.
-
May 2nd, 2011, 06:24 AM
#6
Thread Starter
New Member
Re: Requre field based on combobox value selected
In the main menu at the top Home tab > Records > Save
As for navigating to the next record, again, the built-in navigation at the bottom of each form.
Thanks,
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|