Results 1 to 5 of 5

Thread: option explicit, public?...not sure

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    Hi there,

    For some reason my program's not working too well. There's 2 command buttons, a label and 2 forms. When the command button is pressed, 'I want' & number & 'sweets' appears in the label.

    number is a random number from 1 to 9.

    Then when the other button is pressed, form2 loads, and the number string should appear in a label on that form. But it doesn't. I've tried having number as a string, integer, etc, but still no luck.

    Could you please take a look at my code? Thanks for any help.

    Dim number As Integer

    Private Sub Command1_Click()
    Randomize
    number = Int(Rnd * 9)
    Label1 = "I want " & number & " sweets."
    End Sub

    Private Sub Command2_Click()
    Form2.Show
    End Sub


    Private Sub Form_Load()
    Label1 = number
    End Sub

  2. #2
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    you have your number as dim which is private

    use

    Code:
    ' in form1 in place of dim
    Public number as Integer
    
    ' In form2
    ' Then to access on form2 say
    label.Caption = form1.number

  3. #3
    Guest
    I do not quite understand what you want. Are you trying to make number be the caption of a label1 on Form2? If so, then this should work.

    Code:
    Private Sub Command1_Click() 
        Randomize 
        number = Int(Rnd * 9) 
        Label1 = "I want " & number & " sweets." 
        Form2.Label1 = number
    End Sub

  4. #4
    Guest
    this sort of matches MSDN's code
    Code:
    Private Sub Command1_Click() 
        Randomize 
        number = Int((Rnd * 9) + 1) ' add the one since Rnd produces a random number higher than 0, but less than 1 
        Label1 = "I want " & number & " sweets." 
        Form2.Label1 = number
    End Sub

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    Smile learning all the time

    Thanks everybody,

    I think I'll use billrogers', though, because I actually have lots of integers in my program. I just sent a simplified version so it would be more simple to understand.

    I'll just replace dim number as integer, with public number as integer, and so on,

    thanks again!

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