Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > Sql Select query results in a datagrid


Reply
 
Thread Tools Display Modes
  #1  
Old 06-12-2003, 10:52 PM
Trivial Trivial is offline
Newcomer
 
Join Date: Jun 2003
Location: Brisbane, Australia
Posts: 11
Default Sql Select query results in a datagrid


I want my search results from my SQL query to appear in a datagrid, why is it that I can find no examples? I dont want to use a flexgrid because the rest of my program is in datagrids and it will look out of place.
The code below is what im using and my grid is still empty:

frmSearch
Public strCriteria As String 'read in from input box on different form
Public strField As String 'the column name selected by the user

Private Sub Form_Load()

Dim srs As Recordset
Dim ssql As String
Set dgSearch.DataSource = srs 'dgSearch is the grid
ssql = "SELECT * FROM Members WHERE " + Format(strField) + " = '" + Format(strCriteria) + "'"
Set srs = frmMainForm.db.OpenRecordset(ssql, dbOpenDynaset)
srs.FindFirst (strField + " = '" + strCriteria + "'")
If srs.NoMatch Then

MsgBox "There are no " + strField + "s that match " & "'" + strCriteria + "'", vbOKOnly, "No Match"

Else
MsgBox frmMainForm.rs(strField).Value, vbOKOnly, "Woohoo"

End If

End Sub

It works if the value is not in the database, but if it is it just states the first value in the database and not the one that was actually searched for.
I basically want it so that if there are 3 smiths they all appear in the grid and the user can determine which one they want based on the other columns ie. first name, address.

Any help would be great, I'm sure its simple but I'm tired.
Reply With Quote
  #2  
Old 06-12-2003, 11:36 PM
BostjanK's Avatar
BostjanK BostjanK is offline
Junior Contributor
 
Join Date: Feb 2003
Location: Ljubljana, Slovenia
Posts: 324
Default

First of all, why are you using Format(strField) for the field name in WHERE...?

Second, if more than one record matches your condition, you must loop throught them to retrieve them all:
Code:
If srs.EOF Then MsgBox "No records" Else Do While Not srs.EOF MsgBox srs("FirsName") & " was found" srs.MoveNext Loop End If
__________________
Slovenia - the green treasure of Europe
Reply With Quote
  #3  
Old 06-13-2003, 01:24 AM
joe77s joe77s is offline
Centurion
 
Join Date: Feb 2002
Location: Indonesia
Posts: 170
Default

try this....

Set srs = frmMainForm.db.OpenRecordset(ssql, dbOpenDynaset)
Set dgSearch.DataSource = srs 'dgSearch is the grid

set srs to dgSearch after srs open.....

use ADO...it's more easier for me

__________________
every day is a Gift, how great Thou art

www.corpussoft.com
Reply With Quote
  #4  
Old 06-13-2003, 01:45 AM
Trivial Trivial is offline
Newcomer
 
Join Date: Jun 2003
Location: Brisbane, Australia
Posts: 11
Default

Thanks for the help, didn't notice the Format(strField) in the WHERE statement, a friends idea of helping . The loop helped and now it displays the searched name, but unfortunately my datagrid is still empty.
I tried that Joe77s but no matter where I put that line (Set dgSearch.DataSource = srs) it doesnt like it, I get a runtime error, 'type mismatch'.
Now if I get get the grid to show the values I'll be very happy indeed.
Reply With Quote
  #5  
Old 06-13-2003, 02:02 AM
joe77s joe77s is offline
Centurion
 
Join Date: Feb 2002
Location: Indonesia
Posts: 170
Default

at which line you get this error ??

i try to edit your code.....



Public strCriteria As String 'read in from input box on different form
Public strField As String 'the column name selected by the user

Private Sub Form_Load()

Dim srs As Recordset
Dim ssql As String
ssql = "SELECT * FROM Members WHERE '" & strField & "' = '" & strCriteria & "' "
Set srs = frmMainForm.db.OpenRecordset(ssql, dbOpenDynaset)
Set dgSearch.DataSource = srs 'dgSearch is the grid
sssql = strField & " = " & strCriteria
srs.movelast
srs.FindFirst sssql
If srs.NoMatch Then

MsgBox "There are no " & strField & "s that match " & strCriteria , vbOKOnly, "No Match"

Else
MsgBox frmMainForm.rs(strField).Value, vbOKOnly, "Woohoo"

End If

End Sub
__________________
every day is a Gift, how great Thou art

www.corpussoft.com
Reply With Quote
  #6  
Old 06-13-2003, 02:25 AM
BostjanK's Avatar
BostjanK BostjanK is offline
Junior Contributor
 
Join Date: Feb 2003
Location: Ljubljana, Slovenia
Posts: 324
Default

Quote:
Originally Posted by joe77s
at which line you get this error ??

i try to edit your code.....



Public strCriteria As String 'read in from input box on different form
Public strField As String 'the column name selected by the user

Private Sub Form_Load()

Dim srs As Recordset
Dim ssql As String
ssql = "SELECT * FROM Members WHERE '" & strField & "' = '" & strCriteria & "' "
Set srs = frmMainForm.db.OpenRecordset(ssql, dbOpenDynaset)
Set dgSearch.DataSource = srs 'dgSearch is the grid
sssql = strField & " = " & strCriteria
srs.movelast
srs.FindFirst sssql
If srs.NoMatch Then

MsgBox "There are no " & strField & "s that match " & strCriteria , vbOKOnly, "No Match"

Else
MsgBox frmMainForm.rs(strField).Value, vbOKOnly, "Woohoo"

