Results 1 to 4 of 4

Thread: Random Selection, Access Database

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2001
    Location
    Coleraine, N. Ireland
    Posts
    13

    Wink

    Hi there,

    I'm currently using Data Control. I'm trying to open a screen showing random 'Tips of the Day'. And i don't know the code to select a Random recordsets in my Access database.
    Please Help!

    cheers
    Claire

  2. #2
    Addicted Member PsudoLogical's Avatar
    Join Date
    Dec 2000
    Location
    Upstate N.Y.
    Posts
    144
    If all your 'Tips...' are in the same recordset and you want pick a random one, try using a random number generator in conjuction with the Move method of the recordset attached to your datacontrol....ie.

    Datacontrol.Recordset.Move Int((100 - 1) * rnd + 1)

    Note: The number 100 is the upper limit of you random # range, the number 1 is the lower limit.

    Hope this works for you!

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Personally I'd hard-code the tips into an array ...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2001
    Location
    Coleraine, N. Ireland
    Posts
    13

    Wink Working, but first recordset never changes!

    I've got the random code to work but each time i execute it, it opens with the same Recordset!......Any suggestions on how i can get around this??

    And when i close the form and then go to view it again,....no Tips (Recordsets) are shown!! Any ideas?

    Private Sub Command1_Click()
    Dim Id As Integer
    Dim tip As String
    Id = Int(22 * Rnd) + 1

    Data1.Recordset.FindFirst "Tip_id = " & Id & ""



    tip = Data1.Recordset.Fields(1)

    Label1.Caption = tip
    End Sub

    Private Sub Command2_Click()
    Unload Me
    End Sub

    Private Sub Form_Initialize()
    Dim Id As Integer
    Dim tip As String
    Id = Int(22 * Rnd) + 1

    Data1.Recordset.FindFirst "Tip_id = " & Id & ""

    tip = Data1.Recordset.Fields(1)

    Label1.Caption = tip
    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