Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random Record (A2k)

    I want to select a random image to show in a form, (pretty much like a screensaver)

    Does any one have any examples of this.

    I have my table "tblImage" which is populated with an autonumber "ID" field and a "ImagePath" field containing just a path to a folder.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Random Record (A2k)

    You can use the Rnd() function to generate a random number. If your images table has records numbered 1 ... 15, the expression Int(15 * Rnd + 1) will generate a random number between 1 and 15. You can get the corresponding path by using DLookup, and set the Picture property of the image control I presume you have created to that path:

    imgControl.Picture = DLookup("ImagePath", "tblImage", "ID=" & Int(Rnd * 15 + 1))

    If you wish, you can get the record count using DCount and use that instead of the constant (15 in my example).

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Random Record (A2k)

    Thanks again Hans, this should get me started.

  4. #4
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Random Record (A2k)

    Hans
    Could you clarify the DCount, as you mentioned, I would rather have a random of the table rather than a constant.
    I changed:

    [imgPicture].Picture = DLookup("picfile", "tblImage", "ID=" & Int(Rnd * 5 + 1))

    to:

    [imgPicture].Picture = DLookup("picfile", "tblImage", "ID=" & Int(Rnd * DCount + 1))

    But errors out.

    Using the constant does work.
    (sorry my field names are not as original post)

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Random Record (A2k)

    DCount, just like DLookup, is a domain function that needs arguments. Use DCount("*", "tblImage") to count the number of records in tblImage.

  6. #6
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Random Record (A2k)

    Thanks again.

    Not sure if this is what you meant, but it works:

    Private Sub Form_Timer()
    Dim CntRecord As Integer
    CntRecord = DCount("*", "tblImage")
    [imgPicture].Picture = DLookup("picfile", "tblImage", "ID=" & Int(Rnd * CntRecord + 1))
    End Sub

    <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  7. #7
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Random Record (A2k)

    Hans
    Just one thought.
    The images are now randomised as I required.
    With each image path there is also a comment field:

    [ID],[PicFile],[Comment]

    Al though the images change, the comment does not, ( am i confusing things here)

    The comment stays at the first comment shown. ??

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Random Record (A2k)

    You can't expect Access to update the comment by magic just because you changed the picture. You can expand the code to do that for you too. I don't know whether the comment is a label or a text box; I will assume that it is a text box named txtComment. In the following code, I put the random value in a variable, because it is used twice:

    Private Sub Form_Timer()
    Dim CntRecord As Integer
    Dim intRandom As Integer
    CntRecord = DCount("*", "tblImage")
    intRandom = Int(Rnd * CntRecord + 1)
    [imgPicture].Picture = DLookup("picfile", "tblImage", "ID=" & intRandom)
    [txtComment] = DLookup("Comment", "tblImage", "ID=" & intRandom)
    End Sub

    If the comment is a label, you need to set its Caption property.

    Note: an alternative would have been to open a recordset on tblImage and use the Rnd function to move to a random record in the recordset. But since you got it working with DLookup, we'll stick with that.

  9. #9
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Random Record (A2k)

    Hans
    I hadn't expected Access to change the comment, only to show, the corresponding comment for the currently viewed picture.
    As the code stands at the moment, it works to a point.

    If I have three pictures, for example only and add a comment to each:

    1.jpg "Comment =A"
    2.jpg "Comment = B"
    3.jpg "Comment = C"

    When the program runs, the corresponding comment matches the pic.

    2.jpg shows B
    3.jpg shows C etc

    When the program, or form is closed, the comment has changed to the last comment before closing.
    So with this, the comments are being over-written.

    I merely need to show the comment for the pic which is being viewed.

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Random Record (A2k)

    Apparently, your form is bound to tblImage. Since you want to set the picture to one chosen at random, the form should be unbound: the Record Source of the form must be empty, and the Control Source of the text box displaying the comment must be empty.

  11. #11
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Random Record (A2k)

    Thanks again.
    It works fine now.

  12. #12
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Random Record (A2k)

    <P ID="edit" class=small>(Edited by D Willett on 07-May-03 16:29. )</P>************************************************ *************************************
    I actually get Invalid use of Null Error 94 when this happens
    ************************************************** ***********************************

    Hans
    I've been testing this since yesterday and come up against a small problem.
    The Images show randomly but now and again, an Image will be shown in succession causing the Comment,Description, and ImageCreated fields to be blank, although still showing the picture.

    I tried adding a couple of lines:
    If intRandom = intRandom Then
    intRandom = intRandom +1

    This didn't have the desired effect.
    Can the code be modified to not show the same Image in succession ?

    Public Sub Form_Timer()
    Dim CntRecord As Integer
    Dim intRandom As Integer
    Dim imgCreated As Integer
    Dim imgDescription As Integer

    'Get table record count
    CntRecord = DCount("*", "tblImage")

    'Create random record
    intRandom = Int(Rnd * CntRecord + 1)
    imgCreated = Int(Rnd * CntRecord + 1)
    imgDescription = Int(Rnd * CntRecord + 1)

    'On Error Resume Next

    [imgPicture].Picture = DLookup("picfile", "tblImage", "ID=" & intRandom)
    [Comment] = DLookup("Comment", "tblImage", "ID=" & intRandom)
    [ImageCreated] = DLookup("imagecreated", "tblimage", "id=" & intRandom)
    [Description] = DLookup("Description", "tblimage", "id=" & intRandom)
    End Sub

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Random Record (A2k)

    Declare a variable intPreviousRandom of type integer. Then, something like this:

    intPreviousRandom = intRandom
    Do
    intRandom = Int(Rnd * CntRecord + 1)
    Loop Until intRandom <> intPreviousRandom

    PS What is the purpose of imgCreated and imgDescription? The are assigned a value, but not used (nor should they be).

  14. #14
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Random Record (A2k)

    Hans
    Perhaps this will help you to understand.

    The extra fields correspond to the picture viewed.
    I thought by declaring the fields as before, I could pick up that info also, later in the code to be shown in the form.
    The database points to images in a folder on C:

    C:Home

    (Sorry I've asked so much this week, it's the first chance I've had in ages)
    Attached Files Attached Files

  15. #15
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Random Record (A2k)

    Dave,

    My code mistakenly assumed that your image ID's were numbered 1, 2, .... But since ID is an AutoNumber field, there are gaps in the sequence where records have been deleted. Combined with the fact that you want to display several fields from tblImage, I now recommend making the form bound after all. The On Timer event now moves to a random record of the form. Here is the code behind the form:

    ' Module level variables
    Dim intRandom As Integer
    Dim intPreviousRandom As Integer
    Dim CntRecord As Integer
    Dim rst As Object ' Not as Recordset to avoid ADO/DAO confusion

    Private Sub Form_Close()
    ' Release object memory when form is closed
    Set rst = Nothing
    End Sub

    Private Sub Form_Current()
    ' Try to load picture when moving to different record
    On Error Resume Next
    [imgPicture].Picture = PicFile
    End Sub

    Private Sub Form_Load()
    'Get table record count
    CntRecord = DCount("*", "tblImage")
    ' Set object variable
    Set rst = Me.RecordsetClone
    End Sub

    Public Sub Form_Timer()
    ' Keep track of previous random value
    intPreviousRandom = intRandom
    ' Get random number until different from previous value
    Do
    intRandom = Int(Rnd * CntRecord + 1)
    Loop Until intRandom <> intPreviousRandom
    ' Move recordset to random record (offset from current one)
    rst.Move intRandom - Me.CurrentRecord
    ' Move form to same record
    Me.Bookmark = rst.Bookmark
    End Sub

    I have attached the modified version of the database.
    Attached Files Attached Files

Page 1 of 2 12 LastLast

Posting Permissions

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