Link to home
Start Free TrialLog in
Avatar of Aetia
Aetia

asked on

Retrive a random record from database

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?
Avatar of DotNetLover_Baan
DotNetLover_Baan

r u setting up the connection perfectly ? Why you are using FOR loop if you want to display only one record ?
-Baan
Avatar of Aetia

ASKER

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'.
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
Avatar of Aetia

ASKER

if I click on "Button2" it would show me random record from column "UserAgent" in "TextBox1".
or
SELECT UserAgent FROM UserAgents
ASKER CERTIFIED SOLUTION
Avatar of DotNetLover_Baan
DotNetLover_Baan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aetia

ASKER

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"?
Avatar of Aetia

ASKER

OK! I got it almost working myselft.