Solved

Retrive a random record from database

Posted on 2004-10-06
8
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

690 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