# Solved: Required Field in Access Form



## Scooter1959 (Sep 28, 2009)

Hello All,

I am fairly new to Access and can really use some help here. I have a form with a field "PO Number" that needs to be a required field before my user can use the print button.

All the other ideas I have seen seem to be very complicated to me as I do not do VBA and I am using a button with a macro that saves the form and prints the report. The form is a Sales Ticket.

All I want is to require the field "PO Number" to be required before it will save or print the report from the form.

Thanks in advance for any and all assistance.

Scooter1959


----------



## zhouma (Aug 28, 2009)

Hi Scooter,

Is your form connected to a table? I think the easiest way to make the PO field required would be to make it a required field in your table (by going into the design view of the table) and that will transfer over to the form you're working with.

Best,
Zhouma


----------



## Scooter1959 (Sep 28, 2009)

Hello Zhouma,

That caused me to have errors in other macros on the page. I do have VBA assigning a new ticket number to each record as well.

I think I need VBA code that will allow me to do this, but I am not sure. I do not need to validate it as we don't care what they put in that field, but they must put something in it, before the form can save or print.

Thanks,


----------



## zhouma (Aug 28, 2009)

Ok...let's try out my favorite VBA code to date and see if this helps. If not, OBP will come to the rescue :-D.

Private Sub btnSave_Click()
On Error GoTo Err_btnSave_Click

If Not IsNull(Me.POBox) Then
DoCmd.Close acForm, "Name of Form"

Else

MsgBox "Please fill in the PO Box."

End If

Err_btnSave_Click:
MsgBox Err.Description
Resume Exit_btnSave_Click

End Sub

I wrote the code as if you are using a Save button to check and see if the user has filled in the PO Box field or not. If the PO Box field is filled in then, the form will close. If it is not filled in, then a msg box will come up asking the user to fill in the PO Box field. If the code works you can also use the same code for a print button also. I'm sure there are other ways to do it. The parts in orange I think are optional but I have been learning VBA using it...so it's in all of my code 

Hope this helps.

Best,
Zhouma


----------



## OBP (Mar 8, 2005)

Always good to have the Error trapping (orange text) in the code, just in case there is an unexpected error.


----------



## Scooter1959 (Sep 28, 2009)

Hello,

First, thank you both for your replies very much.

At this time, I use a button with a macro that first saves the record and then prints the report from the form ( SalesTicket), it then starts a new record.

What I am looking for is a way to first verify that anything, just something in the field, has been put into the field, PO Number, before it does the macro events. 

I don't know if I can use the above under those circumstances and make it work.

Keep in mind, I am new to Access and know no VBA. You guys are dealing with a very grateful noob here....lol....

Thank you again.

Scooter1959


----------



## zhouma (Aug 28, 2009)

Ok. The best way I can think to do this is to run your macro using VBA. So let's try the following on for size:

Private Sub btnSave_Click()
On Error GoTo Err_btnSave_Click

If Not IsNull(Me.POBox) Then
DoCmd.RunMacro "[MacroName]"

Else

MsgBox "Please fill in the PO Number."

End If

Err_btnSave_Click:
MsgBox Err.Description
Resume Exit_btnSave_Click

End Sub

The above will work if you have your Macro saved as a name whether Macro 5, SavPrint or whatever else. So the above VBA code will check that something is filled in the PO Number field. If the field is filled in, then VBA will run your macro. If nothing is filled in the PO Number field, a message box will pop up asking the user to fill in the PO Number field.

Btw, which version of Access are you using? 2003? 2007? It makes a bit of difference when it comes to whether Access will prompt you to name the macros...

Best,
Zhouma


----------



## Scooter1959 (Sep 28, 2009)

Zhouma,

Thank you. I am using 2007. I will try this and let you know.

Thanks again,

Scooter1959


----------



## zhouma (Aug 28, 2009)

Ok...well with 2007 a lot of times the Macros are embedded, which for the purpose of this means they aren't named. You can name an embedded macro by going into the property sheet and editing the macro tho.

Either way let me know how ya make out.

Best,
Zhouma


----------



## Scooter1959 (Sep 28, 2009)

zhouma,

Sorry took so long to reply. Lost a friend this week and it is making deadlines difficult.

I tried your code above, but it is still giving me issues.










This is a screen shot of the macro I am currently using. I thought this would help you help me.

I need a way to do all of this in the macro, but make sure that the end user has put ANYTHING into the field "PO_Number" before it does the stuff in this macro.

Do I need a new function to call in this macro? If I add a sub, then where do I place it on the form? Before Focus, etc....

Thank you so very much for helping this grateful noob.

Scooter1959


----------



## zhouma (Aug 28, 2009)

Hi Scooter,

Sorry to hear about your loss. My condolences.

Thanks so much for the screen shot. It does help me.

Ok...so going w/ the premise that you want the code to happen when the user "thinks" they've finished a record and clicks on the print button...I say use the following code.

Private Sub cmdPrintReport_Click()

If Not IsNull(Me.PO_Number) Then

DoCmd.RunMacro "[MacroName]"

Else

MsgBox "Please fill in the PO Number."

End If

End Sub

Yes it is the same code that I gave you previously but I took out the error catching part b/c it was giving me a few errors. Either way, I've attached a demo db which I've tested it to make sure the code works.

Also take a look at the attached screen shot. I changed around your macro a little bit.

I don't think you need the OnError part of your macro, especially if you just want the macro to just save and print and then run your New ticket() code. So I removed the first line of your macro and started w/ the RunCommand Save record. Also make sure to fill in the Report Name and Object Type arguments for the OpenReport and GoToRecord actions.

If you need help making a stand alone macro like I did...do the following. Click on the create tab and click on Macro button in the ribbon. Re-create the macro in the attached screen shot...also add in your RunCode NewTicket() after the GoToRecord action and then save it. At that point, Access will prompt you to name it (the macro) and that is the name that you should use in the above code for the MacroName.

Either way, take a look at the attached screenshot and demo db and let me know if this helps get you on the right track.

Best,
Zhouma


----------



## Scooter1959 (Sep 28, 2009)

Zhouma,

Thank you so much. I will try this in the morning and get back to you then.

I am getting close to my deadline, so I might even need to see about getting you to do some code for me.

Please send me some contact info to my email, [email protected] if you are interested.

Thanks again,

Scooter1959


----------



## Scooter1959 (Sep 28, 2009)

Hello,

I tried to use the code and new macro as Zhouma was so gracious to supply above. When I do use it, I get a new error when I try to compile for error checking.

The attached file is a screen shot of the error. 

I really could use some more help. I am at the last minute and this is the last issue I am having....

Thanks again for any and all assistance.

Scooter1959


----------



## Scooter1959 (Sep 28, 2009)

Hello Zhouma,

My error, I had renamed the field in the form to tell the difference between it and the table while I was trying to figure it out. Once I figured that out, everything worked great.

Thank you ever so much for your assistance.

Scooter1959


----------



## zhouma (Aug 28, 2009)

You're very welcome, Scooter. Glad I was able to help.

Best,
Zhouma


----------