End If

End Sub


Code:
ssql = "SELECT * FROM Members WHERE '" & strField & "' = '" & strCriteria & "' "
is not a valid SQL statement - field names must not be enclosed in '

Try this
Code:
Dim conn As ADODB.Connection Set conn= New ADODB.Connection conn.Open "connection string to your database" Dim srs as ADODB.Recordset Set srs = New ADODB.Recordset sSQL = "SELECT * FROM Members WHERE " & strField & " = '" & strCriteria & "'" set srs = conn.Execute(sSQL) 'Then you have two options: '1 Do While Not rs.EOF ... Loop '2 (as joe77s suggested) Set dgSearch.DataSource = srs
__________________
Slovenia - the green treasure of Europe
Reply With Quote
  #7  
Old 06-13-2003, 02:55 AM
Trivial Trivial is offline
Newcomer
 
Join Date: Jun 2003
Location: Brisbane, Australia
Posts: 11
Default

Ok Thanks heaps for the help, my code now looks like this:

Public strCriteria As String
Public strField As String
Public ssql As String


Private Sub Form_Load()

Dim ssql As String
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=C:\Documents and Settings\Administrator\Desktop\Mydatabase.mdb"

Dim srs As ADODB.Recordset
Set srs = New ADODB.Recordset

ssql = "SELECT * FROM Members WHERE " & strField & " = '" & strCriteria & "'"
Set srs = conn.Execute(ssql)


If srs.EOF Then
MsgBox "There are no " + strField + "s that match " & "'" + strCriteria + "'", vbOKOnly, "No Match"
Else
Do While Not srs.EOF

MsgBox srs(strField) & " was found" ' this could be a lot of boxes
srs.MoveNext
Loop
End If

End Sub

This is looking much better, can anyone suggest where I place the (Set dgSearch.DataSource = srs) like statement, or whatever else will display it to the grid. You have all been very helpful, you dont know how much this helps .
Reply With Quote
  #8  
Old 06-13-2003, 03:01 AM
joe77s joe77s is offline
Centurion
 
Join Date: Feb 2002
Location: Indonesia
Posts: 170
Thumbs up

after this code.....
Set srs = conn.Execute(ssql)
Set dgSearch.DataSource = srs


....nice can help.....
__________________
every day is a Gift, how great Thou art

www.corpussoft.com
Reply With Quote
  #9  
Old 06-13-2003, 03:14 AM
Trivial Trivial is offline
Newcomer
 
Join Date: Jun 2003
Location: Brisbane, Australia
Posts: 11
Default

It doesnt like that line grrr. I get run-time error '7004' The rowset is not bookmarkable. Am I forgetting to set something somewhere else?
Reply With Quote
  #10  
Old 06-13-2003, 03:15 AM
BostjanK's Avatar
BostjanK BostjanK is offline
Junior Contributor
 
Join Date: Feb 2003
Location: Ljubljana, Slovenia
Posts: 324
Default

Actually, when I wrote Do While...Loop that was meant as an alternative to Set dgSearch.DataSource = srs.
Displaying a MsgBox with each record is of course annoying, but Do While...Loop is great if you need to cotrol the population of a DataGrid (or any other control).
Let's say you have DBGrid with two columns - 1st is a number from DB (field1), while 2nd is field1*2. This would look like:
Code:
i = 1 Do While Not srs.EOF With DBGrid .row = i .col = 0 .text = srs("Field1") .col = 1 .text = srs("Field1") * 2 End With i = i + 1 srs.MoveNext Loop
__________________
Slovenia - the green treasure of Europe
Reply With Quote
  #11  
Old 06-13-2003, 04:01 AM
joe77s joe77s is offline
Centurion
 
Join Date: Feb 2002
Location: Indonesia
Posts: 170
Thumbs up

ups.......

hehehe....don't be angry....

what grid are you using......

if you use Microsoft DataGrid Control (msDatGrd.Ocx)
you better use DataControl....
__________________
every day is a Gift, how great Thou art

www.corpussoft.com
Reply With Quote
  #12  
Old 06-13-2003, 08:54 PM
Trivial Trivial is offline
Newcomer
 
Join Date: Jun 2003
Location: Brisbane, Australia
Posts: 11
Default

Ok yea I got the idea that you meant do one or the other but neither of them filled the grid. I've put in the new loop you gave me and I get an error saying rowset not available, any ideas?

Im using an adoc and datagrid, at this stage Im willing to sacrifice looks for functionality so any ideas to get the results in the form of a list or grid would be great.
Reply With Quote
  #13  
Old 06-13-2003, 10:04 PM
Trivial Trivial is offline
Newcomer
 
Join Date: Jun 2003
Location: Brisbane, Australia
Posts: 11
Default

Ok I put in this line like I saw on lots of other posts and it still didnt work:
conn.CursorLocation = adUseClient
But then I went back to my old loop without the .row lines in it and it worked!!!
Thank you so much everyone for all your help! Look out for my name because I may be asking a lot more questions, and I look forward to helping other people when I get more confident with VB.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL results mam0014 Database and Reporting 4 05-14-2003 12:14 PM
Problem with writing Twice Renidrag Web Programming 20 11-07-2002 02:52 PM
Unstable sql select results monjeos Database and Reporting 5 08-07-2002 01:31 AM
Creating Access Databases TomGuy Database and Reporting 8 07-10-2002 10:56 AM
How to compare an empty value in a query.. pragash Database and Reporting 1 10-22-2001 11:12 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->