Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching Record , using ADO

Status
Not open for further replies.

jakhan

IS-IT--Management
Jul 22, 2000
75
0
0
hi folks

I am using VB 6, and creating a database searching form.

I want to search the string from database table (string entered by user into textbox) e.g.

select * from users where fname = "khan"

also the results to be stored in a grid
Can you summarize all procedure and tell me how to do that?

thanks

Jahangir
 
Dear Jahangir,

I'll try illustrate what you require with MSFLex Grid control.

'Simply pass this function the flexgrid object and the sql statement
'and it will do the rest.

'
'This could be customised as per your needs
'Parameter :
' objGrid : The flexgrid object
' sSQL : The sql statement used to retrieve the data
' Returns :
' true : If the grid is successfully filled
' false : If the grid is not filled
Private Function FillGrid(objGrid As MSFlexGrid, sSQL As String)

On Error GoTo errHandler
Dim sConStr As String
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim lngCurrRow As Long ' Current row in the grid
Dim intCols As Integer ' Total cols
Dim intCurrCol As Integer ' Current col in the grid

sConStr = "Your Database DSN"

cn.ConnectionString = sConStr
cn.Open

rs.Open sSQL, cn
If rs.EOF Then
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
FillGrid = False
Exit Function
End If

intCols = rs.Fields.Count

'Print Header

With objGrid
.Cols = intCols
.FixedCols = 0
.Rows = 2
For intCurrCol = 0 To intCols - 1
.TextMatrix(0, intCurrCol) = rs.Fields(intCurrCol).Name
Next
End With

lngCurrRow = 1
Do While rs.EOF = False
With objGrid
For intCurrCol = 0 To intCols - 1
.TextMatrix(lngCurrRow, intCurrCol) = rs.Fields(intCurrCol) & ""
Next
.Rows = .Rows + 1
End With
rs.MoveNext
lngCurrRow = lngCurrRow + 1
Loop
FillGrid = True ' Grid has been successfully filled

Exit Function

errHandler:
MsgBox Err.Description

End Function

Please customise the error handling as per the app. needs.
Hope you will find something out of this.

Regards,
Rajesh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top