By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

Text boxes

P: 48
Hi, I am using Access 2007 and I am trying to do something really simple I have a form with two text boxes and a button. When the user press the button if the box is blank a message will be display. If the user inputs his her name another message will be display with that info. I have the code below, however I do not understand why the code does not run. I am writing the code under the button. Please help. Thanksss

Option Compare Database
Option Explicit

Private Sub Command4_Click()
If IsNull(Me.txtFirst.Value) Or IsNull(Me.txtSecond.Value) Then
MsgBox "Is Blank"
Else
MsgBox "Your name is" & Me.txtFirst.Value & "And your age is" & Me.txtSecond.Value

End If


End Sub
Sep 13 '07 #1
Share this Question
Share on Google+
18 Replies


puppydogbuddy
Expert 100+
P: 1,923
Hi, I am using Access 2007 and I am trying to do something really simple I have a form with two text boxes and a button. When the user press the button if the box is blank a message will be display. If the user inputs his her name another message will be display with that info. I have the code below, however I do not understand why the code does not run. I am writing the code under the button. Please help. Thanksss

Option Compare Database
Option Explicit

Private Sub Command4_Click()
If IsNull(Me.txtFirst.Value) Or IsNull(Me.txtSecond.Value) Then
MsgBox "Is Blank"
Else
MsgBox "Your name is" & Me.txtFirst.Value & "And your age is" & Me.txtSecond.Value

End If


End Sub
Margot,

Nulls don't have a value, so test like this:

If IsNull(Me.txtFirst) Or IsNull(Me.txtSecond) Then


If you allow zero length strings (ZLS) in your textboxes (e.g hitting the spacebar) you need to check the textboxes for spaces as well as nulls...both appear as blank. You can test for both as follows:

If Nz(Me.txtFirst, "") = "" Or Nz(Me.txtSecond, "") = "" Then
Sep 14 '07 #2

P: 48
Margot,

Nulls don't have a value, so test like this:

If IsNull(Me.txtFirst) Or IsNull(Me.txtSecond) Then


If you allow zero length strings (ZLS) in your textboxes (e.g hitting the spacebar) you need to check the textboxes for spaces as well as nulls...both appear as blank. You can test for both as follows:

If Nz(Me.txtFirst, "") = "" Or Nz(Me.txtSecond, "") = "" Then
Thank you,

I tried the first option, the code below. However it does not seem to work. Don't know why?? Thanks

Option Compare Database
Option Explicit

Private Sub Command4_Click()
If IsNull(Me.txtFirst) Or IsNull(Me.txtSecond) Then
MsgBox "Is Blank"
Else
MsgBox "Your name is" & Me.txtFirst.Value & "And your age is" & Me.txtSecond.Value

End If

End Sub
Sep 17 '07 #3

puppydogbuddy
Expert 100+
P: 1,923
Thank you,

I tried the first option, the code below. However it does not seem to work. Don't know why?? Thanks

Option Compare Database
Option Explicit

Private Sub Command4_Click()
If IsNull(Me.txtFirst) Or IsNull(Me.txtSecond) Then
MsgBox "Is Blank"
Else
MsgBox "Your name is" & Me.txtFirst.Value & "And your age is" & Me.txtSecond.Value

End If

End Sub
Margot,
You probably need to use the second option because your field contains spaces, which is different from null.
Sep 17 '07 #4

P: 48
Margot,
You probably need to use the second option because your field contains spaces, which is different from null.
Thank you,

I tried the second and it does not work either.
Sep 17 '07 #5

puppydogbuddy
Expert 100+
P: 1,923
Thank you,

I tried the second and it does not work either.
Then your problem is probably the "Me" reference you used....is your textbox on the main form or is it on a subform? The references are different.
Sep 17 '07 #6

P: 48
Then your problem is probably the "Me" reference you used....is your textbox on the main form or is it on a subform? The references are different.
Thanks,

I believe they are on the main form.
Sep 17 '07 #7

puppydogbuddy
Expert 100+
P: 1,923
Thanks,

I believe they are on the main form.
Your references are ok for a main form. I guess you need to explain what you mean by "it's not working". The way your code is currently written, it should give you a message if any one of the textboxes is blank. If you only want the message when both textboxes are blank, then you would use "and" instead of "or".

If nothing is happening when you click the button, make sure:
1. you have compiled your code without errors.
2. the words "event procedure" appear for the button's cick event when you are looking at the event tab section of the button's property sheet in design view.
Sep 17 '07 #8

P: 48
Your references are ok for a main form. I guess you need to explain what you mean by "it's not working". The way your code is currently written, it should give you a message if any one of the textboxes is blank. If you only want the message when both textboxes are blank, then you would use "and" instead of "or".

