Results 1 to 8 of 8

Thread: read .caption from a button

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    124

    read .caption from a button

    I want my program to read from a label. so that when i click on a button it reads its caption and changes somthing in a modual.

    I have this modual

    VB Code:
    1. Public Type DataType
    2.     TodayMonth As String
    3.     TodayMonthNum As Integer
    4.     TodayDay As Integer
    5.     username As String
    6.     ChosenMonthNum As Integer
    7.     ChosenMonth As String
    8.     ChosenDay As Integer
    9.     TotalUsers As Integer
    10. End Type
    11. Public Data(0) As DataType

    and this is the code inside each button
    VB Code:
    1. Dim TodayDate As Integer
    2.     TodayDate = FriA.Caption
    3.     Data(0).TodayDay = TodayDate

    but when i run the program i get this error.

    Run Time Error "424":
    Object required

    So can anyone see a problem with my code, or is it that you can't read captions this way, and if thats the case how do i read captions?

  2. #2
    New Member
    Join Date
    Mar 2006
    Location
    Vidin
    Posts
    15

    Re: read .caption from a button

    As I see you want to read caption and save it to an Integer so you have to use Val() function for this ...

  3. #3
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526

    Re: read .caption from a button

    I don't know how you're getting an 'Object required' error because you have a Type Mismatch problem. When I pasted your code into a project with one button, one label and one module, I got a Type Mismatch error.

    The mismatch error occurs because the .Caption property of the Label control is a String and you are trying to assign it to TodayDate, which you have declared as an Integer.

    Try this following code, it worked fine for me [changes in bold]:

    VB Code:
    1. 'MODULE CODE:
    2.  
    3. Public Type DataType
    4.     TodayMonth As String
    5.     TodayMonthNum As Integer
    6.     TodayDay As [B]Date[/B]
    7.     username As String
    8.     ChosenMonthNum As Integer
    9.     ChosenMonth As String
    10.     ChosenDay As Integer
    11.     TotalUsers As Integer
    12. End Type
    13. Public Data(0) As DataType
    14.  
    15.  
    16. 'FORM CODE:
    17.  
    18. Private Sub Command1_Click()
    19.     Dim TodayDate As [B]Date[/B]
    20.     TodayDate = [B]CDate([/B]Label1.Caption[B])[/B]
    21.     Data(0).TodayDay = TodayDate
    22.     [B]MsgBox Data(0).TodayDay[/B]
    23. End Sub
    24.  
    25. Private Sub Form_Load()
    26.     [B]Label1.Caption = Date[/B]
    27. End Sub
    Do canibals not eat clowns because they taste funny?

  4. #4
    Fanatic Member space_monkey's Avatar
    Join Date
    Apr 2005
    Location
    神と歩くこと
    Posts
    573

    Re: read .caption from a button

    Maybe you could post some more of your code and the line that errors out if possible.
    Using VB6 or VB.net 2008 with .net 3.5
    "Life... death... either way I'll be confined to a small cubicle!" - Hermes Conrad

  5. #5
    Fanatic Member space_monkey's Avatar
    Join Date
    Apr 2005
    Location
    神と歩くこと
    Posts
    573

    Re: read .caption from a button

    @doofusboy why did you change everything to dates? He's trying to get an interger of some kinda from the caption on a button not a label. I'm not sure why he's doing that but he has some kind of plan. really all he needed to change so that he always gets an integer from a button is to do this TodayDate = CInt(Val(friA.caption)). You might be right but that's not the way i read what he's posted so far.
    Using VB6 or VB.net 2008 with .net 3.5
    "Life... death... either way I'll be confined to a small cubicle!" - Hermes Conrad

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    124

    Re: read .caption from a button

    space_monkey your right, it has to be an integer as it does indeed have a plan. I'll try your TodayDate = CInt(Val(friA.caption)) idea and get back to you.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    124

    Re: read .caption from a button

    Nope, I tried your idea space monkey (TodayDate = CInt(Val(friA.caption))) but i still get the same error.

    The line that errors out is this one:
    VB Code:
    1. [b]TodayDate = FriA.Caption[/b]

  8. #8
    Fanatic Member space_monkey's Avatar
    Join Date
    Apr 2005
    Location
    神と歩くこと
    Posts
    573

    Re: read .caption from a button

    What is the caption on the button at that moment? and when in the process of your program are you calling this function?
    Using VB6 or VB.net 2008 with .net 3.5
    "Life... death... either way I'll be confined to a small cubicle!" - Hermes Conrad

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