jtammyg
asked on
recordset returns incorrect data when using 3 textboxes
Hi!
I have a userform which has multipages. The first page is the one I am having problems with.
I perform a SEARCH in this page against a database in sql server 2000 and want to get specific results back.
Basically I have 3 textboxes:
textbox1 to enter domain
textbox13 to enter law firm
textbox14 to enter Nickname
I had this working when only searching for a domain with the code below.
Here is the code:
'========================= ========== ========== ========== ========== ========== ========== ========== ===
'This routine performs a search for a specific domain and displays the results in a listbox
'in tabular form
'========================= ========== ========== ========== ========== ========== ========== ========== ===
Private Sub CommandButton1_Click()
Dim ADOCNN As ADODB.Connection
Dim ADOCMD As ADODB.Command
Dim ADOrs As ADODB.Recordset
Dim stConn As String
Dim stSQL As String
Dim tocompare As String
Dim tocompare2 As String
Dim tocompare3 As String
Dim fldcount As Variant
Dim i As Long
'Clear active worksheet columns
Sheet1.Range("A:M").Select
Selection.Clear
tocompare = CStr(Trim(TextBox1.Value))
tocompare2 = CStr(Trim(TextBox13.Value) )
tocompare3 = CStr(Trim(TextBox14.Value) )
Set ADOCNN = New ADODB.Connection
With ADOCNN
.CommandTimeout = 0
.Provider = "SQLOLEDB.1"
.ConnectionString = "Initial Catalog=PrivBank;Data Source=dr-ny-sql001;INTEGR ATED SECURITY=sspi;"
.Open
End With
Set ADOCMD = New ADODB.Command
With ADOCMD
.CommandTimeout = 0
.CommandType = adCmdText
.CommandText = "select * from Domain_List_TEST where [domain] like '" & tocompare & "%' or [law_firm] like '" & tocompare2 & "%' or [NickName] like '" & tocompare3 & "%';"
.ActiveConnection = ADOCNN
End With
Set ADOrs = ADOCMD.Execute
For i = 0 To ADOrs.Fields.Count - 1
Sheet1.Cells(1, i + 1).Value = ADOrs.Fields(i).Name
Next i
Sheet1.Range("A:M").Select
Selection.Font.ColorIndex = 2
Sheet1.Cells(2, 1).CopyFromRecordset ADOrs
Dim rngRowSource As Range
Set rngRowSource = Range("A1").CurrentRegion. Offset(1, 0)
With frmData.ListBox1
.ColumnCount = fldcount - 1
.ColumnHeads = True
.RowSource = rngRowSource.Address
End With
Set ADOCNN = Nothing
Set ADOCMD = Nothing
ADOrs.Close
Set ADOrs = Nothing
Sheet1.Range("N1").Select
End Sub
I modified only this part accordingly:
With ADOCMD
.CommandTimeout = 0
.CommandType = adCmdText
.CommandText = "select * from Domain_List_TEST where [domain] like '" & tocompare & "%' or [law_firm] like '" & tocompare2 & "%' or [NickName] like '" & tocompare3 & "%';"
.ActiveConnection = ADOCNN
End With
and of course declared the variables at the beginning.
Now what I am getting when I search in either of the 3 textboxes is ALL the records from the database (SQL SERVER 2000). This is not what I want, I only want to get the records that meet the condition in textbox 1 or textbox13 or textbox14 or if i enter in all 3 textbox that meets all. I hope this is clear enough.
Any ideas where I am going wrong with this?
Thanks a lot!
JT
I have a userform which has multipages. The first page is the one I am having problems with.
I perform a SEARCH in this page against a database in sql server 2000 and want to get specific results back.
Basically I have 3 textboxes:
textbox1 to enter domain
textbox13 to enter law firm
textbox14 to enter Nickname
I had this working when only searching for a domain with the code below.
Here is the code:
'=========================
'This routine performs a search for a specific domain and displays the results in a listbox
'in tabular form
'=========================
Private Sub CommandButton1_Click()
Dim ADOCNN As ADODB.Connection
Dim ADOCMD As ADODB.Command
Dim ADOrs As ADODB.Recordset
Dim stConn As String
Dim stSQL As String
Dim tocompare As String
Dim tocompare2 As String
Dim tocompare3 As String
Dim fldcount As Variant
Dim i As Long
'Clear active worksheet columns
Sheet1.Range("A:M").Select
Selection.Clear
tocompare = CStr(Trim(TextBox1.Value))
tocompare2 = CStr(Trim(TextBox13.Value)
tocompare3 = CStr(Trim(TextBox14.Value)
Set ADOCNN = New ADODB.Connection
With ADOCNN
.CommandTimeout = 0
.Provider = "SQLOLEDB.1"
.ConnectionString = "Initial Catalog=PrivBank;Data Source=dr-ny-sql001;INTEGR
.Open
End With
Set ADOCMD = New ADODB.Command
With ADOCMD
.CommandTimeout = 0
.CommandType = adCmdText
.CommandText = "select * from Domain_List_TEST where [domain] like '" & tocompare & "%' or [law_firm] like '" & tocompare2 & "%' or [NickName] like '" & tocompare3 & "%';"
.ActiveConnection = ADOCNN
End With
Set ADOrs = ADOCMD.Execute
For i = 0 To ADOrs.Fields.Count - 1
Sheet1.Cells(1, i + 1).Value = ADOrs.Fields(i).Name
Next i
Sheet1.Range("A:M").Select
Selection.Font.ColorIndex = 2
Sheet1.Cells(2, 1).CopyFromRecordset ADOrs
Dim rngRowSource As Range
Set rngRowSource = Range("A1").CurrentRegion.
With frmData.ListBox1
.ColumnCount = fldcount - 1
.ColumnHeads = True
.RowSource = rngRowSource.Address
End With
Set ADOCNN = Nothing
Set ADOCMD = Nothing
ADOrs.Close
Set ADOrs = Nothing
Sheet1.Range("N1").Select
End Sub
I modified only this part accordingly:
With ADOCMD
.CommandTimeout = 0
.CommandType = adCmdText
.CommandText = "select * from Domain_List_TEST where [domain] like '" & tocompare & "%' or [law_firm] like '" & tocompare2 & "%' or [NickName] like '" & tocompare3 & "%';"
.ActiveConnection = ADOCNN
End With
and of course declared the variables at the beginning.
Now what I am getting when I search in either of the 3 textboxes is ALL the records from the database (SQL SERVER 2000). This is not what I want, I only want to get the records that meet the condition in textbox 1 or textbox13 or textbox14 or if i enter in all 3 textbox that meets all. I hope this is clear enough.
Any ideas where I am going wrong with this?
Thanks a lot!
JT
ASKER
angelll
thank you for your reply.
although that only works when i insert a search for the 3 boxes. i want to be able to have either one or the other or the other or search in the 3 of them.
I hope this is clear enough.
Thanks!
Tammy
thank you for your reply.
although that only works when i insert a search for the 3 boxes. i want to be able to have either one or the other or the other or search in the 3 of them.
I hope this is clear enough.
Thanks!
Tammy
sorry... copy/paste error
Dim sql as String
dim sep as string
sep = " where "
sql = "select * from Domain_List_TEST "
if tocompare <> empty then
sql = sql & sep & " [domain] like '" & tocompare & "%' "
sep = " OR "
end if
if tocompare2 <> empty then
sql = sql & sep & "[law_firm] like '" & tocompare2 & "%' "
sep = " OR "
end if
if tocompare3 <> empty then
sql = sql & sep & " [NickName] like '" & tocompare3 & "%' "
sep = " OR "
end if
With ADOCMD
.CommandTimeout = 0
.CommandType = adCmdText
.CommandText = sql
.ActiveConnection = ADOCNN
End With
ASKER
angelll,
where would u incorporate this in my code?
thanks!
Tammy
where would u incorporate this in my code?
thanks!
Tammy
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window