PDA

View Full Version : SQL "Like" referencing a textbox



bigtalljv
11-14-2011, 01:13 PM
Hi,

I have a sql statement that I would like to have reference a text box in order to reduce the number of returns.

This is the statement,
rst.Open "SELECT DISTINCT TOP 25 [facname] FROM tbl_facilites WHERE [facname] like '%" & ThisDocument.FormFields("TextBox1") & "%' ORDER BY [facname];", _

The red text was added by me. It works great without it but I have 300 rows and this feeds a drop down that obviously can only hold 25. I just can't seem to figure out how to get this to work. It's throws several different errors when I try this. 3021, either BOF or EOF is True or the current record has been deleted...

The whole thing looks like this, I obviously didn't write it.

thanks,
Jason

Private Sub Document_Open()
On Error GoTo Document_Open_Err
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=wg fog db.mdb;"
rst.Open "SELECT DISTINCT TOP 25 [facname] FROM tbl_facilites ORDER BY [facname];", _
cnn, adOpenStatic
rst.MoveFirst
With ActiveDocument.FormFields("Dropdown1").DropDown.ListEntries
.Clear
Do
.Add rst![facname]
rst.MoveNext
Loop Until rst.EOF
End With
Document_Open_Exit:
On Error Resume Next
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Exit Sub
Document_Open_Err:
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
Resume Document_Open_Exit
End Sub

bigtalljv
11-14-2011, 02:36 PM
of course as soon as i typed this i figured it out. this worked perfectly

rst.Open "SELECT DISTINCT TOP 25 [facname] FROM tbl_facilites WHERE facname LIKE '%" & ActiveDocument.FormFields("TextBox1").Result & "%' ORDER BY [facname];", _
Jason