Solved

Retrive a random record from database

Posted on 2004-10-06
8
342 Views
Last Modified: 2010-05-18
Hey!

I'd like to retrive random record from my MS Access database. It would work like if I click on "Button2" it would show me random record from column "UserAgent" in "TextBox1".

Code I currently have is like this:

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            xConn = New sqlConn
            xConn.connectMe("SELECT * UserAgent FROM UserAgents ORDER BY RAND() LIMIT 1")

        For iCounter = 0 To xConn.getData.Count - 1
            TextBox1.Text = (xConn.dataReturned.Item(iCounter))
        Next
        xConn.OLEConn.Close()

    End Sub

It doesn't work... Anybody got ideas?
0
Comment
Question by:Aetia
  • 4
  • 3
8 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12239269
r u setting up the connection perfectly ? Why you are using FOR loop if you want to display only one record ?
-Baan
0
 

Author Comment

by:Aetia
ID: 12239392
I am newbie... :)

I put tgether all i've found. Something from here something from there...

It seems that it connects to database correctly... it gives me following error:

Syntax error (missing operator) in query expression '*UserAgent'.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12239441
What are you attempting to do with this:

SELECT * UserAgent FROM UserAgents

as Select * will return MULTIPLE columns (all of the columns in the record), and you cannot assign a Alias (UserAgent) to MULTIPLE values.

Try this:

SELECT * FROM UserAgents ...

AW
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Aetia
ID: 12239495
if I click on "Button2" it would show me random record from column "UserAgent" in "TextBox1".
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12239515
or
SELECT UserAgent FROM UserAgents
0
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 500 total points
ID: 12239653
Follow these steps:(ADO.Net approach)
-> Create and open an OleDbConnection with appropriate connection string. (www.connectionstrings.com)
     http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbconnectionclasstopic.asp
-> If the query u are using (but only select UserAgent field) is good, use that.
-> Create a OleDbCommand and use its execute scaler method to get the first record from the selected records.
     http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDataOleDbOleDbCommandClassTopic.asp
     http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDataOleDbOleDbCommandClassExecuteScalarTopic.asp
-> Display that record in the TextBox.

If you need help at any stage , let me know.
-Baan
0
 

Author Comment

by:Aetia
ID: 12239759
As far this should be right now

            xConn = New sqlConn
        xConn = New sqlConn
        xConn.connectMe("SELECT UserAgent FROM UserAgents")

What next? How can I display random result from database in "TextBox1"?
0
 

Author Comment

by:Aetia
ID: 12241626
OK! I got it almost working myselft.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now