If nothing is happening when you click the button, make sure:
1. you have compiled your code without errors.
2. the words "event procedure" appear for the button's cick event when you are looking at the event tab section of the button's property sheet in design view.
Thank you Sir,

When I say that it does not work I mean that when I press the button nothing happens, if I type on the text boxes and press the button I do not get any message either. Below is the code exactly how I have it. I checked under properties the Even tab and it does have the "event procedure." Also I do not have any code errors.

Option Compare Database
Option Explicit

Private Sub Command6_Click()

If Nz(Me.txtFirst, "") = "" Or Nz(Me.txtSecond, "") = "" Then
MsgBox "Is Blank"
Else
MsgBox "Your name is" & Me.txtFirst.Value & "And your age is" & Me.txtSecond.Value

End If


End Sub
Sep 17 '07 #9

puppydogbuddy
Expert 100+
P: 1,923
Thank you Sir,

When I say that it does not work I mean that when I press the button nothing happens, if I type on the text boxes and press the button I do not get any message either. Below is the code exactly how I have it. I checked under properties the Even tab and it does have the "event procedure." Also I do not have any code errors.

Option Compare Database
Option Explicit

Private Sub Command6_Click()

If Nz(Me.txtFirst, "") = "" Or Nz(Me.txtSecond, "") = "" Then
MsgBox "Is Blank"
Else
MsgBox "Your name is" & Me.txtFirst.Value & "And your age is" & Me.txtSecond.Value

End If


End Sub
try using the bang operator instead of a period as shown:
If Nz(Me!txtFirst, "") = "" Or Nz(Me!txtSecond, "") = "" Then

if that doesn't help, it's time to do some debugging. Insert the word "Stop" in your code as shown below, then:
1. run your code
2. when the program stops and gives you the code window, hover over the objects in your code, e.g. Me.txtFirst and see if it shows a value. If you can't see anything, hit the CTRL +G keys to invoke the immediate window. Type ?Me.txtFirst.Value and hit enter. what is returned vs what was entered originally?

If Nz(Me.txtFirst, "") = "" Or Nz(Me.txtSecond, "") = "" Then
Stop
Sep 17 '07 #10

P: 48
try using the bang operator instead of a period as shown:
If Nz(Me!txtFirst, "") = "" Or Nz(Me!txtSecond, "") = "" Then

if that doesn't help, it's time to do some debugging. Insert the word "Stop" in your code as shown below, then:
1. run your code
2. when the program stops and gives you the code window, hover over the objects in your code, e.g. Me.txtFirst and see if it shows a value. If you can't see anything, hit the CTRL +G keys to invoke the immediate window. Type ?Me.txtFirst.Value and hit enter. what is returned vs what was entered originally?

If Nz(Me.txtFirst, "") = "" Or Nz(Me.txtSecond, "") = "" Then
Stop
Thank you,

When I do the stop I get a Macro Window not the Code Window.

If Nz(Me.txtFirst, "") = "" Or Nz(Me.txtSecond, "") = "" Then
Stop
Sep 17 '07 #11

puppydogbuddy
Expert 100+
P: 1,923
Thank you,

When I do the stop I get a Macro Window not the Code Window.

If Nz(Me.txtFirst, "") = "" Or Nz(Me.txtSecond, "") = "" Then
Stop

No wonder it doesn't work! you have been using the macro window to create VBA code instead of using the VBA code window. Can you place your form in design view and create a new button?
1. click the wizard button off so that you can add a button to your form without using the wizard.
2, click the toolbar and select command button from the list of objects on the toolbar and paste it to your form.
3. Highlight the button, invoke the property sheet, go to the event tab, then go to the click event and on the right hand side of the property sheet, you will see a button with 3 dots (...). Click on the 3 dots and you will see a dialog box with choices on it.....choose the "code" builder.
4. The code builder will create the beginning and end statements of your button procedure for you. Now all you have to do is copy and paste the code I gave you above in that sub that was created. Compile your code.
5. Test your new button....it should be working now.
6. Delete the old button.....
Sep 17 '07 #12

P: 48
No wonder it doesn't work! you have been using the macro window to create VBA code instead of using the VBA code window. Can you place your form in design view and create a new button?
1. click the wizard button off so that you can add a button to your form without using the wizard.
2, click the toolbar and select command button from the list of objects on the toolbar and paste it to your form.
3. Highlight the button, invoke the property sheet, go to the event tab, then go to the click event and on the right hand side of the property sheet, you will see a button with 3 dots (...). Click on the 3 dots and you will see a dialog box with choices on it.....choose the "code" builder.
4. The code builder will create the beginning and end statements of your button procedure for you. Now all you have to do is copy and paste the code I gave you above in that sub that was created. Compile your code.
5. Test your new button....it should be working now.
6. Delete the old button.....
Thank you,

