passing arrary threw query

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2007
Posts: 16
Reputation: Ravi Singhal is an unknown quantity at this point 
Solved Threads: 0
Ravi Singhal Ravi Singhal is offline Offline
Newbie Poster

passing arrary threw query

 
0
  #1
Jun 25th, 2007
hi ,
i am writing a programme in vb6. I want to check whether my cities are matching with cities in database.
so i want to ask that how to pass a array to a database by query.
I have written the following code please help me

Dim CITYARRAY(5) As String
CITYARRAY(0) = "MIAMI"
CITYARRAY(1) = "FRED"
CITYARRAY(2) = "INDIAN HILL"
CITYARRAY(3) = "MARTINSVILLE"
CITYARRAY(4) = "MARY"

Dim connection As New ADODB.connection
connection.Open ("Provider =Microsoft.Jet.OLEDB.4.0;Data Source =L:\RDES Training\Geocode World RL6.0\USGEO.MDB;PERSIST SECURITY INFO = FALSE")
Dim QUERY As String
QUERY = "SELECT CITY FROM USCITY WHERE CITY IN ('" & CITYARRAY & "') "
Dim RECORDSET As New ADODB.RECORDSET
RECORDSET.Open QUERY, connection, adOpenDynamic, adLockOptimistic
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 319
Reputation: DenisOxon is an unknown quantity at this point 
Solved Threads: 15
DenisOxon's Avatar
DenisOxon DenisOxon is offline Offline
Posting Whiz

Re: passing arrary threw query

 
0
  #2
Jun 25th, 2007
What is your question ?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 537
Reputation: choudhuryshouvi is an unknown quantity at this point 
Solved Threads: 49
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro

Re: passing arrary threw query

 
0
  #3
Jun 26th, 2007
Try this code...
'used stuffs :- button(command1), list box(list1) , database,table and fields name remain same
Option Explicit

Dim connection As ADODB.connection
Dim recordset As ADODB.recordset
Dim CITYARRAY(5) As String, QUERY As String

Private Sub Command1_Click()
Dim i, f, nf As Integer

f = 0 'variable for tracking total no. of cities found in the database
nf = 0 'variable for tracking total no. of cities not found in the database

CITYARRAY(0) = "MIAMI"
CITYARRAY(1) = "FRED"
CITYARRAY(2) = "INDIAN HILL"
CITYARRAY(3) = "MARTINSVILLE"
CITYARRAY(4) = "MARY"

'looping through each element of the array,sending its value through the query and checking whether this value does exist in the database or not.
'if match found then increasing the f variable by 1 & printing msg in the listbox otherwise increasing the nf variable by 1 & also displaying a msg in the
'listbox
For i = 0 To 4 Step 1
QUERY = "select city from uscity where city in(" & Chr(34) & CITYARRAY(i) & Chr(34) & ")"
recordset.Open QUERY, connection, adOpenDynamic, adLockOptimistic

If recordset.RecordCount > 0 Then
List1.AddItem CITYARRAY(i) & " [ Found in the database ]"
f = f + 1
Else
List1.AddItem CITYARRAY(i) & " [ Not Found in the database ]"
nf = nf + 1
End If
recordset.Close
Next i

'printing summary of the above query
List1.AddItem ""
List1.AddItem "-=Result of Searching=-"
List1.AddItem "Cities found : " & f
List1.AddItem "Cities not found : " & nf
End Sub

Private Sub Form_Load()
'initiating the connection and recordset objects here. try to avoid doing the same in general declaration section
Set connection = New ADODB.connection
Set recordset = New ADODB.recordset

recordset.CursorLocation = adUseClient
recordset.CursorType = adOpenDynamic

'specifiying location of the database file dynamically so there is no need to mention the path of the database everytime the location of that file is
'changed. app.path automatically detects path of the current working directory of the project file.
connection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\usgeo.mdb" & ";PERSIST SECURITY INFO=FALSE"
End Sub
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 848
Reputation: QVeen72 is on a distinguished road 
Solved Threads: 120
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Posting Shark

Re: passing arrary threw query

 
0
  #4
Jun 28th, 2007
Hi ,

Try This :

Dim NewStr As String
NewStr = "'" & Join(CITYARR,"','") & "'"

QUERY = "SELECT CITY FROM USCITY WHERE CITY IN (" & NewStr & ")"

REgards
Veena
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 95
Reputation: manoshailu is an unknown quantity at this point 
Solved Threads: 9
manoshailu's Avatar
manoshailu manoshailu is offline Offline
Junior Poster in Training

Re: passing arrary threw query

 
0
  #5
Jun 28th, 2007
Hai ravi,
u can use below coding according to ur requirement. Al the best
Try below coding.....
Private Sub Command1_Click()
Dim CITYARRAY(5) As String
CITYARRAY(0) = "MIAMI"
CITYARRAY(1) = "FRED"
CITYARRAY(2) = "INDIAN HILL"
CITYARRAY(3) = "MARTINSVILLE"
CITYARRAY(4) = "MARY"
Set rs = Nothing
For i = 0 To 4
rs.Open "select city from uscity where city = '" & CITYARRAY(i) & "'", cn, adOpenDynamic, adLockOptimistic
If rs.BOF = False And rs.EOF = False Then
Print rs(0)
End If
rs.Close
Next
End Sub

Regards,
Shailaja
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC