Text boxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • margot
    New Member
    • Sep 2007
    • 48

    Text boxes

    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.txtFi rst.Value) Or IsNull(Me.txtSe cond.Value) Then
    MsgBox "Is Blank"
    Else
    MsgBox "Your name is" & Me.txtFirst.Val ue & "And your age is" & Me.txtSecond.Va lue

    End If


    End Sub
  • puppydogbuddy
    Recognized Expert Top Contributor
    • May 2007
    • 1923

    #2
    Originally posted by margot
    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.txtFi rst.Value) Or IsNull(Me.txtSe cond.Value) Then
    MsgBox "Is Blank"
    Else
    MsgBox "Your name is" & Me.txtFirst.Val ue & "And your age is" & Me.txtSecond.Va lue

    End If


    End Sub
    Margot,

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

    If IsNull(Me.txtFi rst) Or IsNull(Me.txtSe cond) 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

    Comment

    • margot
      New Member
      • Sep 2007
      • 48

      #3
      Originally posted by puppydogbuddy
      Margot,

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

      If IsNull(Me.txtFi rst) Or IsNull(Me.txtSe cond) 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.txtFi rst) Or IsNull(Me.txtSe cond) Then
      MsgBox "Is Blank"
      Else
      MsgBox "Your name is" & Me.txtFirst.Val ue & "And your age is" & Me.txtSecond.Va lue

      End If

      End Sub

      Comment

      • puppydogbuddy
        Recognized Expert Top Contributor
        • May 2007
        • 1923

        #4
        Originally posted by margot
        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.txtFi rst) Or IsNull(Me.txtSe cond) Then
        MsgBox "Is Blank"
        Else
        MsgBox "Your name is" & Me.txtFirst.Val ue & "And your age is" & Me.txtSecond.Va lue

        End If

        End Sub
        Margot,
        You probably need to use the second option because your field contains spaces, which is different from null.

        Comment

        • margot
          New Member
          • Sep 2007
          • 48

          #5
          Originally posted by puppydogbuddy
          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.

          Comment

          • puppydogbuddy
            Recognized Expert Top Contributor
            • May 2007
            • 1923

            #6
            Originally posted by margot
            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.

            Comment

            • margot
              New Member
              • Sep 2007
              • 48

              #7
              Originally posted by puppydogbuddy
              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.

              Comment

              • puppydogbuddy
                Recognized Expert Top Contributor
                • May 2007
                • 1923

                #8
                Originally posted by margot
                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.

                Comment

                • margot
                  New Member
                  • Sep 2007
                  • 48

                  #9
                  Originally posted by puppydogbuddy
                  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.Val ue & "And your age is" & Me.txtSecond.Va lue

                  End If


                  End Sub

                  Comment

                  • puppydogbuddy
                    Recognized Expert Top Contributor
                    • May 2007
                    • 1923

                    #10
                    Originally posted by margot
                    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.Val ue & "And your age is" & Me.txtSecond.Va lue

                    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.Va lue and hit enter. what is returned vs what was entered originally?

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

                    Comment

                    • margot
                      New Member
                      • Sep 2007
                      • 48

                      #11
                      Originally posted by puppydogbuddy
                      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.Va lue 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

                      Comment

                      • puppydogbuddy
                        Recognized Expert Top Contributor
                        • May 2007
                        • 1923

                        #12
                        Originally posted by margot
                        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.....

                        Comment

                        • margot
                          New Member
                          • Sep 2007
                          • 48

                          #13
                          Originally posted by puppydogbuddy
                          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?

                          Comment

                          • puppydogbuddy
                            Recognized Expert Top Contributor
                            • May 2007
                            • 1923

                            #14
                            Originally posted by margot
                            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?


                            _______________ _______________ _______________ _______________ ___
                            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.

                            Comment

                            • margot
                              New Member
                              • Sep 2007
                              • 48

                              #15
                              Originally posted by puppydogbuddy
                              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?


                              _______________ _______________ _______________ _______________ ___
                              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.

                              Comment

                              Working...