?
Solved

Retrive a random record from database

Posted on 2004-10-06
8
Medium Priority
?
347 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1500 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

801 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