Results 1 to 5 of 5

Thread: Display random returned string from Access dB

  1. #1

    Thread Starter
    Junior Member drizz's Avatar
    Join Date
    Jan 2004
    Location
    Redmond
    Posts
    19

    Display random returned string from Access dB

    I have an Access dB that I am trying to use to highlight our Products on our intranet. I have created a simple page that can display either the first string in my query or all the strings returned. I am trying to make it so that every time the page is reloaded, it cycles to a new product w/ picture and title and description.

    Do I need to create an array and use the randomize function in Vbscript? I am somewhat of a newbie so any help is appreciated. If what I have written doesn't make sense, just let me know and I'll try to clarify.

    Here is the code:

    <%

    Dim adoCon 'Database Connection Variable
    Dim strAdoConfig 'Holds the Database driver and the path and name of the database
    Dim strCon 'Holds the Database driver and the path and name of the database
    Dim strProgSql 'Holds the SQL Select query for the database

    'Create database connection
    'Create a connection object
    Set adoCon = Server.CreateObject("ADODB.Connection")

    'Connection String for Access 2000/2002
    strAdoConfig = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("./_db/programs.mdb")

    'Set an active connection to the Connection object
    adoCon.Open strAdoConfig

    %>

    <html>
    <head>
    <title>Test Programs</title>
    </head>
    <body>
    <%
    Dim rsPrograms 'Database recordset holding the items
    Dim intProgItems 'Loop counter for displaying the items

    'Create recorset object
    Set rsPrograms = Server.CreateObject("ADODB.Recordset")

    'Create Select Statement
    strProgSql = "SELECT Prog_Main.name, Prog_Main.desc, Prog_Main.url, Prog_Pics.pics FROM Prog_Main LEFT JOIN Prog_Pics ON Prog_Main.id = Prog_Pics.prog_id "

    'Query the database
    rsPrograms.Open strProgSql, adoCon

    'If there are no items to display then display a message saying so
    If rsPrograms.EOF Then Response.Write("<span class=""text"">Sorry, There are no Programs to display</span>")

    'Loop round to display each of the programs
    For intProgItems = 1 to 10

    'If there are no records then exit for loop
    If rsPrograms.EOF Then Exit For

    %>

    <table><tr><td>
    <% = rsPrograms("pics") %>
    <% = rsPrograms("name") %>
    <% = rsPrograms("desc") %>
    <% = rsPrograms("url") %>
    </td></tr></table><br>
    <%
    'Move to the next record in the recordset
    rsPrograms.MoveNext
    Next

    'Reset server objects
    rsPrograms.Close
    Set rsPrograms = Nothing
    Set strAdoConfig = Nothing
    Set adoCon = Nothing
    %>
    </body>
    </html>
    Drew

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Display random returned string from Access dB

    VB Code:
    1. <%
    2.  
    3. Dim adoCon              'Database Connection Variable
    4. Dim strAdoConfig        'Holds the Database driver and the path and name of the database
    5. Dim strCon              'Holds the Database driver and the path and name of the database
    6. Dim strProgSql          'Holds the SQL Select query for the database
    7.  
    8.  
    9.  
    10.  
    11. 'Create database connection
    12. 'Create a connection object
    13.   Set adoCon = Server.CreateObject("ADODB.Connection")
    14.  
    15. 'Connection String for Access 2000/2002
    16.   strAdoConfig = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("./_db/programs.mdb")
    17.  
    18. 'Set an active connection to the Connection object
    19.   adoCon.Open strAdoConfig
    20.  
    21. %>
    22.  
    23. <html>
    24. <head>
    25.   <title>Test Programs</title>
    26. </head>
    27. <body>
    28. <%
    29.   Dim rsPrograms        'Database recordset holding the items
    30.   Dim intProgItems  'Loop counter for displaying the items
    31.  
    32. 'Create recorset object
    33.   Set rsPrograms = Server.CreateObject("ADODB.Recordset")
    34.  
    35. 'Create Select Statement
    36.   strProgSql = "SELECT Prog_Main.name, Prog_Main.desc, Prog_Main.url, Prog_Pics.pics  FROM Prog_Main LEFT JOIN Prog_Pics ON Prog_Main.id = Prog_Pics.prog_id   "
    37.  
    38. 'Query the database
    39.   rsPrograms.Open strProgSql, adoCon
    40.  
    41.  
    42.  
    43. 'If there are no items to display then display a message saying so
    44.   If rsPrograms.EOF Then Response.Write("<span class=""text"">Sorry, There are no Programs to display</span>")
    45.  
    46.  
    47.  
    48. 'If there are no records then exit for loop
    49.   If rsPrograms.EOF Then Exit For
    50.  
    51.  
    52. Dim randomnumber
    53. Dim numberofprograms
    54.  
    55. numberofprograms = rsPrograms.RecordCount
    56. randomnumber =  Int((numberofprograms - 1 + 1) * Rnd + 1)
    57.  
    58. rsPrograms.Move randonnumber, 1
    59.  
    60. %>
    61.  
    62. <table><tr><td>
    63. <% = rsPrograms("pics") %>
    64. <% = rsPrograms("name") %>
    65. <% = rsPrograms("desc") %>
    66. <% = rsPrograms("url") %>
    67. </td></tr></table><br>
    68. <%
    69.  
    70. 'Reset server objects
    71.   rsPrograms.Close
    72.   Set rsPrograms = Nothing
    73.   Set strAdoConfig = Nothing
    74.   Set adoCon = Nothing
    75. %>
    76. </body>
    77. </html>

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    It's pretty inefficient, but I've modified your code

  4. #4

    Thread Starter
    Junior Member drizz's Avatar
    Join Date
    Jan 2004
    Location
    Redmond
    Posts
    19
    Mendhak, thank you!!! I just need somewhere to start. Can you suggest another way of doing this. I was wondering if there way though another select statement...

    Anyway, thanks for the quick response...
    Drew

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    First do a SELECT COUNT()... on the table.

    Get that number, and then perform the random number thing on that number, and *then* use it in the original SQL statement like:

    strSQL = "SELECT Prog_Main.name, Prog_Main.desc, Prog_Main.url, Prog_Pics.pics FROM Prog_Main LEFT JOIN Prog_Pics ON Prog_Main.id = Prog_Pics.prog_id WHERE Prog_Main.id = " & therandomnumber

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