Solved

Retrive a random record from database

Posted on 2004-10-06
8
341 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
 

Author Comment

by:Aetia
ID: 12239495
if I click on "Button2" it would show me random record from column "UserAgent" in "TextBox1".
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

18 Experts available now in Live!

Get 1:1 Help Now