Results 1 to 5 of 5

Thread: what wrong for my code?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    what wrong for my code?

    Code:
    Private Sub Command1_Click()
    Dim degrees As Double
    Dim minutes As Double
    Dim seconds As Double
    Dim decimals As Double
        
    If Text1.Text <> "" Or Text2.Text <> "" Or Text3.Text <> "" Then
        degrees = Text1.Text
        minutes = Text2.Text / 60
        seconds = Text3.Text / 3600
        decimals = degrees + minutes + seconds
        Label6.Caption = decimals
    ElseIf test1.Text = "" And Text2.Text = "" And Text3.Text = "" Then
        MsgBox "Please key in your input value", vbCritical, "Degree Converter"
    End If
    End Sub
    if the textbox 1 ,2,3 is empty, i want it give a message.but with the code above, it came out an error "object required". Why?

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: what wrong for my code?

    In one statement you have Text1 in the Elseif you have test1.Text Which is correct? (my bet is Text1.Text and not test1.Text)
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: what wrong for my code?

    Two things. In your ElseIf you misspelled Text.

    Also, please do not use vbCritical.....it tends to scare the heck out of people and the fact that they have not entered anything is not critical. The application will not melt down. All you are trying to do is issue a friendly reminder that they need to input something.

    Use either vbInformation or vbExclamation.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: what wrong for my code?

    Your code will fail if 2 of the 3 textboxes (or even 1 of them) is blank. Try this.
    Code:
    Private Sub Command1_Click()
    Dim degrees As Double
    Dim minutes As Double
    Dim seconds As Double
    Dim decimals As Double
    
      If Trim$(Text1.Text) = "" Or Trim$(Text2.Text) = "" Or Trim$(Text3.Text) = "" Then
          MsgBox "Please key in your input values", vbInformation, "Degree Converter"
      Else
        degrees = CDbl(Text1.Text)
        minutes = CDbl(Text2.Text) / 60
        seconds = CDbl(Text3.Text) / 3600
        decimals = degrees + minutes + seconds
        Label6.Caption = decimals
      End If
    
    End Sub
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: what wrong for my code?

    The best way is to not let them be blank in the first place. Do this for each textbox. (If you had a control array you'd only have to do it once)

    vb Code:
    1. Private Sub Text1_Validate(Cancel As Boolean)
    2.  
    3.     If Trim$(Text1.Text) = "" Then
    4.         MsgBox "I'm empty"
    5.         Text1.SetFocus
    6.         Cancel = True
    7.     End If
    8.        
    9. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width