startup query

Discussion in 'AutoCAD' started by Graeme, Feb 23, 2004.

  1. Graeme

    Graeme Guest

    Hi.

    This macro, when run aftter startup, opens a text file, populates a listbox,
    reads a random entry from the listbox and creates a message box.

    It works fine after startup, but at startup the messagebox is always the
    last line from the text file / listbox. Can someone please tell me why it
    does this and what, if anything, can be done to fix it.

    thanks

    Graeme

    Public Sub AcadStartup()

    Dim TextLine
    Dim j As Integer
    Dim randnum As Integer

    Open "c:\ACADmessages.txt" For Input As #1
    Do While Not EOF(1)
    Line Input #1, TextLine
    UserForm2.ListBox1.AddItem (TextLine)
    Loop
    Close #1
    j = UserForm2.ListBox1.ListCount
    randnum = Int((j * Rnd) + 1)
    MsgBox UserForm2.ListBox1.List(randum)
    End Sub
     
    Graeme, Feb 23, 2004
    #1
  2. Graeme

    Graeme Guest

    PS, it's not because of the spelling mistake on the second last line.
     
    Graeme, Feb 23, 2004
    #2
  3. Graeme

    Norman Yuan Guest

    See comments in-line

    Since ListBox's ListIndex is from 0 to ListCount-1, so this line of code
    shuld be:

    randnum=Int((j*Rnd) 'randnum will get value from 0 to j-1

    Do you get run-time error here: "randum" is undecleared variable here? Or it
    is just a typo?
    you could test to see whether you get "randnum" value correctly:

    MsgBox "Random Value=" & randnum & vbcr &
    UserForm2.ListBox1.List(randnum)
     
    Norman Yuan, Feb 23, 2004
    #3
  4. Also.... to keep it from running the same pattern of "random" numbers each
    time you need to add the following to your code:

    Private Sub UserForm_Click()

    Randomize Timer 'add this

    Dim TextLine
    ...
    ...

    James
     
    James Belshan, Feb 23, 2004
    #4
  5. Graeme

    Graeme Guest

    Thanks Norman,

    It was just a typo.
     
    Graeme, Feb 24, 2004
    #5
  6. Graeme

    Graeme Guest

    Thanks James, that fixed the problem.

    Graeme
     
    Graeme, Feb 24, 2004
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.