Must fill in textbox

AthenaBS

New member
Local time
Today, 08:05
Joined
Dec 10, 2005
Messages
5
Hi

I have created a form with one unbound textbox and a command button.

How do I make sure that the query only runs if text is entered in the textbox?
I do not want the command button to run unless the textbox has data to query.

At the moment the Query runs and displays all records.
 
Hi
Try
Code:
 If IsNull(Me.textbox) or Me.textbox = "" Then
MsgBox "You must complete textbox.", vbOkOnly, "Data required"
Me.textbox.SetFocus
Exit Sub
End If
That works for me.
Good luck
 
Sounds good, but I'm quite new to Access, where do I enter this code?
Under the Form properties, Textbox or Command Button.

Then what event should I use?
My Text box is called: Text0
Button is called: Command0
 
Hi
If you go into form design and then hit the properties button. Click on the command button and select the event tab. You should see an event in the OnClick property of the button. Open this up (clic on the little box with three dots) and you will get the code.
Insert the code after any Dim statements but before the OpenQuery statement. This way it will check that the textbox is filled before it looks at the query. If the box isn't completed it will exit the code and get you to fill the box.
The code you would want becomes
Code:
 If IsNull(Me.Text0) or Me.Text0 = "" Then
MsgBox "You must complete textbox.", vbOkOnly, "Data required"
Me.Text0.SetFocus
Exit Sub
End If

As a later development you can start refining the code so that if entries are not of a certain sort then you reject but perhaps best to go one step at a time.
Good luck
 

Users who are viewing this thread

Back
Top Bottom