Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making a field required in form but not table 4

Status
Not open for further replies.

JSD

Technical User
Jan 18, 2002
189
0
0
US
Hello,

I was wondering if there is a way to set a field to be required in the form with VB, rather than setting it in the table? Any ideas are very much appreciated.

Thanks

Jeremy
 
Jeremy,

You could set the validation rule in the form design so the field could not be null.

 
But them, to what purpose? It the information is not saved anywhere, requiring it on the form SEEMS pointless.





MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Hello,

Thanks for the help guys. I tried putting >0 in the Validation Rule, but my field is text not number. I'm having trouble finding help on putting validation for text. I tried <>&quot; &quot; And Is Not Null, but it still passes by the field allowing it to be empty. Any further suggestions?

Thanks

Jeremy
 
MichaelRed,

I appreciate you opinion, but I do need this to work because I'm using the underlying table for two different forms. One form enables the addition of job production records for employees from an assembly plant, while the other form does the same but applies to employees in the manufacturing plant. Understand that employees in the assembly plant only work on one job at a time, while employees in the manufacturing plant may be working on multiple jobs and set-ups. Everyone scans their employeeno, jobno, etc, but if a job in the manufacturing plant is in set-up it does not require a routestep scan, though in the assembly plant they will always have to scan the routestep. Rather than creating two different tables for each plant, I'd like to use one table but change the Validation in the forms so if assembly forgets to scan a route, an error msg occurs but if manufacturing is working on a set-up they will not get an error message if they don't scan the route step. I hope this clarifies. Any of your suggestions would be helpful.

P.S. - I didn't proofread this so sorry if you don't follow

Jeremy
 
for me, the 'propper' placement of this would be in the &quot;bewforeUpdate&quot; event. Of course the form -at that point- would need to include the &quot;plant&quot;, to determine which situation (assembly or manufacturing, I assume this is available, so the routine is just a simple conditional (If-Then-Else), with the criteria of not empty for the control. If the test fails, cancel the update, provide a popup message box and set focus back to hte control.




MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Sorry if this is totally way off (I don't have Access on my machine at work so can't verify...) but isn't there a property on the textbox itself called &quot;Required&quot; that you can just change to &quot;Yes&quot;?

Again, my apologies for taking up your time if this is incorrect.
 
Hello MichaelRed,

I have tried your method using a simple If---Then in the LostFocus event of the form (it would not work in the Before Update event). This works great except cursor does not go back to the stepno control.

Private Sub stepno_LostFocus()
If IsNull(Me!stepno) Then
MsgBox &quot;You must scan the route step&quot;
response = acDataErrContinue
DoCmd.GoToControl &quot;stepno&quot; <= this gets ignored
End If
End Sub

I guess VB doesn't like going to a control it hasn't left yet. Not much more in help about seting focus that worked (I tried Me!stepno.SetFocus). Would it be too much to ask for another push in the right direction?

Thanks,

Jeremy

 
could try &quot;is not null&quot; in validation rule
 
Jeremy,

It's been a while since I did something like this, and it's not very elegant (but it always worked for me) -

Try doing a .setfocus to another field in the form, then doing a .setfocus back to stepno

(if you had a control named tbx_1)
Me!tbx_1.SetFocus
Me!stepno.SetFocus

Coley
 
Thanks caltman sir,

Simple and effective - works perfectly.

Jeremy


 
If you place the code in the LostFocus event of the FORM, the the control name would need to be the fully qualified format (Form!form(&quot;formname&quot;).ControlName, or something similar (I often get the specifics of these constructions worng the first time or several!). A &quot;better location would be in the LostFocus event of the control, but then, you would need to be sure that the form would have the location before there was any poossability that this control could have the focus to begin with.

The &quot;syntax&quot; of your sub inidcates that &quot;stepnp&quot; is, however, the control name. Further, I would suggest that you simply use the SetFocus Method, rather than the DoCmd....

I copied your procedure, modified it to reflect a control on an existing form in one of my plethora of 'sample' databases, and it appears to work, as shown by the following (note the Debug.Print statements). Unfortunatly, the reality seems to NOT actuall move the cursor back to the control, which I'm not at all understanding. I KNOW I've done this a several multituds of times before w/o these problems.


Code:
Private Sub txtComment_LostFocus()

    Debug.Print &quot;Start: &quot; & Me.ActiveControl.Name

    If IsNull(Me(&quot;txtComment&quot;)) Then
        MsgBox &quot;You must scan the route step&quot;
        Me(&quot;txtComment&quot;).SetFocus
    End If

    Debug.Print &quot;End: &quot; & Me.ActiveControl.Name

End Sub

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Thanks all for the help

and helping me think

Jeremy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top