Using If statement between 2 text boxes in VB6

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wernerh
    New Member
    • Jul 2007
    • 104

    Using If statement between 2 text boxes in VB6

    Please could anyone tell me what is wrong with this code.

    Do I need anything else other than this for it to work, or is my code missing further info requirements. Presently getting no result in Ret1.text5

    If Ret1.text4 = 100 Then Ret1.Text5 = Wrong

    Thank you
    Werner
  • Martin2007
    New Member
    • Jul 2007
    • 21

    #2
    Hi, if you are trying to check that the text box contains the string 100, u need the quotation marks...
    i.e. if textbox.Text = "100".

    If you are trying to check if it is a number you will need to parse the string to a number. (in this case integer)

    i.e. Integer.Parse(T extBox.Text)

    note.. this is for VB.net

    Also you would need stirng notation around te text you are trying to enter

    Comment

    • hariharanmca
      Top Contributor
      • Dec 2006
      • 1977

      #3
      Originally posted by Wernerh
      Please could anyone tell me what is wrong with this code.

      Do I need anything else other than this for it to work, or is my code missing further info requirements. Presently getting no result in Ret1.text5

      If Ret1.text4 = 100 Then Ret1.Text5 = Wrong

      Thank you
      Werner

      You never mentioned, what is your front-end and what it means for Ret1?

      Code:
      If Val(txtName1.Text) = 100 Then txtName2.Text = valSomeValue
      This will be correct.

      Comment

      • Wernerh
        New Member
        • Jul 2007
        • 104

        #4
        Originally posted by hariharanmca
        You never mentioned, what is your front-end and what it means for Ret1?

        Code:
        If Val(txtName1.Text) = 100 Then txtName2.Text = valSomeValue
        This will be correct.
        Hi, thanks it was just the "" that was omitted. It works perfectly now.

        Thanks for the help.

        Werner

        Comment

        • Wernerh
          New Member
          • Jul 2007
          • 104

          #5
          One more question if you could maybe have a look for me. I have a excel book that is opened by a command button running this script. It gives me object error. All I want it to do it open the book (which it does no problem) and then run the if statement. Gives me the error when it gets to that part. This is VB6

          Private Sub Command3_Click( )

          Set xlBook = GetObject("c:\m iprice software\data\r etail.xls")
          ' Display Microsoft Excel and the Worksheet
          ' window.
          xlBook.Applicat ion.Visible = True
          xlBook.Windows( 1).Visible = True


          If textbox4.Text < " 100" Then

          'Copy text fields Part 1
          Retail.Range("g 5") = textbox4.Text

          Else

          'Copy text fields Part 2
          Retail.Range("g 5") = textbox5.Text

          End If

          End Sub

          Thanks once again.

          Comment

          • hariharanmca
            Top Contributor
            • Dec 2006
            • 1977

            #6
            Originally posted by Wernerh
            One more question if you could maybe have a look for me. I have a excel book that is opened by a command button running this script. It gives me object error. All I want it to do it open the book (which it does no problem) and then run the if statement. Gives me the error when it gets to that part. This is VB6

            Private Sub Command3_Click( )

            Set xlBook = GetObject("c:\m iprice software\data\r etail.xls")
            ' Display Microsoft Excel and the Worksheet
            ' window.
            xlBook.Applicat ion.Visible = True
            xlBook.Windows( 1).Visible = True


            If textbox4.Text < " 100" Then

            'Copy text fields Part 1
            Retail.Range("g 5") = textbox4.Text

            Else

            'Copy text fields Part 2
            Retail.Range("g 5") = textbox5.Text

            End If

            End Sub

            Thanks once again.

            If textbox4.Text < " 100" Then

            are you sure that should be " <Space>100".
            okay, If you go for that
            then use

            If Trim(textbox4.T ext) = "100" Then

            what is the error that you are getting?
            Last edited by hariharanmca; Jul 26 '07, 10:49 AM. Reason: If Trim(textbox4.Text) < "100" Then Changed < to =

            Comment

            • Martin2007
              New Member
              • Jul 2007
              • 21

              #7
              Hey, I'm not too familiar with VB6, but i think that the statement
              Code:
               If textbox4.Text < " 100" Then
              is the problem...

              You are asking if a string is less than another string... which if you think about it, doesnt really make sense.

              If you are trying to find if the number entered into the textbox4 is less than 400, then you need to parse the text as an integer or a double... in vb.net this would be

              Code:
               If Integer.Parse(textbox4.text) < 100 Then
              That will check if the number entered into the text box is less than 100... is this what you wanted?

              Comment

              • Wernerh
                New Member
                • Jul 2007
                • 104

                #8
                Originally posted by Martin2007
                Hey, I'm not too familiar with VB6, but i think that the statement
                Code:
                 If textbox4.Text < " 100" Then
                is the problem...

                You are asking if a string is less than another string... which if you think about it, doesnt really make sense.

                If you are trying to find if the number entered into the textbox4 is less than 400, then you need to parse the text as an integer or a double... in vb.net this would be

                Code:
                 If Integer.Parse(textbox4.text) < 100 Then
                That will check if the number entered into the text box is less than 100... is this what you wanted?
                Yes, I want it to read the value ot that textbox and if it is < than 100 to follow the rest of the statement. The bottom line is i am trying to get that result posted in a excel sheet. The error I am getting is "run time error" object required.

                Comment

                • Wernerh
                  New Member
                  • Jul 2007
                  • 104

                  #9
                  Originally posted by Wernerh
                  Yes, I want it to read the value ot that textbox and if it is < than 100 to follow the rest of the statement. The bottom line is i am trying to get that result posted in a excel sheet. The error I am getting is "run time error" object required.
                  I actually don't need to use the if statement as all i am trying to do is to get the textbox4 posted in excel. I can then in excel write a if statement which is easy.

                  Comment

                  • hariharanmca
                    Top Contributor
                    • Dec 2006
                    • 1977

                    #10
                    Originally posted by Wernerh
                    I actually don't need to use the if statement as all i am trying to do is to get the textbox4 posted in excel. I can then in excel write a if statement which is easy.

                    It is difficult to get only in some piece of code. And you have to explain more.

                    Comment

                    • Wernerh
                      New Member
                      • Jul 2007
                      • 104

                      #11
                      Originally posted by hariharanmca
                      It is difficult to get only in some piece of code. And you have to explain more.
                      Ok, will try.


                      The function requirement is simple, but the actual coding is what is the problem. The first part opens a specific Xls workbook - that runs no problem. The second part is trying to enter a figure form a textbox into the workbook. As I said I don't need to use specifically the if code if there is another wa, this just seemd the easiest way. Hope that makes more sense. Sorry for the hassle.

                      Here is the whole code:

                      Private Sub Command3_Click( )

                      Set xlBook = GetObject("c:\m iprice software\data\r etail.xls")
                      ' Display Microsoft Excel and the Worksheet
                      ' window.
                      xlBook.Applicat ion.Visible = True
                      xlBook.Windows( 1).Visible = True


                      If textbox4.Text < " 100" Then

                      'Copy text fields Part 1
                      Retail.Range("g 5") = textbox4.Text

                      Else

                      'Copy text fields Part 2
                      Retail.Range("g 5") = textbox4.Text

                      End If

                      End Sub

                      Comment

                      • Wernerh
                        New Member
                        • Jul 2007
                        • 104

                        #12
                        Originally posted by Wernerh
                        Ok, will try.


                        The function requirement is simple, but the actual coding is what is the problem. The first part opens a specific Xls workbook - that runs no problem. The second part is trying to enter a figure form a textbox into the workbook. As I said I don't need to use specifically the if code if there is another wa, this just seemd the easiest way. Hope that makes more sense. Sorry for the hassle.

                        Here is the whole code:

                        Private Sub Command3_Click( )

                        Set xlBook = GetObject("c:\m iprice software\data\r etail.xls")
                        ' Display Microsoft Excel and the Worksheet
                        ' window.
                        xlBook.Applicat ion.Visible = True
                        xlBook.Windows( 1).Visible = True


                        If textbox4.Text < " 100" Then

                        'Copy text fields Part 1
                        Retail.Range("g 5") = textbox4.Text

                        Else

                        'Copy text fields Part 2
                        Retail.Range("g 5") = textbox4.Text

                        End If

                        End Sub
                        When I debug it stops at
                        If textbox4.Text < " 100" Then

                        and then says : Object required.

                        Comment

                        • Wernerh
                          New Member
                          • Jul 2007
                          • 104

                          #13
                          Originally posted by Wernerh
                          When I debug it stops at
                          If textbox4.Text < " 100" Then

                          and then says : Object required.
                          do you know of a simple code that will take the value of textbox4 and post it into excel cell g5?

                          Comment

                          • hariharanmca
                            Top Contributor
                            • Dec 2006
                            • 1977

                            #14
                            Originally posted by Wernerh
                            Ok, will try.


                            If textbox4.Text < " 100" Then

                            End If

                            End Sub

                            still cannot get this
                            If textbox4.Text < " 100" Then

                            why not this
                            If val(textbox4.Te xt) < 100 Then

                            Comment

                            • Wernerh
                              New Member
                              • Jul 2007
                              • 104

                              #15
                              Originally posted by hariharanmca
                              still cannot get this
                              If textbox4.Text < " 100" Then

                              why not this
                              If val(textbox4.Te xt) < 100 Then
                              I have used the code like you have suggested, but still same object required error. :-<<

                              Comment

                              Working...