I did what you said, but I still will get the macros window. Even thought I have been always working on the Microsoft Visual Basic window. I am using Access2007 do you think it has to do with that?
Sep 18 '07 #13

puppydogbuddy
Expert 100+
P: 1,923
Thank you,

I did what you said, but I still will get the macros window. Even thought I have been always working on the Microsoft Visual Basic window. I am using Access2007 do you think it has to do with that?

Margot,

Although, the user interface and navigation are different in Access 2007, there are still separate windows for vb code and for macros....It is just a matter of knowing how to get to the vb code window. If you put your form in design view and hit the CTRL+G keys, you should get the Visual Basic Editor with the code behind your form.

The following link shows how to create a close "button" in ms access 2007.....and although you are not creating a close button, the process should be similar for your button....The vb code window is behind the form Here are the steps that were given for the close button. Is this similar to the way you created your button?

http://articles.techrepublic.com.com...1-5745809.html
__________________________________________________ _____________
First, you need to open the form in Design View, and make sure the Control Wizard button in the toolbox is selected. Then, follow these steps:

Click the Command Button tool, and then click where you want the button to appear in your form.
Select the Form Operations category and then click Close Form under Actions.
Select the default text or type your own, and click Next.
Enter a name for the command, such as CloseForm button, and then click Finish.
Sep 19 '07 #14

P: 48
Margot,

Although, the user interface and navigation are different in Access 2007, there are still separate windows for vb code and for macros....It is just a matter of knowing how to get to the vb code window. If you put your form in design view and hit the CTRL+G keys, you should get the Visual Basic Editor with the code behind your form.

The following link shows how to create a close "button" in ms access 2007.....and although you are not creating a close button, the process should be similar for your button....The vb code window is behind the form Here are the steps that were given for the close button. Is this similar to the way you created your button?

http://articles.techrepublic.com.com...1-5745809.html
__________________________________________________ _____________
First, you need to open the form in Design View, and make sure the Control Wizard button in the toolbox is selected. Then, follow these steps:

Click the Command Button tool, and then click where you want the button to appear in your form.
Select the Form Operations category and then click Close Form under Actions.
Select the default text or type your own, and click Next.
Enter a name for the command, such as CloseForm button, and then click Finish.
Thank you,

I did not create my button using the wizard option since I cannot write code on VB, when I click to see the code behind I will get the macro window with the Action and Argument columns.

However when I do CTRL+G I will get to the VB window with my code written for the Enter button. But when I type STOP on the first line of code I will get a window that only says Macros which is different to the window that I get when using the wizard option. Hope you can help. I am really confuse on why is not working.
Sep 21 '07 #15

P: 48
Thank you,

I did not create my button using the wizard option since I cannot write code on VB, when I click to see the code behind I will get the macro window with the Action and Argument columns.

However when I do CTRL+G I will get to the VB window with my code written for the Enter button. But when I type STOP on the first line of code I will get a window that only says Macros which is different to the window that I get when using the wizard option. Hope you can help. I am really confuse on why is not working.
Sir,

I did the same code on a new database and it works; however the exact same code does not work on the database that I am working on. I had tried before with more complex codes and did not work either that is why I tried something simple. So what do you think might be the problem?
Sep 21 '07 #16

puppydogbuddy
Expert 100+
P: 1,923
Sir,

I did the same code on a new database and it works; however the exact same code does not work on the database that I am working on. I had tried before with more complex codes and did not work either that is why I tried something simple. So what do you think might be the problem?
Margot,
It sounds like the DB (or maybe just the form) you are working on might be corrupted. You should proceed as if your file could be corrupted and progressively perform the usual procedures that are used in such cases, starting with a repair and compact of the database, then followup with a decompile if the repair and compact did not work........and so on as prescribed in the links below:

http://allenbrowne.com/ser-47.html

http://granite.ab.ca/access/corruption/symptoms.htm
Sep 21 '07 #17

P: 48
Margot,
It sounds like the DB (or maybe just the form) you are working on might be corrupted. You should proceed as if your file could be corrupted and progressively perform the usual procedures that are used in such cases, starting with a repair and compact of the database, then followup with a decompile if the repair and compact did not work........and so on as prescribed in the links below:

http://allenbrowne.com/ser-47.html

http://granite.ab.ca/access/corruption/symptoms.htm
Thank you for all your help.
Sep 24 '07 #18

P: 48
Thank you for all your help.
One last question, I opened another database and tried to do the same code and it worked. Then I started to build a new database i have some reports and forms, but when I try to include code it does not work, can it be posible that the whole program is corrupted? I don't quite understand.
Sep 24 '07 #19

Post your reply

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